Revision This is an old revision of PywellInterpreter made by KlenwellAdmin on 2012-12-11 11:07:12.
- Use python on the command line
- Run a script
- Introduce the sys module
- Introduce some of the idiosyncracies of python
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()
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 Make a copy of the `hello.py` script named `add.py` and modity it add a series of numbers from the command line like so:
python /tmp/add.py 1 2 3
- Evidently the `^` character is not used for exponentiation. What does it do?
- Modify your to interactively prompt the user (you) for a name to say hello to. (See the x function).
* Note documentation version may differ from version of python you're using.
Next Lessons: PywellModules Modules, PywellExceptions Exceptions