Getting Started With Python on the Raspberry Pi

Posted by: on Sep 15, 2013 | 4 Comments

raspberry-piPython is a simple programming tool available on Raspberry Pi. We take you through an easy example that takes input, displays it, and does a check on one of the inputs given during the running of the program.

Login to the RPi (either locally or via SSH – it does not matter for the tutorial) and then enter:

This will open a text editor called “nano” and create a new python file called “example.py”. All python scripts end with .py as this tells the system how to handle the contents.

We are going to enter a simple script, here it is completed:

Lets go through what each line means and is doing

Line 1 : this tells the RPi to run this file as a python script and where python is installed (this is standard and rarely changes) so you can just type this line in.
Line 2 : this imports a python package called sys – this contains some common scripts that means we do not have to write them manually

Now we get to the main part of the script, with Lines 4-6 we are setting up a re-usable function called “prompt” that we will call later in the script. Functions are useful as you can call them over and over again without re-writing the code. So knowing functions can be vary useful. Lets go into each line:

Line 4: This is setting up the system to use a standard function called “prompt” this is a function that will ask for some input when the script is run.
Line 5: With this line we taking the response of the user running the script and stripping out any strange responses (Like carriage returns etc.)
Line 6: We are returning the result of the function

Line 8: Here we are storing a list of strings that the script will use later – they could ask anything you wish, but in our code we are asking the users name, school and what school year they are in.

Line 10:  Here we are preparing the script to set a list (or array) called “Answers”
Line 11: This line will start a loop  that will loop around for each question that we stored in the list called “fields” back on line 8. We are saying for each single question in the array of questions do the following

In python when you are doing things in a loop as here, you indent your code with 4 spaces (not a tab!). Python will run anything indented in the loop

Line 12: Print to the screen the question (first loop will ask your name, second loop your school and third loop your school year)
Line 13: Wait for the user to type in their answer and temporally store it in a string called “v”
Line 14: Put the temporary answer V into the next slot of the string list (array). First loop will store it as answers[0], second loop answers[1] and so on until all answers have been stored.

Ok lets recap what we have done so far. We have created a new script file in nano and set up the script to ask the user some questions. These questions are stored in an array (or string list) so that we can respond with the answers later. The questions and thus what responses you get are easy to change. But currently we are storing all answers as strings. Later on we will be converting one of the strings into a number (or integer).

Lets carry on to the end of the script

Line 16: We start a multi line command that will print some text to the screen. Since it is multi line we need to use three quote marks (“). The %s will be replaced by one of the answers we placed into the string list
Line 17: We print a second line to the screen – again the %s will be replaced.
Line 18: We print another line to the screen, the three ending quotes (“) tell the script we have finished wring the text to the screen. the % is telling python that we are now specifying what to replace the three %s’s with. You can see we are calling the string list answers. The number in the square brackets [0], tells it what string the print. It will read it in order so answers[0] will print the users name after Hello.

So now you can get some input from the user and print it to the screen. But you can do more with the response. You can use it to decide on what to print as I will show below

Line 20: Here we are taking the response of the school year and turning it into an integer so we can use it later. We are calling this integer “year”.

Line 22: We are stating a set of checks on the integer stored in year, we are asking the script to check if “9″ was entered. if it was it will go onto line 23. If not it will move to line 24.
Line 23: Since the user said they’re in year 9, print a statement to the screen “hope your GCSE’s are going well.” You will notice that GCSE’s has a backwards slash “GCSE\’s) before the apostrophe. This is called an escape code and it is required as we are using a character that could be code or text. We are telling the script it is text.
Line 24 and Line 25: Since the user said they’re in 8 we print a different statement to the screen. elif is code for Else If. So since the script did not find the year as 9, check to see if they said 8.
Line 26: If they entered something else in the year just say thank you for playing.

Now you have typed in the code press the following

ctrl+0 (that O not zero)

That will save the contents of the screen into the file. Now press

ctrl+x

That will close the nano text editor

Now your ready to run your code, so enter:

and the script will run!

If you’re still looking into picking up a Raspberry Pi, check out the options currently available on Amazon.

Vincent is an IT Consultant who has been passionate about technology since lower school. He likes to pass this passion on to others through his posts.

Facebook Twitter LinkedIn Google+ 

4 Comments

  1. Chad F
    7 October, 2013

    Thanks for all the great information! I’m looking to get started with some basic programming, but haven’t decided on which machine to use.

    Reply
    • Vincent Willcox
      12 November, 2013

      I started on the commodore 64 back in the mid 80′s (I think around the age of 6-7). Compared to that, the Raspberry Pi is a dream to use.

      I love the open nature of the RPi. You can program in a lot more then just Python. What sort of thing are you looking to write? I am happy to give pointers!

      Reply
  2. Daniel V
    24 April, 2014

    thanks that help a lot.
    but just a question how do i re open it so that i can edit the programe

    and i am gana be using te raspberry to colect data from a omron plc and upload it to a web server

    Reply
    • Daniel V
      24 April, 2014

      … it seems you just need to run the nano agian ….

      Reply

Tell us what you think!

x
The ClassThink Guide to Managing iPad in Education
Available on iTunes Soon!

If you'd like us to let you know when our guide is available, pop your email below: