Python is a programming language that has a fairly easy syntax, allows object-oriented programming, allows imperative programming, is suitable for larger system, is suitable for quick hacks, is suitable for web programming, is suitable for creating standalone applications, etc, etc. In short Python is a useful language to know.
This document will give a short overview of Python and show you some example program. If you’re interested in the details I recommend that you visit Pythons website that contains all the details.
If you want a more in-depth tutorial then you can find several on-line, for example:
- Python Tutorial written by the language inventor. Not my favorite tutorial.
- Dive into Python, I haven’t read this.
- An Unofficial Python Tutorial Wiki
There are numerous more, use Google to find more. If you, like me, prefer to read books I can highly recommend Learning Python and if you want specific code example you should look at the Python Cookbook or the website with the same name (much of the material in the book comes from this website).
Example Python program
Here is a simple program that just print out a few numbers
for value in range( nr ):
print value
someSimpleFunction( 5 )
The result of running this program is
1
2
3
4
This simple example shows a number of interesting features:
- There are no type definitions. On line 1 and 2 the program declares two variables nr and bvalue but there is no type defintions.
- Line 1 shows a fuction header, not the there are no curly braces, begin/end etc that indicates where the function starts and ends. Python only uses indentation to indicate the code blocks, in other words: if you doesn’t properly indentate your code Python will give you a syntax error!
- The for-loop in line 2 doesn’t use any indexes like a for-loop in C.
There are of course more to know about Python, see below, but now you have at least seen some Python code.
Running Python
It’s possible to run Python in, at least, three different ways:
- Interactively “within” Python itself
- By launching Python with a Python source code file as an argument
- As a shell script
And then you can of course package your Python program as a standalone application but I will only talk about the first three options.
Interactively running Python
You can use this mode when you’re exploring Python, or trying out a piece of code, or simple as a calculator. It’s not a very efficient way to write a large program. Here is a short example of how to launch Python and run the program above:
2: Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
3: [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
4: Type "help", "copyright", "credits" or "license" for more information.
5: >>> def someSimpleFunction( nr ):
6: ... for value in range( nr ):
7: ... print value
8: ...
9: >>> someSimpleFunction( 5 )
10: 0
11: 1
12: 2
13: 3
14: 4
15: >>>
You can see how I launch Python on line 1 simply by typing python, this launches Python into interactive mode and I can start using it. Line 2-4 are information lines printed by Python, you don’t really have to pay any attention to them but they indicate that I’m currently running version 2.5.1 on a Mac.
The >>> on line 5 is Pythons way of telling you that it’s waiting for you to type something. I typed def someSimpleFunction( nr ): and then return. On line 6 you can now see how Python prints ... , this means that you have written something that isn’t a complete Python statement and it’s now waiting for the rest.
I continue to type the rest of the code, note that I indent the code, and when I come to the end of the function, line 8, I just type return and Python will correctly interpret this as the end of the function. In line 9 Python is once again waiting for me to issue a command, I tell it to run the function with 5 as an argument and on line 10-14 the result is printed.
Running a source file
Assume that I’ve saved the program above into a file called example.py, the extension “.py” is by default used to indicate that the file contains Python code.
0
1
2
3
4
infinitum:Desktop jem$
Running as a shell script
To be able to run the program as a shell script I have to slightly modify the source file and insert the standard “shell script header”.
def someSimpleFunction( nr ):
for value in range( nr ):
print value
someSimpleFunction( 5 )
Now I must sure the the “execution bit” is set and then I can run the script directly
-rw-r--r--@ 1 jem staff 135 Feb 19 14:43 example.py
infinitum:Desktop jem$ chmod u+x example.py
infinitum:Desktop jem$ ls -l
-rwxr--r--@ 1 jem staff 135 Feb 19 14:43 example.py
infinitum:Desktop jem$ ./example.py
0
1
2
3
4
infinitum:Desktop jem$