Hello World - Starting Python
Let's make a new directory for our RVR+/RVR projects
If you ever want to pull the latest SDK updates using git, you'll ideally want to keep your own code separate from the SDK. In this tutorial, we'll accomplish that by putting everything in a new directory outside of the SDK. Enter this command in your terminal to create the directory
# Create a new directory called rvr-projects inside of our home directory
mkdir ~/rvr-projects
Before moving to the next step make sure you are in the new directory
cd ~/rvr-projects
note
About Text Editors and IDEs
A text editor is exactly what it sounds like. It's a program that allows you to edit text files. Python scripts are text files that contain Python code.
An IDE, or Integrated Development Environment, is a program that typically includes text editing features, plus some extra features designed to make programming in particular languages easier. An IDE may provide helpful auto-complete suggestions, debugging features, and much more.
Raspberry Pi OS typically has the following programs preinstalled:
- Mousepad
- A simple graphical text editor, similar to Notepad on Windows.
- Found under Accessories->Text Editor in the Applications Menu
- Nano
- A terminal-based text editor. Since its entire interface is implemented in the terminal, you can use it from another computer over SSH.
- To learn more about using Nano, you can check out this How-To Geek tutorial or the previously mentioned Pac-Man treasure hunt on the terminal from the Raspberry Pi Foundation.
- Geany
- A small lightweight IDE that supports several languages including Python.
- Found under Programming in the Applications Menu.
- Thonny Python IDE
- A more focused IDE specifically for Python development.
- Found under Programming in the Applications Menu.
Getting Started with Python - Hello World
It's traditional to start any programming language with a "Hello World" program. It turns out that in Python, this is really easy.
tip
Nano Text Editor is probably already on your pi, to test this try this commend:
nano --version
If it returns something like this then you are set:
GNU nano, version 3.2
(C) 1999-2011, 2013-2018 Free Software Foundation, Inc.
(C) 2014-2018 the contributors to nano
Email: nano@nano-editor.org Web: https://nano-editor.org/
Now to open Nano you can just type this command:
nano
Open up your text editor or Python IDE of choice, and make a new file. Save it as
hello.py
in~/rvr-projects
.Add the line below (and save):
print("Hello World")
If we want to, we can also add comments to our script. Comments are notes to ourselves (or other programmers reading our programs later). They are ignored by the python interpreter, and begin with the
#
character like this:# This is a comment. It won't do anything functional. # We will use comments throughout this tutorial, but you can # leave them out if you'd like
Now, in your terminal, make sure you are in
~/rvr-projects
. You can tell by looking at your prompt, which should look like this:pi@raspberrypi:~/rvr-projects $
If it doesn't, this will get you there:
cd ~/rvr-projects
In your terminal, enter the following command:
# Invokes the default Python 3.x interpreter to run our script python3 hello.py
It should look like this:
pi@raspberrypi:~/rvr-projects $ python3 hello.py hello world! pi@raspberrypi:~/rvr-projects $