Variables¶
Variables are used to store data that may change, such as the number of points you have in a game. Variables can have different data types.
A variable can be thought of as a box that the computer can use to store a value. The value held in that box can change or ‘vary’. All variables are made up of three parts: a name, a type and a value. In the figure below there are three variables of different types:
The variable name contains the string Bob, the variable winner contains the value True and the variable score contains the value 35.
In Python we must give the variables we want to use a name and once we have done that we can start to use them, assigning and manipulating values.
Let’s look at an example using arithmetic operators. Imagine that you want to convert the temperature you read from the microbit in Celsius to Fahrenheit, you could use code like this
celsiusTemp = temperature()
fahrenheitTemp = celsiusTemp * 9 / 5 + 32
Assigning number variables¶
Set variables to their initial values at the start of a program using the = sign:
>>> count = 0
Changing number variables¶
You can modify number variables by adding, subtracting, multiplying and dividing:
increment¶
score = 2
score += 5
display.scroll(score)
decrement¶
score = 7
score -= 5
display.scroll(score)
multiply¶
score = 2
score *= 5
display.scroll(score)
divide¶
score = 10
score /= 2
display.scroll(score)
Assigning strings¶
A group of words or symbols is called a string. Put quotation marks around the contents of a string:
name = 'micro:bit'
display.scroll(name)
You can use single ' or double quotation marks " around strings, but you should be consistent. We use single quotation marks in examples because they are easier to type and double quotation marks can be confused for two single quotation marks.
Boolean variables¶
Boolean variables only have two values: True or False. They can be useful for adding control to your program:
# Imports go at the top
from microbit import *
running = False
while True:
if button_a.was_pressed():
running = not running
if running:
display.show(1)
else:
display.show(0)
This example uses a Boolean variable running to create a toggle button. The program has two states: ‘running’ (showing 1 on the display) and ‘not running’ (showing 0). Pressing button A toggles the state of the running variable between False and True every time you press it.
not is a logical operator that flips the state of the variable. If its value was True, it becomes False. If it was False, it becomes True.
Note
True and False must start with capital letters.
Variable names¶
- should describe what they contain so your code is easier to understand
- must start with a letter, not a number
- cannot contain any spaces
- can’t be the same as Python keywords like
True,iforwhile - are case-sensitive:
scorewould be a different variable toScore
Exercises¶
- Try to output a variable through
display.show()by assigning a different number to the variablenumberandNumber. For example 20 fornumberand 10 forNumber. - Try to use increment these variables by incrementing them by 10 and show the result on the LED screen.
