In this post, I’ll give a quick overview of what a Python program is, what they can be used for, and finally, how to write and run a simple program on the Raspberry Pi.
What is a Python program?
The Python programming language actually started as a scripting language for Linux. Python programs are similar to shell scripts in that the files contain a series of commands that the computer executes from top to bottom. Python is a very useful and versatile high level programming language, with easy to read syntax that allows programmers to use fewer lines of code than would be possible in languages such as assembly, C, or Java.
Compare a “hello world” program written in C vs. the same program written in Python:
Python programs don’t need to be compiled before running them, as you do with C programs. However, you will need to install the Python interpreter on your computer to run them. The interpreter is the program that reads the Python file and executes the code. There are programs like Py2exe or Pyinstaller that can package Python code into stand-alone executable programs so you can run Python programs on computers without the Python interpreter installed.
What can a Python program do?
Like shell scripts, Python can automate tasks like batch renaming and moving large amounts of files. Using IDLE, Python’s REPL (read, eval, print, loop) function can be used just like a command line. However, there are more useful things you can create with Python. Programmers use Python to create things like:
- Web applications
- Desktop applications and utilities
- Special GUIs
- Small databases
- 2D games
Python also has a large collection of libraries, which speeds up the development process. There are libraries for everything you can think of – game programming, rendering graphics, GUI interfaces, web frameworks, and scientific computing.
Many (but not all) of the things you can do in C can be done in Python. Computations are slower in Python than in C, but its ease of use makes Python an ideal language for prototyping programs and applications that aren’t computationally intensive.
How to create and run a program in Python
We will only cover the basics of writing and executing a Python program here, but for a great tutorial covering everything a programmer needs to know about Python, you might want to check out the book Learning Python 5th Ed. (O’Reilly) by Mark Lutz.
Python 2 and Python 3 come pre-installed on Raspbian, but to install it on another Linux OS or to update it, simply run sudo apt-get install python3 or sudo apt-get install python. To access Python from the command prompt, type “python” or “python3” depending on which version you want to use. This opens up the Python REPL (read-eval-print-loop), from which you can enter Python commands just like you use the command line. Use Ctrl-D to exit the REPL:
Our first program in Python will be the standard “hello world” program. To begin, enter sudo nano hello-world.py at the command prompt to open the nano text editor and create a new file named hello-world.py.
Enter this code into nano, then press Ctrl-X to exit and save the file:
#!/usr/bin/python print "Hello, World!";
All Python program files will need to be saved as “.py” file types. You can write the program in a text editor such as notepad or notepad++ as long as you save the file with a “.py” extension.
To run the program without making it executable, navigate to the location where you saved your file, and type python hello-world.py.
We can make the hello-world.py file executable by entering chmod +x hello-world.py at the command prompt. Now, all we need to do to run the program is enter: ./hello-world.py.
Here are some additional resources that will help you make the most out of programming in Python:
- Complete list of Python libraries
- Complete list of Python syntax
- The Python Package Index (PyPi)
- Installing Python packages on the Raspberry Pi
Hopefully you found this post useful. If you have any questions, feel free to leave a comment below. Also, if you know someone who would enjoy this article, please share it! You can also get updates when ever we post a new article by entering your email address in the subscribe box below…
Need an easy-to-use way to design circuits and layout PCBs?
Try EasyEDA, a free circuit design software that also offers low cost, high quality PCB manufacturing.