Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
The third way is to enter the Python statement exit (). When you do this,
you get a pop-up window titled Kill? that says The program is still
running! Do you want to kill it? and then you can press the
OK button. This last option is a little violent, but it will get you out of IDLE
and back to the LXDE GUI.
Now that you’ve played with IDLE a bit, its basic features should be more
useful to you. As your experience with Python grows, you might want to try out
some of the IDLE power-user features as well.

By the Way: More IDLE, Please
The official Python website maintains an IDLE document that is worth
exploring for more info on using IDLE. You can find it at
docs.python.org/3/library/idle.html.

Creating Python Scripts


Instead of typing in each Python statement every time you need to run a program, you can create whole
files of Python statements and then run them. These whole files of Python statements are called Python
scripts.


You can run Python scripts from either the Python interactive shell or from IDLE. Listing 3.3 shows a
file called sample.py that contains two Python statements.


LISTING 3.3 The sample.py Python Script


Click here to view code image


pi@raspberrypi ~ $ cat py3prog/sample.py
print ("Here is a sample python script.")
print ("Here is the second line of the sample script.")
pi@raspberrypi ~ $

By the Way: Where Is My samply.py?
You will not find this script, py3prog/sample.py, on your Rapsberry Pi. It was created
for this book. Later in this chapter, you will be learning how to create your very own
Python scripts.

Running a Python Script in the Interactive Shell


To run the sample.py script in the Python interactive shell, at the command line, type python3
py3prog/sample.py and press Enter. Listing 3.4 shows the results you should get. As you can
see, the shell runs the two Python statements without any problems.


LISTING 3.4 The Execution of sample.py

Free download pdf