Operation Manual

Experiments in Python
85
Notes:
Tip...
When you see
ellipsis (…) it
means that
Python is
expecting more
instructions to
follow your
“while”
instruction. Just
press Return
again to clear it.
It is not good practice to put all your code for a block into one line like this.
You can press Return after each instruction here and Python will show you “...” to
indicate that it is expecting more instructions but remember to indent each line in
the block with spaces.
>>> a = 0
>>> while (a < 10000):
... a + =1
... print("Raspberry Pi Users = ", a)
...
Running Python programs
Python programs don’t have to run from IDLE either. You could use any application
to create the text and then run the program using Python directly. You could even
use a word processor but you’ll find that it keeps trying to capitalise words at the
start of each line and correct your grammar. It is not recommended you try to use
a word processor for programming!
Those programs you created before can be edited using the “nano” text editor, for
example. Open up the Linux terminal so you can see a “$” prompt. Open the
program you created earlier – factors.py – in the nano editor, using:
$ nano factors.py
With some trial and error, you should be able to use nano to change the program
to read:
for number in range( 1, 51):
factors = 0
print(number, end=": ")
for divisor in range(1, number+1):
if number%divisor == 0:
print(divisor, end=" ")
factors+=1
if (factors == 2):
print("and is a prime number")
else:
print()
Save it (“Ctrl-O”), close nano (“Ctrl-X”) and then, to run in the terminal window,
type this:
$ python factors.py
You should see the program’s output in the Linux terminal window, as it did in the
IDLE Python Shell.
For the remainder of this Python section, we will use IDLE but, if you prefer, you
can use a different text editor and run your programs from the Linux terminal as
shown here.