Python tutorial

This is a very brief and focused overview of the Python language. It assumes some familiarity with at least one other programming language, such as C or Java.

Values

We’ll start with the syntax for various kinds of values and operators on them. In this section, you can use the ipython command in a terminal window to follow a long. Any code you type at the ipython prompt will be evaluated and its result displayed.

ipython command

ipython command

To exit ipython and return to the regular shell, use control-D at the prompt, and then confirm by pressing enter.

Numbers

Integers (whole numbers) are represented exactly as you expect: just digits, possibly with a negative sign:

In [1]: 123
Out[1]: 123

In [2]: -6
Out[2]: -6

The operators are what you’d expect:

In [4]: 123 + 234
Out[4]: 357

In [5]: 234 - 123
Out[5]: 111

In [6]: 123 * 234
Out[6]: 28782

In [7]: 234 / 3
Out[7]: 78

In [8]: 234 % 4
Out[8]: 2

As in C/Java, the % is the modulus operator – the remainder when you divide.

Floats (real numbers) have a decimal point. As usual, their representation is not precise and is subject to rounding errors, as you see in the first entry below. You can use the period character to distinguish between an integer and a float, as in the last two entries below.

In [9]: 3.1415
Out[9]: 3.1415000000000002

In [10]: type(3.1415)
Out[10]: <type 'float'>

In [11]: type(3)
Out[11]: <type 'int'>

In [12]: type(3.)
Out[12]: <type 'float'>

The division operator does integer division if both operands are integers. Integer division always produces an integer. So for example, 9 divided by 2 is 4. To force float division, you can add the decimal point to either operand, or use the float() function to convert:

In [13]: 9 / 2
Out[13]: 4

In [14]: 9 / 2.
Out[14]: 4.5

In [15]: 9. / 2
Out[15]: 4.5

In [16]: 9 / float(2)
Out[16]: 4.5

In [17]: float(9 / 2)
Out[17]: 4.0

In the last entry, we the result to a float but at that point, it is already the integer 4, not 4.5.

Strings

There are a variety of string types in Python. To start with, a basic string is surrounded by single or double quotes; these are interchangeable.

In [19]: 'My name'
Out[19]: 'My name'

In [20]: "Your idea"
Out[20]: 'Your idea'

As in C/Java, you can use \n to represent a new line.

In [18]: print "Hello\nworld."
-------> print("Hello\nworld.")
Hello
world.

The plus + operator on strings is used to join them together (concatenation).

In [21]: "Hello, " + "world"
Out[21]: 'Hello, world'

A basic string may not span multiple lines of source code (although it can contain multiple \n characters), but there is a variant using triple-double quotes that can embed newlines directly:

In [24]: """This is a string
   ....: that can have newlines
   ....: directly in it."""
Out[24]: 'This is a string\nthat can have newlines\ndirectly in it.'

One more variant is a raw string, in which newlines and other backslashed characters are taken as is, not interpreted as usual. Use either single or double quotes, but precede the opening quote with r:

In [25]: print r"Hello\nworld"
-------> print(r"Hello\nworld")
Hello\nworld

In [26]: r'This contains \n, not a new line.'
Out[26]: 'This contains \\n, not a new line.'

Finally, you can mark a string as being unicode, which means it’s capable of storing characters from all the world’s languages. For this, the opening quote is preceded by u:

In [30]: u'Hello'
Out[30]: u'Hello'

In [31]: u'café'
Out[31]: u'caf\xc3\xa9'

In Django applications, you’ll see where each form is used.

The type of strings is designated str or unicode, and these are also the names of functions that convert other objects to strings. In reverse, int() and float() can convert strings back to numbers.

In [32]: type('hi')
Out[32]: <type 'str'>

In [33]: type(u'hi')
Out[33]: <type 'unicode'>

In [34]: str(3.141)
Out[34]: '3.141'

In [35]: unicode(99)
Out[35]: u'99'

In [36]: int('3')
Out[36]: 3

In [37]: float('3.14')
Out[37]: 3.1400000000000001

In [38]: int('3.14')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/home/league/c/f12/<ipython console> in <module>()

ValueError: invalid literal for int() with base 10: '3.14'

In [39]: int(3.14)
Out[39]: 3

Note the error produced by trying to convert '3.14' directly to an integer.

Find the length of a string with len():

In [40]: len('Hello\nworld')
Out[40]: 11

In [41]: len(r'Hello\nworld')
Out[41]: 12

Variables

Variable names in Python contain letters, numbers, and underscores, but (same as C/Java) cannot start with a number. Unlike C/Java, you do not have to declare variables before you use them. Simply assign them a value, and then you can use the value later on.

In [45]: myvar = 32

In [46]: print myvar
-------> print(myvar)
32

In [47]: print myVar
-------> print(myVar)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/home/league/c/f12/<ipython console> in <module>()

NameError: name 'myVar' is not defined

In [49]: myvar * myvar
Out[49]: 1024

.py files

So far, we have been typing code into the interactive ipython loop. To write a standalone program, you create a file with the extension .py. In gedit, just save a new document into the cs690 directory, naming it hello.py. You should see it change the language mode (status bar at bottom) to Python.

A python program in gedit

A python program in gedit

To run a python file, open the terminal and change to the cs690 directory. Then enter the command python (not ipython this time) followed by the file name:

liucs:~$ cd cs690
liucs:~/cs690$ python hello.py
Hello, world.
The area of the circle is 78.5398163397

You can also see comments in this program – they are the lines that start with a pound sign (#).

Value exercises

  1. Explain why len('Hello\nworld') and len(r'Hello\nworld') produce different results.

  2. Write a Python expression that will convert the string '3.14' to the integer 3 without producing an error.

  3. Why did we get the NameError in the section on Variables above (input 47)?

  4. Does Python obey the usual rules of operator precedence? Hint: what is the result of the expression 1+2*3?

  5. Write a python program rectangle.py that defines two variables length and width (values 4 and 5, respectively) and computes the perimeter of a rectangle with those dimensions. It should work like this:

    liucs:~/cs690$ python rectangle.py
    The perimeter of a 4 x 5 rectangle is 18.

Control structures

One thing that is unusual about Python compared to C/Java and other languages is that indentation is used delimit blocks of code for functions, classes, and control structures. In C/Java, we use curly braces to delimit blocks. Indenting statements within a block is considered good programming style, but the compiler doesn’t actually care. In Python, it’s indentation only that controls block structure.

Conditions

Let’s start with conditional statements, using the keywords if and else. Unlike in C/Java, the Boolean expression does not need to be surrounded by parentheses. Instead, the line ends with a colon (:), which in Python generally indicates that the following line (or several lines) will be indented.

if average < 60:
    print "Sorry, you failed."
    print "Please try again."
else:
    print "Yay, you passed!"

In a block like this, the starting column of the if and the else must line up precisely. It’s a common error to forget the colon at the end of an if or else line. That error message would look like this:

  File "scores.py", line 2
    if average < 60
                  ^
SyntaxError: invalid syntax

If you want to cascade a series of if/else logic, you can either keep indenting, or use the elif keyword:

# Version 1 -- keep indenting
if average >= 90:
    print "A"
else:
    if average >= 80:
        print "B"
    else:
        if average >= 70:
            print "C"
        else:
            print "F"
# Version 2 -- elif keyword
if average >= 90:
    print "A"
elif average >= 80:
    print "B"
elif average >= 70:
    print "C"
else:
    print "F"

The relational operators are these:

  <  <=  ==  >=  >

but the logical operators are spelled out:

   and   or   not

In Python, the Boolean values are True and False (must be capitalized).

In [12]: x = 3

In [13]: x == 3
Out[13]: True

In [14]: 3 < 4 or 4 > 3
Out[14]: True

In [15]: not False
Out[15]: True

Loops

Python has a for loop, but it’s not very similar to the way that C/Java use for. In Python’s version, you are always looping over some sequence of things. That will make a little more sense in the section on lists. Meanwhile, we can just iterate over integers, as follows.

The function range(N) produces a list of integers from zero up to (but not including) N. You can test this at the ipython prompt:

In [3]: range(8)
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7]

So, range gives us a sequence of things to iterate over. The loop looks like this:

for i in range(8):
    print i*i

The above code prints the square of each integer from zero up to 7:

0
1
4
9
16
25
36
49

The range function need not start at zero, if you specify the starting point as a parameter. You may also specify a step:

In [6]: range(4,9)
Out[6]: [4, 5, 6, 7, 8]

In [7]: range(4,20,2)
Out[7]: [4, 6, 8, 10, 12, 14, 16, 18]

Besides integer ranges, we can also iterate over the characters in a string. Try something like below – you can do loops directly in ipython if you backspace to unindent on the last line:

In [5]: for c in 'shazam!':
   ...:     print c, c,
   ...: 
s s h h a a z z a a m m ! !

The extra comma at the end of the print statement will prevent it from automatically going to the next line, which is the usual behavior of print.

You can, of course, nest conditional expressions inside loops:

sum = 0
for k in range(20):
    if k % 3 == 0:
        sum += 5
        print k, "is divisible by 3."
print "Sum is", sum

Control exercises

  1. Write a Python program wages.py that asks the user how many hours he/she worked this week, then prints out the pay amount. To get integer input from the user, try this:

    hours = int(raw_input("Enter hours worked: "))

    Assume pay is $20/hour up to 40 hours per week, but then $30/hour for any hours over 40. Below are some sample runs. In each case, the user types in the number of hours, and the program responds with the pay amount.

    liucs:~/cs690$ python wages.py
    Enter hours worked: 32
    Your pay is $ 640
    liucs:~/cs690$ python wages.py
    Enter hours worked: 45
    Your pay is $ 950
  2. This is a popular programming interview question:

    Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz.” For numbers which are multiples of both three and five print “FizzBuzz.”

    Name your solution fizz.py. For example:

    liucs:~/cs690$ python fizz.py
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz
    16
    17
    ...(etc)

Functions

Functions are specified with the keyword def. They take zero or more parameters in parentheses, and the body is indented. Here is a simple example with two functions. The first returns a value, the second does not.

def square(x):
    return x*x

def printAnswer(x, y):
    print "The square of", x, "is", y

for k in range(2,10):
    printAnswer(k, square(k))
liucs:~/cs690$ python func.py
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 8 is 64
The square of 9 is 81

Function exercises

  1. Given the functions defined above, what is the result of the expression square(square(square(3))) ?

  2. Write a Python function exp(a,b) that will compute the exponent a^b. That is, a multiplied by itself b times. (Remember, when b=0, the result is 1.) Here are some test cases you can try – save everything to exp.py:

    • exp(3,4) ⇒ 81
    • exp(5,3) ⇒ 125
    • exp(9,0) ⇒ 1

Data structures

Python has two built-in data structures that are extremely useful: lists, for sequences of objects, and dictionaries that represent a mapping from one object (usually a string label) to another.

Lists

To specify a list, separate the items with commas and surround them with square brackets. Here is a list of integers:

In [1]: myList = [3,4,5,6]

In [2]: myList[0]
Out[2]: 3

In [3]: len(myList)
Out[3]: 4

As you can see, the square bracket notation is also used to fetch elements from the list, starting from index zero. The len() function returns the length, and accessing at that length or beyond is an error:

In [4]: myList[4]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)

/home/league/c/f12/cs690/<ipython console> in <module>()

IndexError: list index out of range

Lists do not need to have every element of the same type, you can freely mixes numbers, strings, and other lists:

In [5]: cool = [1.4, "two", 3, [8, 9, [123]]]

In [6]: len(cool)
Out[6]: 4

In [7]: cool[3]
Out[7]: [8, 9, [123]]

Lists support a very interesting set of operations called slicing. Slices are constructed by placing a colon between the square brackets, like this: myList[:]. Before the colon, you can place a number that indicates the starting index of the slice (the default is zero, the beginning.) After the colon, you can specify the last index of the slice (the default is the length of the list). Here are some examples:

  • myList[:][3, 4, 5, 6] (reproduces the entire list)
  • myList[2:][5, 6] (starting from index 2)
  • myList[:2][3, 4] (starting from 0, up to but not including index 2)
  • myList[1:3][4, 5] (starting from 1, up to but not including index 3)

You can also use negative numbers in slices, to indicate positions counting from the end of the list.

  • myList[-1]6 (last element of the list)
  • myList[:-1][3, 4, 5] (all but last element)
  • myList[-2:][5, 6]
  • myList[-3:][4, 5, 6]

All these slicing operations also work on strings.

  • 'Hello'[1:]'ello'
  • 'peaches'[:-2]'peach'

Dictionaries

A dictionary is a set of key-value pairs, separated by commas and surrounded by curly braces:

course = {'department': 'CS', 'number': 117, 'prereq': ['CS101', 'CS102']}

The keys precede the colons, and are almost always strings. Once you have this dictionary, you can look up its elements using list notation:

  • course['department']'CS'
  • course['number']117
  • course['prereq']['CS101', 'CS102']
  • course['prereq'][1]'CS102'
  • len(course['prereq'])2

You can also iterate over all the keys like this:

course = {'department': 'CS', 'number': 117, 'prereq': ['CS101', 'CS102']}

for k in course.iterkeys():
    print "Value of", k, "is", course[k]
liucs:~/cs690$ python dict.py
Value of department is CS
Value of prereq is ['CS101', 'CS102']
Value of number is 117

Note that the order in which the keys appear in a dictionary is arbitrary. You may not get them back in the same order they were specified when you created the dictionary.

Data exercises

  1. Write a python function tailEquals that will search for the tail of a list (skipping some number of elements from the beginning) whose sum equals some goal. Save it in taileq.py. As a hint, there is a built-in function sum that will return the sum of a list, so sum([3,4,5])12. We would expect the following results:

    • tailEquals([2,4,5,7,9], 16)[7, 9]
    • tailEquals([2,4,5,7,9], 21)[5, 7, 9]
    • tailEquals([2,4,5,7,9], 25)[4, 5, 7, 9]
    • tailEquals([2,4,5,7,9], 18)None

    (None is a Python keyword that represents no value, a bit like NULL/null in C/Java.)

  2. Write a python function hasSame that will search dictionary entries to find one whose value is the same as its key. (If there are multiple such keys, it just returns one of them.)

    • hasSame({'a': 5, 'b': 'b', 'c': 9})b
    • hasSame({'a': 5, 'b': 12, 'c': 'c'})c
    • hasSame({'a': 5, 'b': 'a', 'c': 9})None

Modules

TODO

Exceptions

TODO