- Account
- My Orders & Cart
- Lists
- myLists
- Quote Manager
- Account
- myDIGIKEY
- My Orders & Cart
- Lists
- myLists
- Quote Manager
- Logout
Digi-Key customers in the United States can select from a range of delivery options, including Ground shipping at $6.99 and 2-Day at $12.99
Credit account for qualified institutions and businesses
Payment in Advance by Wire Transfer
More Products From Fully Authorized Partners
Average Time to Ship 1-3 Days. Please see product page, cart, and checkout for actual ship speed.
Extra Ship Charges May Apply
For more information visit Help & Support
One of the biggest selling points for Raspberry Pi is adaptability, thanks to its extensive I/O options. In this tutorial, we will learn how to get started with Python on the Raspberry Pi 3 Model B.
Python is a programming language that has recently become very popular—so popular, in fact, that it is now the fourth most popular language (according to the TIOBE index). But why has Python become so popular? What makes it an ideal language for the Raspberry Pi?
Unlike other languages (C, C++, etc.), Python is an interpreted language, which means when a Python program is executed, an interpreter reads the Python instructions and then performs the desired action. The interpreter itself is written in CPU native instructions, but the Python program is not. This means Python programs can, in theory, run on ANY computer, so long as that computer has a Python interpreter. This makes Python a cross-platform language, which is one of the main reasons why it has become so popular. A program written on Windows does not need to be rewritten to work on a Mac or a Linux machine. It’s also incredibly simple, easy to read, and powerful. It can write advanced programs, including graphical interfaces, networking, parallelism, and much more. Python is a productive language, resulting in faster program production and requiring less code and time.
Since Linux has been written for the Pi (ARM core), the Python interpreter can run on it and, therefore, so can Python programs. So, how do we start with this wonderful programming language on the Pi?
Raspbian, the default OS choice for the Raspberry Pi, should contain both Python 2 and 3, so loading Python should be easy to navigate through menu options. Firstly, click the top left Pi icon on the menu bar. Then navigate to “Programming” and click “Python 3 (IDEL)”.
Loading Python 3 IDE
If you do not have Python on your system, you will need to install it. To do this, open a terminal window and enter the following command:
sudo apt-get install python3
The terminal icon
When Python 3 loads, the visible window is the shell, which has two main purposes:
The main shell
The IDE also has a menu bar with many options, including file operations, editing, shell specific options (“Interrupt program” for example), program debugging, IDE options, and “Help”. For now, we will only concern ourselves with creating a new file, which is done by clicking File > New File.
Creating a new file
Once the new window loads, we can finally enter our first Python program. To do this, enter the code as shown below into the window. Then, save the file.
# A Basic Python Program
# Declare some variables first
name = ""
age = 0
currentYear = 0
yearBorn = 0
# ----------------------------------
# Start by getting the users details
name = input("Enter your name: ")
age = int(input("Enter your max age this year: "))
currentYear = int(input("What is the current year? : "))
# ----------------------------------
# Perform a basic calculation
yearBorn = currentYear - age
# ----------------------------------
# Print the result
print("You were born in the year " + str(yearBorn))
With the code entered and the file saved, it’s time to run the program. Running a Python program can be done in one of three ways: press F5 in the window with the program to run, go to the menu bar and click Run > Python Shell, or run the file via a terminal window as an argument for Python. For now, the easiest way is to simply press F5 in the window with the code. Once pressed, the code should return no errors, and the shell window should prompt for data.
The executed program with user data filled in
Getting Python programs to run on the Raspberry Pi is incredibly easy with an OS like Raspbian, which is designed for programmers of all kinds. Another major advantage of using Python on the Pi is that you can write the program on a different computer (such as Windows) and then transfer the program to the Pi when it’s ready to run. Just remember, however, that some code in Python is unique to the Pi—only the Pi has GPIO!
Have questions or comments? Continue the conversation on TechForum, Digi-Key's online community and technical resource.
Visit TechForumCo-Browse
By using the Co-Browse feature, you are agreeing to allow a support representative from Digi-Key to view your browser remotely. When the Co-Browse window opens, give the session ID that is located in the toolbar to the representative.
Digi-Key respects your right to privacy. For more information please see our Privacy Notice and Cookie Notice.
Yes, Continue to Co-BrowseGet fast and accurate answers from Digi-Key's Technicians and Experienced Engineers on our TechForum.
Please visit the Help & Support area of our website to find information regarding ordering, shipping, delivery and more.
Registered users can track orders from their account dropdown, or click here. *Order Status may take 12 hours to update after initial order is placed.
Users can begin the returns process by starting with our Returns Page.
Quotes can be created by registered users in myLists.
Visit the Registration Page and enter the required information. You will receive an email confirmation when your registration is complete.
Digi-Key customers in the United States can select from a range of delivery options, including Ground shipping at $6.99 and 2-Day at $12.99
Credit account for qualified institutions and businesses
Payment in Advance by Wire Transfer
More Products From Fully Authorized Partners
Average Time to Ship 1-3 Days. Please see product page, cart, and checkout for actual ship speed.
Extra Ship Charges May Apply
For more information visit Help & Support
Thank you!
Keep an eye on your inbox for news and updates from Digi-Key!
Please enter an email address
CA Privacy Rights: Do Not Sell My Personal Information