3. Python Variables
3.1 What is a Program?
A program is a set of instructions that a computer can execute to perform a specific task. In Python, a program is typically written in a text file with a .py extension. When you run the program, the Python interpreter reads the code and executes it line by line. Generally, a program requires input, processing, and output. The input is the data that the program will work with, the processing is the logic and calculations performed on that data, and the output is the result of the program's execution.

3.2 Variables
In Python, variables are used to store data values. A variable is essentially a name that references a specific piece of data (like a number, string, or list). You can assign a value to a variable using the assignment operator =, and then use that variable later in your code.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
In the above code:
- Line 7 - Sets the distance type to cm
- Line 12 - Sets a variable named distance to the value of
10 - Line 13 - Moves the Edison forwards at
speed 5,10cm
Notice in line 13 that the Edison moves a distance of 10cm because the length variable was created a set to 10 previously.
3.2.1 Settings values to variables
Setting values to variables is a fundamental part of programming in Python. You use the assignment operator = to give a variable a value. Recalling the value only requires you to use the name of the variable.
You can set multiple variables and use them to set up other variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
In the above we now have three variables. length_1 and length_2 are set to 10 and 5 respectively. length_3 is the sum of length_1 and length_2.
Based off line 14, how far will the Edison travel?
Variables can be called multiple times:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
This time the Edison moves three separate times. Notice that we make the Edison wait in between each move.
How long is the Edison waiting after each move?
We can make the Edison do a little dance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
There are 2 things to note:
We've changed the names of the three "length" variables to left_spin, right_spin and wait_time. It's important and useful to name variables with names that are descriptive.
Line 16 spins the Edison to the left but uses the right_spin variable. The reverse is true of line 18. Why does this work? What will actually happen?