Python Club

Meeting 2


Agenda

  • Chapter 3 Discussion and Questions
  • Exercises
  • Next session

Chapter 3 Discussion and Questions

Exercises

Exercise 0

Implmement the mathematical function in a python function:

f(x) = x + 1

Now create two new python functions , g and h that peform the following:

g(x) = 2f(x)
h(x) = f(2x)

Exercise 1

Move the last line of this program to the top, so the function call appears before the definitions. Run the program and see what error message you get.

Exercise 2

Move the function call back to the bottom and move the definition of print_lyrics after the definition of repeat_lyrics. What happens when you run this program?

def print_lyrics():
    print "I'm a lumberjack, and I'm okay."
    print "I sleep all night and I work all day."

def repeat_lyrics():
    print_lyrics()
    print_lyrics()

repeat_lyrics()

Exercise 3

Python provides a built-in function called len that returns the length of a string, so the value of len('allen') is 5. Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.

>>> 'right_justify('allen')
                                                                 allen

Exercise 4

A function object is a value you can assign to a variable or pass as an argument. For example, do_twice is a function that takes a function object as an argument and calls it twice:

def do_twice(f):
    f()
    f()

Here’s an example that uses do_twice to call a function named print_spam twice.

def print_spam():
    print 'spam'

do_twice(print_spam)

Type this example into a script and test it. Modify do_twice so that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument. Write a more general version of print_spam, called print_twice, that takes a string as a parameter and prints it twice. Use the modified version of do_twice to call print_twice twice, passing 'spam' as an argument. Define a new function called do_four that takes a function object and a value and calls the function four times, passing the value as a parameter. There should be only two statements in the body of this function, not four. Solution: http://thinkpython.com/code/do_four.py.

Exercise 5

This exercise can be done using only the statements and other features we have learned so far. Write a function that draws a grid like the following:

+ - - - - + - - - - +
|         |         |
|         |         |
|         |         |
|         |         |
+ - - - - + - - - - +
|         |         |
|         |         |
|         |         |
|         |         |
+ - - - - + - - - - +

Hint: to print more than one value on a line, you can print a comma-separated sequence:

print '+', '-'

If the sequence ends with a comma, Python leaves the line unfinished, so the value printed next appears on the same line.

print '+', 
print '-'

The output of these statements is '+ -'. A print statement all by itself ends the current line and goes to the next line.

Write a function that draws a similar grid with four rows and four columns.

Exercise 6

We are going to extend Exercise 5 so that it can print grids n x n grids for any n. The steps are:

1. Write a function `get_line(symbol1, symbol2, n)` that returns a string such that `symbol1` is printed followed by four copies of `symbol2`, repeat `n` times. Finally, ensure that all symbols are sepearted by spaces. For example:

>>> print get_line('+', '-', 6)
+ - - - - + - - - - + - - - - + - - - - + - - - - + - - - - +
2. Write a function `get_row(n)` that returns one single row of the grid as a single string. Hint: use '\n'. For example,

>>> r = get_row(4)
>>> r
'+ - - - - + - - - - + - - - - + - - - - +\n|         |         |         |         |\n|         |         |         |         |\n|         |         |         |         |\n|         |         |         |         |\n'
>>> print r
+ - - - - + - - - - + - - - - + - - - - +
|         |         |         |         |
|         |         |         |         |
|         |         |         |         |
|         |         |         |         |
3. Write a function `print_grid(n)` that returns the complete complete of `n` rows and `n` columns.

Next session