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):
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:
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
>>> 5 % 5
>>> 3 % 7
You can cast objects to different types in python using the following functions:
>>> print float(5)
>>> print int("5")
>>> print str(5)
If you're ever confused about the type of an object in python, use the built in type() function call:
>>> print type(True)
>>> print type(3 == 4)
Recall the range() function, which will also be of help for the fizzbuzz() function:
>>> print range(10)
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
>>> for num in range(3,6):
>>> print num
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 (firstname.lastname@example.org) if you have questions! (Seriously!)
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
A quick refresher on Booleans. Boolean connectives are simply the words "and", "or", and "not":
>>> if not 4 > 5:
>>> print "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()
>>> print "!22$5".isalpha()
Note that the Boolean values True and False are capitalized in Python.
< Previous Section Next Section >