Python

Lesson 2 of 10

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

  1. Task 1, not checked yet. Make a variable named planet with the text Mars.

  2. Task 2, not checked yet. Make a variable named visitors with the number 3. On the next line, change it to 4.

  3. Task 3, not checked yet. Make the variables price (12) and count (5). Then make a variable total that is price multiplied by count.

  4. Task 4, not checked yet. Share 17 items among 5 people. Make a variable groups with the number of whole groups, and a variable leftover with the number of items that remain. Use // and %.

  5. Task 5, not checked yet. Print the sentence Mars has 4 visitors today. Build it with an f-string that uses planet and visitors.

Task 1 of 5

Make a variable named planet with the text Mars.

All tasks

main.py

Ctrl+Enter runs your code. Ctrl+Shift+Enter checks it. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.