David Abel

Portrait


Python Tutorial

python logo

This tutorial is intended as an introduction to Python for folks with some prior programming experience.

Section 2: Functions (functions.py)

Now we'll get into making some functions with the basics introduced above.

Open basics.py in your favorite text editor. Again, remember that white space is strongly enforced, so you must indent inside each subsequent body.

Python is strongly, dynamically typed, so the parameters in function headers do not include a type, as opposed to say Java or C++. For example:

def function_name (arg_one, arg_two=default_value):
    # Body

Notice again that a colon is required at the end of the function header (if you get syntax errors, check to make sure you included your colons! This is a super common mistake).

Assigning a default value (as is the case for arg_two) allows a function to be called without that parameter, if you'd like - the second parameter will default to the provided default_value if no value is provided.

All python functions return the None type by default, which is python's version of null/nil.

There are two functions in basics.py for you to implement:

[1] fizz_buzz()

This function should print out the numbers from 1 to 100. For multiples of three, print "fizz", instead of the number, and for multiples of five, print "buzz" instead of the number. For numbers that are multiples of three and five, print "fizzbuzz" (instead of the number). You will need the "modulo" operator, a % b, which evaluates a mod b. For example:

>>> 5 % 3
2
>>> 5 % 5
0
>>> 3 % 7
3

You can cast objects to different types in python using the following functions:

>>> print float(5)
5.0
>>> print int("5")
5
>>> print str(5)
'5'

If you're ever confused about the type of an object in python, use the built in type() function call:

>>> print type(True)
<type 'bool'>
>>> print type(3 == 4)
<type 'bool'>

Recall the range() function, which will also be of help for the fizzbuzz() function:

>>> print range(10)
[0,1,...,9]

Returns a list of all the integers from 0 to the provided number (exclusive):

A reminder that you can loop through lists as follows:

>>> for num in [3,4,5]:
>>>     print num
3
4
5
>>> for num in range(3,6):
>>>     print num
3
4
5

Happy fizz buzzing! Remember to run a file, just enter: "(dir/of/file) python python_file.py" at the Terminal/*nix Shell/cygwin. Shoot me an email (dabel@cs.brown.edu) if you have questions! (Seriously!)

[2] is_palindrome(string)

This function should return True if the provided string is a palindrome, and False otherwise. We will also require that the string provided contains only letters. A few python functions that will be helpful:

>>> len("hey") # Returns the length of the object (defined for most types you would expect)
3

For example:

>>> len([1,2,3])
3
>>> len("howdy")
5
>>> range(len("howdy")))
[0,1,2,3,4]

A quick refresher on Booleans. Boolean connectives are simply the words "and", "or", and "not":

>>> if not 4 > 5:
>>>     print "woohoo!"
woohoo!

The exclamation point can also be used for inequality:

>>> if 5 != 4:
>>>     print "woohoo!" woohoo!

For our is_palindrome() function, we will require that only strings consisting of letters can be a palindrome (i.e. "!e!" is not a palindrome in our example). We can use the <str>.isalpha() string method to check this:

>>> print "abc".isalpha()
True
>>> print "!22$5".isalpha()
False

Note that the Boolean values True and False are capitalized in Python.

Good luck!

< Previous Section Next Section >