While using the interactive mode is fast and fun, real programs contain many lines of code. So, we'll need to use an editor and create Python source code files.
First, let's create a new Desktop folder to contain our files. Right click on the Desktop, select the Create New->Folder
option. Call it 'Python'
and press OK
. We'll use this folder to store all the programs created in this series.
Depending on your version of the Raspberry Pi's Linux operating system comes with a Python-ready editor called Geany. As Geany understands the Python code it makes the task of creating, running and debugging your programs much easier.
(If your Pi doesn't have Geany don't worry, it's easy to install. Just follow these instructions.)
Once a file is saved with a '.py'
extension Geany will colour code the source in the editor. Colour coding not only makes the code easier to read, but will also help you spot typos and errors, such as a missed string quote. The separate left-hand panel shows code symbols, including any variables.
Let's create a simple program. Open the LXDE Desktop menu and select the 'Programming->Geany'
option. In the Geany window select the File->New
menu option. An editor window will appear in a new tab. Now select File->Save
menu option. We'll call this file 'first.py'
and save it in the Desktop's 'Python'
folder we created earlier. To do this you'll need to click on the 'Browse for other folders'
option and select this folder.
Now we've saved an empty file we can type in the program code. It's best to save frequently as you type so you don't lose any changes - if you use the shortcut combination Ctrl-S (control key + 's'
key) it only takes a second.
Here's the code:
import os, platform
print("Welcome to " + platform.system())
print("You are logged in as " + os.getlogin())
print("Your current directory is " + os.getcwd())
print("Running Python version " + platform.python_version())
On the first line we import both the 'os'
and 'platform'
modules, using the comma ','
character to separate each module. The following print statement lines are similar to the ones we used earlier in this series. Adding text string prefix will make the print output more descriptive.
Now it's time to run the program. Save the file then select the Build->Execute
menu option, or press the F5
function key. You should see a new terminal window open and, hopefully, the output of our three print statements.
(Note: Geany can have a configuration issue on some Raspberry Pi distributions. So, if you don't see a terminal window at all, check out the 'Fix Geany' instructions here.)
Did you see a Python error message instead of the printed output?
If the answer is yes the error message should indicate where the problem might be. Carefully check all your typing, especially the string quotes and parenthesis brackets. Fix the problem and run again. Repeat the process until you see the print output. Testing and fixing source code in this manner is called debugging - all programmers spend quite a bit of time debugging their code.
And that's it, your first program. You can open the source file run it again anytime you like, or experiment by adding some Python statements of your own.
A post from my Learn Python on the Raspberry Pi tutorial.
No comments:
Post a Comment