Lesson ยท 10 min
Variables and numbers
Names for values
A variable is a name that points to a value. You make one with an equals sign. You do not need a special keyword.
city = "Lisbon"
temperature = 18
print(city)
print(temperature)
You can give a variable a new value at any time:
score = 10
score = 12
Python programmers write variable names in lower case, with underscores between words, such as final_score. A name cannot start with a digit or contain spaces.
Kinds of values
Every value has a type.
| Type | Example | What it is |
|---|---|---|
str |
"Mars" |
text |
int |
42 |
a whole number |
float |
3.5 |
a number with decimals |
bool |
True |
True or False (with capital letters) |
The function type(value) tells you the type.
Math
| Operator | What it does | Example | Result |
|---|---|---|---|
+ - * |
add, subtract, multiply | 7 * 2 |
14 |
/ |
divide (always gives a float) | 7 / 2 |
3.5 |
// |
divide and drop the decimals | 7 // 2 |
3 |
% |
remainder | 7 % 2 |
1 |
** |
power | 7 ** 2 |
49 |
f-strings
An f-string puts values into text. Write the letter f before the opening quotation mark. Put a name or a calculation inside braces.
name = "Sam"
print(f"Hello, {name}!")
print(f"Two plus two is {2 + 2}.")
Your turn
Follow the five tasks. Each task is checked on its own.
Tasks
5 to do
Task 1, not checked yet. Make a variable named
planetwith the textMars.Task 2, not checked yet. Make a variable named
visitorswith the number3. On the next line, change it to4.Task 3, not checked yet. Make the variables
price(12) andcount(5). Then make a variabletotalthat ispricemultiplied bycount.Task 4, not checked yet. Share
17items among5people. Make a variablegroupswith the number of whole groups, and a variableleftoverwith the number of items that remain. Use//and%.Task 5, not checked yet. Print the sentence
Mars has 4 visitors today.Build it with an f-string that usesplanetandvisitors.
Task 1 of 5
Make a variable named planet with the text Mars.