klenwell information services : PywellScripts

Revision [2338]

This is an old revision of PywellScripts made by KlenwellAdmin on 2012-12-14 15:10:19.

Pywell Lesson: Interpreter



Interactive Interpreter

To learn python you obviously need to be able to run python. On linux, it comes pre-installed, so if you're using a linux os such as Ubuntu, as this tutorial assumes, you should be ready to get started.

One of my favorites features of python is the interactive interpreter. It allows you to easily play with python. Try it for yourself. Open a terminal and type python like so:


The interpreter will tell you what version it's running and give you a prompt for input:

Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.

At the prompt, try typing the following:

>>> print 'hello world'
>>> sum(range(5))

Input a valid command and you'll usually get some kind of output in return.

My favorite thing about the interactive interpreter is it allows you to quickly answer questions about the language without having to waste time looking up stuff online or -- worse yet, like I used to have to do before the internet -- in books! For instance, how do you do exponentiation in Python: 3^2 or 3**2?

>>> 3^2
>>> 3**2

One very useful builtin python function is dir. It will give you a list of all the attributes and methods attached to an object. See the references section below for other builtin python functions. To learn a little about strings in python, try this:

>>> name = "guido"
>>> dir(name)
>>> name.capitalize()


Here's a simple Python script:

#!/usr/bin/env python

    Pywell Python Tutorial: Scripts
    A basic python script. This is the script's "
docstring". Docstrings are
    a great place to summarize your script or module and include licensing

    Copyright (c) 2012 MIT License: Tom Atwell

# Imports
import sys

# Globals / Constants

# Main
def main():
    args = sys.argv[1:]
    message = "hello %s" % (" ".join(args))
    print message

if __name__ == "__main__":

The script provides a simple template for your python script. It import one module from the python standard library: sys. (More on modules next time.)

Note the if __name__ == "__main__": line at bottom. This is a somewhat idiomatic line that checks to see if the current file is the script being invoked. It says basically, "if this script is called from the command line, run what follows."

Copy and paste that into a text file and save it as hello.py somewhere on your computer -- say your tmp dir. Now your can run it from your terminal by typing python followed by the file path. For example (make sure to adjust path if you used a different one):

python /tmp/hello.py

Now try these:

python /tmp/hello.py world
python /tmp/hello.py planet earth


Make a copy of the hello.py script named add.py and modify it add a series of numbers from the command line like so:

python /tmp/add.py 1 2 3

Extra Credit

  1. Evidently the ^ character is not used for exponentiation. What does it do?
  2. Modify your script to interactively prompt the user (you) to enter a list of numbers to add. (See documentation for raw_input function.)


* Note documentation version may differ from version of python you're using.

Next Lessons: PywellModules Modules, PywellExceptions Exceptions