Python, lesson 2
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:
Every value has a type, and the type decides what you can do with it. You can ask for it
with type():
| Type | Example values | Used for |
|---|---|---|
int | , , |
2 exercises to try what you just read.
0-7545_000| whole numbers of any size |
float | 3.14, -0.5, 2.0 | numbers with a fraction |
str | "hi", '42', "" | text |
bool | True, False | yes/no answers |
None | None | "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().
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.
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.
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:
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.
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 = Truescore = 10
score = score + 5 # the right side is worked out first, then stored
print(score) # 15print(type(42)) # <class 'int'>
print(type("42")) # <class 'str'>TAX_RATE = 0.23
MAX_PLAYERS = 4left, right = "cat", "dog"
left, right = right, left
print(left, right) # dog catitem = "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
3Ana booked 3 seats for 36.left: <left>
right: <right>tea
juiceleft: juice
right: tea