In this tutorial you’ll write your first Python program on your Raspberry Pi. After completed this tutorial, you will know how to startup a Python IDE (integrated development environment) and how to write and run a simple Python program.
What you’ll need for this tutorial
For this tutorial, you need to have your Raspberry Pi running on the latest version of Raspbian. This version includes “Thonny”. We’ll use this user-friendly IDE to write our first Python program on our Raspberry Pi.
Open the IDE
You can find Thonny in the applications menu as shown in the figure below. To write your first program on the Pi, the “Simple Mode” version will do an excellent job. If you don’t find “Tonny” between your applications, then you probably do not have the latest version of Raspbian. Consult our tutorial “Update your Raspberry Pi to the latest version” before continuing.
Write the code
Instead of the classic “Hello world” program, we’ll make it a little less simple and go for a program asking to enter your name. The name will be stored as a variable and returned onto the screen.
Write or paste following code in the IDE:
person = input('Enter your name: ') print('Hello', person)
Save and run the program
Once finished, you can run the code by clicking on the Run button. But before running the program you’ll have to give it a name and save it. In our case we’ll store the file in the “Documents” folder and name it “hello_you.py” . Python files have “py” as extension. Here, you don’t have to specify the extension.
Now the program should ask for your name on the bottom part of the window (Shell). After entering it, it will respond by “Hello ” and your name. Have a look at the figure on the top of the page to see the result in my case.
- On the right part of the window (Variables), you can see the value(s) of the variable(s)
- You can easily amend the program and run it again as many times you want
Run the program from the terminal window
It’s possible to run the program from the terminal window. To do this, open a terminal window by clicking on the terminal icon on the top of your screen.
On the command line, we have to move first to the folder where the Python file has been stored, “Documents” in our case. To do this enter:
And to run the program we’ll enter the command “python3”, followed by a space and the filename. In our case, it’s:
Normally, the program should run and ask for your name. After entering it the program will respond (as shown in the figure above).
Congratulation! You made your first python program on the Raspberry Pi. Have lots of fun with your next ones!