Wikijunior:Raspberry Pi/Introduction to Python

Python is a general-purpose programming language that is easy to learn and is used in nearly all of the tutorials in this wikibook.

Getting started


Python 3 and IDLE are pre-installed on Raspberry Pi OS. IDLE can be opened from the Programming menu.

If you don't have a Raspberry Pi, you can install a copy of Python 3 on just about any computer from the official website.

Using the IDLE Shell
IDLE (Integrated Development and Learning Environment) is included with Python 3 and is easy to use. It is an Integrated Development Environment (IDE) which is a type of software that is designed to make it easy and convenient to write source code.

The first window that opens is the IDLE Shell, which is the Python 3 shell. You can run a quick line of Python in the shell, for example Python is a calculator so let's try adding :

Feel free to play with Python's calculator and try using the different operators:


 * Plus, such as
 * Minus, such as
 * Multiply, such as
 * Divide, such as
 * Square, such as
 * Modulo (gives the remainder from a division), such as

You can also access the Python 3 shell by typing  into the  Terminal program and pressing the Enter key.

Hello World
Whenever a programmer starts with a new programming language. The first thing to do is to output the phrase "Hello World".

Open IDLE, select File, then New File (Ctrl+N) (or press the and  keys together), create a new file and name it , then type the following:

Run the code in IDLE by selecting Run, then Run Module (F5) (or press the key) and it should display in the Python shell as:

is a built-in function that outputs text onto the screen. As it's a word that has already be defined by Python to do something, programmers call it a "keyword".

Comments
Sometimes you may need to make notes in your code or remove a line to troubleshoot why it is not working (which is referred to as debugging).

In Python, you can add a comment using the hash symbol  (in the US, this is confusingly called a "pound sign"). Let's add a comment to our Hello World program from earlier:

Once the hash symbol has been typed, the Python interpreter will ignore the rest of the line which allows us to make notes.

Variables


Variables are a container that is used to store values that we can define in our program.

In Python, variables are assigned with a name, a variable assignment operator  and a value. Some examples of values are:

Booleans
True or false such as  or.

Integers
Positive or negative whole numbers such as  or.

Floating-point numbers
Positive or negative numbers that use a decimal point such as  or.

Strings
A series of letters, numbers or symbols such as. In Python, either single quotes  or double quotes   can be used to define a string.

Lists
A list is an ordered collection of values each separated by a comma  of any of the values above. Any Python 3 datatype can be used.

Python 3 has a built-in functions for modifiying lists. For example, let's try combining the two lists together:

Dictionaries
A dictionary is like a list but for each entry there are two values. Here's an example:

Other datatypes
These are just a few examples of the variable types that are available in Python. There are many other types as well including complex numbers, tuples, sets and more.

If statements


The  statement is used to make parts of the source code run providing a certain condition is met. For example, let's make a variable called "x" and set it to 5.

The keyword  is used for defining what the source code should do if the condition   is not met.

What about if statements that have more than just two conditions? We use the keyword  (which just means "else if"). In the example below, we use the less than  and greater than   symbols to determine if a number is positive, negative or zero.

Try running this example in IDLE and change the value of  to get all 3 outputs:

Loops
Sometimes you will want the code to repeat which can be done using loops. There are 2 kinds of loops used in Python:

For loops
For loops are useful when you know how many times the code needs to repeat itself.

In this example, there is a list of numbers from 1 to 5. The square brackets  are used as lists are a specific variable datatype that is used to store multiple values.

While loops
A while loop repeats endlessly until the condition is met. For example, printing the numbers from 1 to 10:

Modules
Modules are Python source code written by other software developers. By default, Python can't do very much but we can make our Python programs do more by importing a module.

You can use the  keyword to add a module. Use the  to be more specific if you just want to do one thing from that module.

For example, let's print the number Pi. The number Pi is often shortened to, but it actually continues on forever in real-life (mathematicians call this an irrational number). In Python,  prints Pi to 15 decimal places.

Now we have Pi, we can create a simple program that calculates the area of a circle. The formula for this is: area = pi × radius2


 * Circumference – the outside line of a circle
 * Radius – a straight line that connects the center of the circle to the circumference

The single asterisk  is used to multiply one number by another (just like the multiplication sign  ). The double asterisk  with a   is used to square a number such as: 22 = 4

If you want to go a step further, you can try to find the length of the circle's circumference. The formula is: circumference = pi × diameter


 * Diameter – a straight line that goes through the centre of the circle from one side of the circumference to the other. This line is double the length of the radius.
 * Pi – the ratio of the length of the circumference to the diameter. This means that the circumference of every circle is about 3.14 times as long as the diameter. It is named after the Greek letter Pi (π).

General

 * Bug – An unwanted mistake in the source code.
 * Comment – A line of source code that is ignored by the interpreter/compiler, so it can be used for making notes or debugging.
 * Debugging – Removing bugs from the source code. Most IDEs have tools that allow debugging at runtime to make it easier to see where the bugs are.
 * Hello World – A popular test program used by software developers when they are learning a new programming language.
 * If statement – A conditional section of source code that runs certain source code based on the condition that was used.
 * Indentation – The use of tabs (with the key) or spaces (with the ) before a line of source code. In Python, this is required for the interpreter to understand how the program works.
 * Integrated Development Environment (IDE) – A program designed for software development. A typical IDE includes a text editor to write the source code, an interpreter (or compiler) for running the sourcing code and debugging tools to troubleshoot mistakes made when writing the source code.
 * Interpreter – A program that reads and runs the source code of an interpreted programming language such as Python.
 * Keyword – A word that is reserved for use by a programming language. Examples in Python include:,  ,  ,  ,   and.
 * Source code – The text of the programming language that makes up the program.

Loops

 * For loop – A repeated section of source code that runs a certain number of times.
 * While loop – A repeated section of source code that is exited by a condition.

Variables

 * Boolean – A variable datatype that can either be True or False.
 * Datatype – The type of data which a variable holds. Python is a dynamically typed programming language, so you can just assign the value to the variable.
 * Integer – A variable datatype for any positive or negative whole number (including zero) without a decimal point.
 * String – A variable datatype that defines a string of characters such as.
 * Variable – A value with a particular datatype that is defined by the programmer usually at the top of a program.

Modules

 * Module – A Python source code file written by other software developers. Many modules are included with the default Python installation, but there are also external Python modules which need to be installed using pip or Anaconda.
 * pip – A command-line Python package manager for installing external modules that aren't included in the standard Python installation.