In this tutorial you’ll write your first Python program on your Raspberry Pi. After having completed this tutorial, you will know how to startup a Python IDE (Integrated Development Environment) and how to write and run a simple Python script.
- Prepare your Raspberry Pi
For this tutorial, you need to have your Raspberry Pi running on the latest version of Raspberry Pi OS. This version includes “Thonny”. We’ll use this user-friendly IDE to write our first Python program on our Raspberry Pi.
- Open Thonny Python IDE
You can find Thonny in the applications menu (left upper corner of your screen) as shown in the figure below. If you don’t find “Tonny” between your applications, then you probably don’t have the latest version of Raspbery Pi OS. Consult our tutorial “How to 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: ')
- Save your program
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.
- Run your script in the Thonny IDE
Now you can run the program. After clicking on the “Run” button, it should ask for your name on the bottom part of the window (Shell). After entering it, it will respond by “Hello ” and your name.
Click on the “Debug” button to open the Variables window (on the right part of the IDE). There you can observe the value(s) of the variable(s). Have a look at the image on top of the post to see the Variables window in action.
- Run the Python script from the terminal window
It’s also possible to run the program from the terminal window. To do this, open a terminal window by clicking on the terminal icon (left upper corner 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!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?