Python: Examples

Note: Lines beginning with ">>>" and "..." indicate input to Python (these are the default prompts of the interactive interpreter). Everything else is output from Python.

This page contains a few fragments of actual Python code. They are intended to give you an impression of the overall syntax and how certain constructs look like.

Let's start with the simplest thing: A minimalist hello-world program.

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

Well, that doesn't really look much different from other common languages. How about putting the code into a function using a string variable?

>>> def hello (what):
...     text = "Hello, " + what + "!"
...     print text
>>> hello ("World")
Hello, World!

Python has flow control statements like most other common languages: if/else, while, for, break, continue. It also supports exceptions for easy and proper error handling.

One interesting difference from C is in the for loop, which can be applied to any "iterable" object:

>>> str = "foo"; lst = ["abra", 2038, "cadabra"]
>>> for char in str:
...     print char
>>> for elem in lst:
...     print elem

An "iterable" object doesn't have to be a string or a list. In fact, the elements of the for loop don't even have to exist all at once. The following example demonstrates a so-called generator function. It can be used as an iterable object which creates its objects on the fly.

>>> def iterquad ():
...     for i in range(5):
...         yield (i*i)
>>> for j in iterquad():
...     print j

The following piece of code shows a simple function definition. Some languages differentiate between functions that return a value and functions that don't, calling the latter "procedures" (like in Pascal, for example). However, in Python it's all called functions, just like in C. If there is no explicit return value, then the value None is returned implicitely. Unlike in C, a Python function can return multiple values by returning a tuple.

>>> def quadcube (x):
...     return x**2, x**3
>>> a, b = quadcube(3)
>>> print a
>>> print b

Functions can be recursive, can have default arguments, and you can easily write functions which accept a variable amount of arguments of variable types, and which also return a variable amount of values of variable types.

In Python, functions are first-class objects: You can write functions that return functions (which can be created dynamically on the fly). You can pass around functions like any other object.

Now let's have a look at the object-orientation. The following is a very simple class definition.

>>> class Student:
...     def __init__ (self, name, age, gender):
...   = name
...         self.age    = age
...         self.gender = gender
>>> Sue = Student("Susan Miller", 20, "f")
>>> print Sue
<__main__.Student instance at 0x81a96cc>
>>> print Sue.age

Within classes, you can override all standard operators, including assignment and array-indexing. Python supports multiple inheritance, class methods and meta-classes. All standard types (integers, strings, lists etc.) are classes, too, so you can subclass them and change the inherited behaviour.

[HTML 4.01]