Python Fundamentals Tutorial: Getting Started

ProTech's Python Fundamentals Tutorial

Robert Zuber

1. Getting Started

1.1. The Interactive Interpreter

The Python installation includes an interactive interpreter that you can use to execute code as you type it. This is a great tool to use to try small samples and see the result immediately without having to manage output or print statements.

1.2. Lab

If you have not done it yet, download the lab files at the following URL:

(Linux / Mac) Open a terminal and type python. (Windows) Open the Python IDLE IDE from the Start menu.

  1. What is the version number?
  2. Type help(str) This is a simple way to get documentation for a builtin or standard library function. You can also use the online HTML documentation.
>>> help(str)
Help on class str in module __builtin__:

class str(basestring)
 |  str(object) -> string
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.
 |  Method resolution order:
 |      str
 |      basestring
 |      object
 |  Methods defined here:
 |  __add__(...)
 |      x.__add__(y) <==> x+y
  1. Note the use of methods with names with two underscores at the beginning and end of the name. These are methods that you will generally never call directly. How do you think the __add__() method gets executed?
  2. Now try typing the following commands to see the output. Note that you don’t assign a result, you get that result in the interpreter.
>>> 'hello world'
'hello world'
>>> _ + '!'
'hello world!'
>>> hw = _
>>> hw
'hello world!'

In the interactive interpreter, you can use the special variable "_" to refer to the result of the last statement. Handy in this mode, but meaningless in scripts.


Throughout the rest of this courseware, the ">>>" in a listing indicates that the code is being executed in the interactive interpreter.

1.3. Lab

Enter the following into a new file "" in your text editor of choice.

print 'hello world!'

Save and exit, then execute the script as shown below.

$ python
hello world

On unix, you can also use shebang (#!) notation on the first line.