Python Wiki
Advertisement

General Information[]

The Python Interactive Shell is an interactive interpreter that can execute Python commands without saving it to a file.

Starting it[]

The Interactive Shell can be started with the bash-command

python

. If you want to run programs stored in a file on this shell, type in

python -i FILENAME.py

where FILENAME is the name of your file. This is useful for debugging.

Use it as a calculator[]

As explained above, the Interactive Shell can execute Python commands. Same goes for simple calculations. Here are a few examples:

>>> 1 + 3
4
>>> 12 / 4
3
>>> 12 % 4
0

Logical expressions[]

We can do the exact same thing with logical expressions, e.g.:

>>> True and (not False)
True
>>> (False or (not (False and True))) and True
True


File operations[]

As we can open, write and read files by executing a script, we can also do that in this environment.

>>> f = open('test.txt', 'w')
>>> f.write('Hello ')
>>> f.write('World!')
>>> f.close()
>>> g = open('test.txt', 'r')
>>> for entry in g:
...     print(entry)
...
Hello World!
>>> g.close()
>>> import os
>>> os.remove('text.txt')

For further explanations about the usage of files, please look at Files.

Result of the last operation[]

Also, you can get result of the last operation in the shell using "_":

>>> 1 + 1
2
>>> _
2
>>> " ".join(["1", "2", "3"])
'1 2 3'
>>> _
'1 2 3'
Advertisement