klenwell information services : PywellInterpreter

Revision [2348]

This is an old revision of PywellInterpreter made by KlenwellAdmin on 2012-12-14 15:44:52.

Pywell Lesson: Interpreter

return to PyWell Tutorial Index



To learn Python you obviously need to be able to run Python. This tutorial assumes you have it installed. 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 simply type python. 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'
>>> range(5)
>>> 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()


Type the following commands from in the Python interpreter:

>>> license
>>> 'version %.1f of %s supports %s-like formatting' % (2, 'python', 'sprintf')
>>> 1 % 2
>>> 2 % 2
>>> 2 / 0
>>> math.pow(5,2)
>>> import math
>>> math.pow(5,2)
>>> import this

Any ideas as to what's happening in each case?

Extra Credit

  1. Evidently the ^ character is not used for exponentiation. What does it do?
  2. Is sprintf-type formatting supported in version 3 of Python?