Learn/Python/2. Values and variables
Lesson 2 of 15
Sign in

Python, lesson 2

Values and variables

A program works with values: numbers, pieces of text, yes/no answers. A variable is a name that points at a value so you can use it again later. You create one with =:

There is no keyword like let or var, and you don't declare a type. The first assignment creates the name. A later assignment simply points the same name at a new value:

The basic types

Every value has a type, and the type decides what you can do with it. You can ask for it with type():

TypeExample valuesUsed for
int, ,

2 exercises to try what you just read.

Exercise 1

Movie night tickets

Press Ctrl Enter or Check to test your code.

Exercise 2

Swap the cups

Press Ctrl Enter or Check to test your code.

0
-7
545_000
whole numbers of any size
float3.14, -0.5, 2.0numbers with a fraction
str"hi", '42', ""text
boolTrue, Falseyes/no answers
NoneNone"no value here yet"

Notice that 42 and "42" are different things. One is a number you can do maths with; the other is two characters of text. Python will not silently mix them: "42" + 1 is an error. You convert on purpose with int(), float() and str().

Naming things

Variable names use lowercase letters, digits and underscores, and can't start with a digit. Python style (PEP 8) joins words with underscores: total_price, not totalPrice. Pick names that say what the value means. seconds_left tells the reader far more than x.

Names are case sensitive, so Score and score are two different variables.

Constants

Python has no const keyword. Instead, everyone agrees that a name written in ALL CAPS is a constant, a value you set once and never change:

Nothing stops you from reassigning it, but other programmers will read the capitals as a promise that you won't.

Several names at once

You can assign several variables in one line. Python evaluates everything on the right before storing anything on the left, which makes swapping two values a one-liner:

Putting values into text with f-strings

Put an f before the opening quote and you can drop any expression into the text inside curly braces. This is called an f-string and it is the usual way to build messages:

f-strings call str() for you, so you never need to convert numbers by hand just to print them.

Further reading

  • The Python Tutorial: Numbers, text and variables
  • Built-in types
  • PEP 8: Naming conventions

The starter reads a name (first line) and a number of seats (second line). A ticket costs 12. Store the price in a constant called TICKET_PRICE, work out the total, and print one line in exactly this form:

Sample input:

Sample output:

Two cups sit on a table. The first input line is what is in the left cup, the second line is what is in the right cup. Swap them using a single multiple assignment, then print:

Sample input:

Sample output:

city = "Lisbon"
population = 545_000      # underscores make big numbers easier to read
average_temp = 17.4
is_capital = True
score = 10
score = score + 5   # the right side is worked out first, then stored
print(score)        # 15
print(type(42))      # <class 'int'>
print(type("42"))    # <class 'str'>
TAX_RATE = 0.23
MAX_PLAYERS = 4
left, right = "cat", "dog"
left, right = right, left
print(left, right)    # dog cat
item = "coffee"
price = 2.5
print(f"One {item} costs {price} euros.")
print(f"Two cost {price * 2} euros.")    # any expression works inside {}
<name> booked <seats> seats for 36.
Ana
3
Ana booked 3 seats for 36.
left: <left>
right: <right>
tea
juice
left: juice
right: tea