André Restivo

Python Exercises

Simple

  1. Write a program that prints ‘Hello World’ to the screen.
  2. Write a program that asks the user for their name and greets them with their name.
  3. Modify the previous program such that only the users Alice and Bob are greeted with their names.
  4. Write a program that asks the user for a number n and prints the sum of the numbers 1 to n.
  5. Modify the previous program so that it uses a function called sum_numbers.
  6. Write a program that asks for the radius of a circle and calculates its area (use the math module to get PI: math.pi).

Lists

  1. Write a function that returns the largest element in a list.
  2. Write a function that reverses a list.
  3. Write a function that checks whether an element occurs in a list.
  4. Write a function that computes the sum of all elements of a list.

Database

Start by saving the following data into a CSV file called employees.csv.

21,"William",1400,1,3
22,"Evelyn",1600,2,7
23,"Jackson",900,3,11
24,"Ella",750,4,13
25,"Harper",2000,5,17

Using the employees database:

  1. Write a program that read this CSV file and stores it into the employees table.
  2. Write a program that writes into a salaries.csv file the average salary of each department (id, name, average).
  3. Write a program that asks the user for a filename and a department id and writes into the file all the employee data from that department in CSV format.