David Abel

Portrait


Python Tutorial

python logo

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

Section 1: Basics (interpreter, hello_world.py )

Open up the python interpreter. Follow along with the commands below to get a sense of some python basics.

>>> print "Hello World!"
Hello World!

Simple! Anything contained in quotes "" is a String type in python, abbreviated to type str. Now create a variable to hold on to the string and try the commands below:

>>> h = "Hello World!"

Note that you do not need to specify the type of h. You can index into a string as though it were an array:

>>> print h[0]
H
>>> print h[1]
e

Negative numbers are used to access elements from the end of the object:

>>> print h[-1]
!
>>> print h[-2]
d

The slice command is a nice shortcut for chopping up strings:

>>> print h[1:]
ello World!
>>> print h[:3]
Hel
>>> print h[:-1]
Hello World

You can also add elements easily: (note: shorthand for x = x + y is x += y)

>>> h = h + " Goodbye"
>>> print h
Hello World! Goodbye

All of these operations also apply to lists:

>>> h = [1, 2, 3, "hello", True]
>>> h.append(-5.5)
>>> print h[1:]
[2, 3, "hello", True, -5.5]
>>> print h[-1]
-5.5

In python, lists may include data of a variety of types, as is the case above. Try making a quick list of integers with the range() function:

>>> print range(4)
[0,1,2,3]
>>> print range(1,4)
[1,2,3]
>>> print range(4,0,-2)
[4,2]

The parameters to range() are as follows: range(lower_bound, upper_bound, increment), where lower_bound is inclusive, upper_bound is exclusive. Note that lower_bound defaults to 0, and increment defaults to 1. This will become extremely useful for quickly creating loops.

Note that the # is used for single line comments:

>>> # This is a comment
>>> print "This is not a comment"
This is not a comment

You can also make tuples quickly just by separating data with commas:

>>> tup = "a", "b", "c"

To make a tuple of one element, just trail the data with a comma:

>>> tup = "a",

Conditionals are pretty straight forward in python:

>>> if 5 > 4 and 3 == 3:
>>>     print "Woohoo!"
>>> elif 7 > 2 or 4 != 3:
>>>     print "How did we get here?"
>>> else:
>>>     print ":("
Woohoo!

*IMPORTANT*: all subsequent scopes require an additional indent. Python strongly enforces indentation. Additionally, at the beginning of a new scope (loop, conditional, function, class definition, etc.), you need a colon.

For instance, a for loop follows the general structure of:

>>> for (var) in (iterable):
>>>     # Body

For example, try these

>>> for char in "escher":
>>>     print char
>>> for x in range(5):
>>>     if x > 2:
>>>         print x

To exit the python interpreter, press Ctrl+D, or enter quit().

The file hello_world.py in the code contains all of the above examples in case you want to look through anything again. You can run this file by telling the python interpreter to interpret the file from a *nix shell, or cygwin on windows: (dir/of/file) python hello_world.py. This should print out Hello World!

< Previous Section Next Section >