Programming in Python

Exercise 1

  1. Translate the following expression into code: $\frac{1.3/1.2 + 4.3 \times 2.0}{1.5 - 2.3}$

  2. Check if 4431 and 7523 are equal mod 7.

  3. Define pi using the approximation $\frac{355}{113}$. Use it to compute the circumference and area of a circle of radius 5. (There's a subtle error which can occur in this problem! Try printing out pi after defining it. Do you get an unexpected answer? What do you think happened?)

Exercise 2

  1. Write a short function which decides if an integer is even.

  2. Fill the details for the absolute value function.

  3. Write a function which squares a number and then use it to write a function which takes three integers and returns the sum of their squares.

  4. Write a function which computes the factorial of an integer.

Exercise 3

  1. Write a function which adds up the squares of the first $n$ odd integers.

  2. Write a function which prints the natural numbers less than $n$ which are divisible by 3, divisible by 5 but not divisible by 10.

  3. Write a program which prints out an 16 x 16 "chess board" which indicates a black square with a 'B' and a white square with a 'W'.

  4. Print out the first ten Fibonacci numbers.

  5. Try reimplementing the factorial function using either a while loop or an iterator. Which do you think is better? Which do you think is easier to understand?