Posted on Leave a comment

How to write your first Python program on the Raspberry Pi

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.

  1. 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.

  2. 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.

    Raspberry Pi Thonny

  3. 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)

  4. 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.

    Raspberry Pi Thonny python hello you save

  5. 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.

    Raspberry Pi Thonny python hello you
    Remark
    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.

  6. 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:

    cd Documents

    And to run the program we’ll enter the command “python3”, followed by a space and the filename. In our case, it’s:

    python3 hello_you.py

    Raspberry Pi terminal hello you
    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!

And by the way, if you are interested in learning to program electronic components on your Raspberry Pi, visit our shop. We have a nice kit which contains all the things you need to start.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter

freva.com respects your privacy. Read our privacy policy on how we handle your personal information.