1. Your first Python3 program
The Raspberry Pi comes with all the tools you need to start programming in Python. This tutorial will use Idle 3 which is the Integrated Development Environment ( IDE ) for Python 3.
To get going, launch Idle3 from the desktop icon or from the Programming menu.
This is the Python 3 Shell of yours. You could type your Python code directly into this and run it line by line while you typed it. It will become unmanageable though when you start writing larger programs, so let's create a python file that we can write our code to and then execute when we want to. To do this, press File->New Window, and then you will see another window open to allow text to be entered.
We now have an environment in which we can write code and execute it, so let's get to writing the first program!
Your First Program
There is a very long lasting tradition with programming that your first program should be a simple "Hello World!" program which simply prints "Hello World!" to the screen, so let's give it a go...
Type the following code in your text window:
Now press File->Save As and save it as a "1.py" tutorial because we need to save it before we can run it.
When you've saved it, press Run->Run Module. If you typed the code right, you will see Hello World! Printed in the other window of your Python Shell. Congratulations, this is the very first computer program!
So, what's Syntax? Python expects the code to be in a particular format for him to understand. Go to your Hello World program and delete the redundant quotes so it looks like the following:
Now save and try to run your software again by pressing Run->Run Module and see what happens.
This is what is known as a Syntax Error. This is because Python requires those quotes to be there so it knows that the "Hello World!" is text to be printed to the screen and should not be interpreted as other code. Not all errors will appear with a Syntax Error box as you may have other syntax errors that Python may not know are syntax errors and will give you an error in a different form.
Consider any of the following combinations to see which one is working and which one is not:
print( "Hello World!" )
print "Hello World!"
print ("Hello World!")
Now that you've written and executed your first program we can go onto the next part and start writing bigger programs that interact with the user.
2. Using variables in Python
If you have gotten so far then you will have written your first program which outputs "Hello World " to the screen. While outputting is a useful feature of programs, it doesn't make for a very interesting one as programs normally handle various bits of data and interact with users. This tutorial will teach you how you can use variables (areas of memory) to store bits of information, change them and print the results out to screen.
Open Idle3 like you did for the first tutorial and set it up for a new program. Save this python file as tutorial2.py this time.
Before we start using areas of memory, lets have a look at how Python can handle some basic calculations for us and how we can use the print function we saw in our first program to output the answer. Type the following into your idle editor window, save and run:
You should then see "8" printed to the shell window. Notice how this print statement didn't need the double quotes. This is because we want Python to read the 5+3 as code and not text as this will make it do the calculation, give us our answer and pass the answer to the print statement. Have a play with the figures and also the type of calculation (* / -) to see what happens.
This is great but the numbers are what we call hard coded, which means they exist in our code as 5 and 3 and will never change. This doesn't make for a very dynamic program, although hard coded values can be useful at times. What we want to do is store the values in memory so we can change them and use them throughout our program. To do this we use "variables" which are areas of memory we can use to store various values like numbers, text and images. Let's look at the following code:
number1 = 5
number2 = 3
print(number1 + number2)
This code will store the value 5 inside a variable called "number1" and the value 3 inside a variable called "number2" It helps you to view values in the program using the names number1 and number2, which is exactly what the print statement above is doing.
We could calculate the total before the print statement and assign it to a variable, and then only print the variable as follows:
total = 5
total = total + 3
This code assigns 5 to the "total" variable. It then assigns a total of 5 at the time, adds 3 which changes to a total of 8 and then prints it out.
You can also assign strings (text) to variables:
my_string = "This is some text"
Let's take a look at how we can read the meaning from the user and print it out. Please take the following code:
message = "You typed in: "
print("Please type something: ")
input_line = sys.stdin.readline()
print(message + input_line)
Now, we have a couple of new parts here. The first line has an import which imports "sys". Sys is a library which contains more useful code that is commonly used by people and comes as standard with Python. Specifically we are using it on the line that reads
input_line = sys.stdin.readline().
sys.stdin.readline() part reads in what the user types until the point they press return. It then assigns that value to the variable "input_line".
We then come to the final print message. This uses two variables and prints them together. The + symbol means that python will add the two string together, i.e. the message and the input_line. Run the code and you will see the output.
Printing Numbers and Strings
The previous example works since python treats the message variable and the user input as strings. The print statement must have the same value types when printing the output. For example , the following example will not work:
message = "Your age is "
number = 20
print(message + number)
You'll notice a error about not being able to convert an
str. This is because message is a string and print then expects the number to be a string, but as it is an integer there is a mismatch and it will throw the error you see. To overcome this we can tell python to treat the number as a string by using the following:
print(message + str(number))
You will have the opposite problem if you try and add a number to a number that the user types in as the
sys.stdin.readline() function reads in everything as a string. Luckily you can do conversions the other way using the following:
input_num = int(sys.stdin.readline())
input_num = input_num + 10
We wrap the
sys.stdin.readline() call using the
int() function which converts the input read in from the user to an integer meaning we can happily use it as a number.
From what you've learned, see if you can read a number from the user, multiply it by 2 and then print it back to the screen with a little message.