Order number, amount, status — a program constantly holds small pieces of data, and every piece gets a name. That name is a variable. The second thing you learn on day one: text and numbers look alike and behave differently.
A variable is a name for a value
live example
order_id = "ord-042"
status = 200
print(order_id, status)
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
Name on the left, value on the right, an equals sign between them. It is not the "equals" of mathematics but "put the right side into the left".
The value can be replaced — that is what makes it a variable:
live example
status = 200
status = 404
print(status) # 404
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
Practice here: Print a string · Define a variable
Numbers and strings are different things
The quotes decide everything. "200" is a string, three characters of text.
200 is a number you can do arithmetic with:
live example
code = "200"
print(code + "1") # 2001 — strings are glued together
code = 200
print(code + 1) # 201 — numbers are added
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
A value almost always arrives as text — from a file, from a form, from a service
response — and arithmetic needs numbers. int() and float() do the conversion:
live example
total = int("4990") # 4990, now a number
price = float("99.90") # 99.9, a decimal
print(total + 10, price) # 5000 99.9
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
The other way round, from number to text, is str():
live example
text = str(4990)
print(text + " RUB") # 4990 RUB — glued as text
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
int("four") fails — and rightly so: there is nothing to convert.
Practice here: Number from a string
Strings: the daily material
Quotes around a string are single or double — take the ones that do not appear
inside the text itself. len() gives the length:
live example
print(len("ord-042")) # 7
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
A message is assembled from pieces with an f-string: put an f before the quotes
and write the expression in curly braces. Addition works too, but only between
strings — a number cannot be plugged in that way.
live example
# error: wrong way — a number cannot be glued to a string
order_id = "ord-042"
total = 4990
print("Order " + order_id + " for " + total)
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
The program stops with TypeError: can only concatenate str (not "int") to str,
that is, "a string can only be glued to a string".
live example
# right way
order_id = "ord-042"
total = 4990
print(f"Order {order_id} for {total} RUB")
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
Inside the braces you can calculate: f"twice {total * 2}".
Searching inside a string is the in operator:
live example
response = "Order not found"
print("not found" in response) # True
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
The answer is True or False. Those two values are called booleans, and every
check rests on them.
Five operations you will need all the time:
live example
print(" ord-042 ".strip()) # ord-042 — trim spaces
print("ORD-042".lower()) # ord-042 — lower case
print("ord-042".startswith("ord")) # True — starts with
print("ord-042".split("-")) # ['ord', '042'] — cut by a character
print("ord-042".replace("ord", "x")) # x-042 — replace
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
strip() and lower() save you when comparing data from different places: one
file says "Anna ", another says "anna" — the same thing to the eye, not to the
program.
Practice here: Substitute a value into a string · String length · String inside a string
Comments
Everything after a # is a note for a human, the program skips it:
live example
price = 4990 # price before the discount
print(price)
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
Write them where the code does not explain why: what a line does is visible anyway.
Common mistakes
Unclosed quotes — print("hello). A SyntaxError with the line number;
check quote and bracket pairs.
Typo in a name — you defined order_id and used order_di. NameError: name 'order_di' is not defined, that is, "I do not know that name".
Compared a number with a string. The program does not crash, it quietly answers "no match":
live example
# wrong way
code = "200"
print(code == 200) # False — text compared with a number
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
live example
# right way
code = "200"
print(int(code) == 200) # True — both brought to one type first
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
In short
- A variable is a name for a value;
=stores, it does not compare. "200"and200are different: text becomes a number withint()andfloat(), a number becomes text withstr().- An f-string (
f"Order {order_id}") is the normal way to build a message; a number cannot be glued to a string with+. strip()andlower()bring data from different places to a comparable form.inanswersTrueorFalse— every check starts there.
What to read next
- Comparisons and conditions — how a program makes a decision.
- Lists and dicts — what JSON looks like from the inside.
- Loops — how to go through every record and miss nothing.