These are the notes for Python episode 2. You will learn why typing the same value everywhere breaks a program, what a variable is and how = really works, the NameError and Python's "Did you mean" hint, the rules for naming variables, the four basic data types (int, float, str, bool) and the type() function, why 0.1 + 0.2 is not exactly 0.3, None, dynamic typing, and swapping two variables in one line. Every example below runs as-is in the SyntaxPilot compiler, with its real output from Python 3.13.
The problem: the price is typed everywhere
Raju runs a chai stall. One cup costs ₹10, and our bill program has that price typed into every line:
print("Chai x 3 =", 10 * 3)
print("Chai x 5 =", 10 * 5)
print("Chai x 2 =", 10 * 2)
Chai x 3 = 30
Chai x 5 = 50
Chai x 2 = 20
Then milk gets costlier and chai becomes ₹12. We change the first two lines, and in a hurry, miss the third:
print("Chai x 3 =", 12 * 3)
print("Chai x 5 =", 12 * 5)
print("Chai x 2 =", 10 * 2)
Chai x 3 = 36
Chai x 5 = 60
Chai x 2 = 20
Two cups should cost ₹24. No error, just a silently wrong answer.
Why it failed: the same value lived in three places. Changing it meant finding and fixing every copy by hand. Write a value once, give it a name, and use the name everywhere.
What a variable is
A variable is a name for a value. Think of the labelled jars in Raju's kitchen:
| In the kitchen | In Python |
|---|---|
| The label on a jar ("SUGAR") | the variable's name: chai_price |
| What's inside the jar | the value: 12 |
| Putting sugar into the jar | assignment: chai_price = 12 |
| Emptying it and refilling | reassignment: chai_price = 15 replaces 12 |
chai_price = 12
print("Chai x 3 =", chai_price * 3)
print("Chai x 5 =", chai_price * 5)
print("Chai x 2 =", chai_price * 2)
Chai x 3 = 36
Chai x 5 = 60
Chai x 2 = 24
When the price changes again, we change one line (chai_price = 15) and every bill updates: 45, 75, 30.
= is not "equals"
In maths, = means "is the same as". In Python, = is the assignment operator: "put the value on the right into the name on the left". The right side is worked out first:
total = 12 * 3 # 36 is calculated, then stored in total
NameError: using a name Python has never seen
chai_price = 12
print("Chai x 3 =", chai_price * 3)
print("Chai x 5 =", chai_prise * 5)
Chai x 3 = 36
Traceback (most recent call last):
File "main.py", line 3, in <module>
print("Chai x 5 =", chai_prise * 5)
^^^^^^^^^^
NameError: name 'chai_prise' is not defined. Did you mean: 'chai_price'?
Python only knows names you have created. chai_price and chai_prise are two different jars, and the second one doesn't exist. Notice that line 2 ran before the crash: Python runs line by line. And Python even suggests the fix: Did you mean: 'chai_price'?
Naming rules
| Name | Result | Why | Fix |
|---|---|---|---|
2cups = 20 |
SyntaxError: invalid decimal literal |
A name can't start with a digit | cups2 = 20 |
my price = 10 |
SyntaxError: invalid syntax |
No spaces inside a name | my_price = 10 |
class = 5 |
SyntaxError: invalid syntax |
class is a keyword (a word Python keeps for itself) |
class_no = 5 |
- Use letters, digits and underscores; don't start with a digit.
- Names are case sensitive:
priceandPriceare two different variables.
price = 10
Price = 20
print(price, Price)
10 20
- Python style is snake_case: lowercase words joined by underscores:
chai_price,total_bill,is_open. - Choose names that explain themselves.
x,a1,temp2tell the next reader nothing.
Some keywords you can't use as names: False, None, True, and, class, def, elif, else, for, if, import, in, not, or, return, while.
Data types: every value has a kind
The same digits behave differently:
print(10 + 5)
print("10" + "5")
print(type(10), type("10"))
15
105
<class 'int'> <class 'str'>
10 is a number, so + adds. "10" is text, so + joins. A data type is the kind of value, and it decides what you can do with it. The built-in function type() tells you a value's type.
| At the chai stall | Type | Examples |
|---|---|---|
| Cups sold: 3 | int (integer, a whole number) |
3, -7, 2026 |
| Milk left: 2.5 litres | float (floating point, a decimal number) |
2.5, 12.75, -0.5 |
| Customer's name | str (string, text in quotes) |
"Ravi", "10" |
| Is the shop open? | bool (boolean, yes/no) |
True, False |
cups = 3
price = 12.5
name = "Ravi"
shop_open = True
print(type(cups))
print(type(price))
print(type(name))
print(type(shop_open))
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
We never told Python the type. It worked it out from the value: quotes mean str, a decimal point means float, True/False means bool, a plain whole number means int.
The float surprise: 0.1 + 0.2
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
print(10 / 2)
print(type(10 / 2))
0.30000000000000004
0.3
5.0
<class 'float'>
Write 1/3 as a decimal: 0.3333... forever, so it's never exact. Computers store numbers in binary (0s and 1s), and in binary, 0.1 never ends either. The computer cuts it off, and a tiny error remains. This is not a Python bug: every language that uses floating point numbers does it.
- Show money and measurements with
round(value, 2). - A single
/always gives afloat, even when the answer is whole:10 / 2is5.0.
True, False and None
shop_open = true
Traceback (most recent call last):
File "main.py", line 1, in <module>
shop_open = true
^^^^
NameError: name 'true' is not defined. Did you mean: 'True'?
The booleans are True and False, with a capital first letter. Small true is just an unknown name.
None (capital N) is a special value meaning "no value yet". Its type is NoneType.
A variable is a name tag (dynamic typing)
A better picture than a box: a variable is a tag hanging on a value, like a hotel key tag. You can move the tag to any value, even of a different type:
x = 5
print(x, type(x))
x = "five"
print(x, type(x))
x = None
print(x, type(x))
5 <class 'int'>
five <class 'str'>
None <class 'NoneType'>
This is dynamic typing: the type belongs to the value, not to the name. (In Java or C, a variable's type is fixed when you declare it.)
Assigning one variable to another copies the value it points to at that moment:
a = 5
b = a
a = 10
print(a, b)
10 5
Many names at once, and the one-line swap
a, b = "chai", "coffee"
print(a, b)
a, b = b, a
print(a, b)
chai coffee
coffee chai
Python evaluates everything on the right first, then hands the values to the names on the left. By hand, swapping two glasses needs a spare third glass; Python doesn't. You can also give several names the same value: x = y = 0.
Common mistakes (and the real error messages)
| Mistake | Error | Why | Fix |
|---|---|---|---|
print(chai_prise) |
NameError: name 'chai_prise' is not defined. Did you mean: 'chai_price'? |
Typo, or used before it was created | Spell it exactly as created |
2cups = 20 |
SyntaxError: invalid decimal literal |
Name starts with a digit | cups2 |
my price = 10 |
SyntaxError: invalid syntax |
Space in a name | my_price |
class = 5 |
SyntaxError: invalid syntax |
class is a keyword |
class_no |
shop_open = true |
NameError: name 'true' is not defined. Did you mean: 'True'? |
Booleans are capitalised | True |
print("Cups: " + 3) |
TypeError: can only concatenate str (not "int") to str |
+ can't join text and a number |
print("Cups:", 3) |
print(0.1 + 0.2) expecting 0.3 |
prints 0.30000000000000004 |
Binary floating point | round(x, 2) |
Mini project: chai stall bill
# Mini project: chai stall bill
customer = "Ravi"
chai_price = 12
samosa_price = 15.5
cups = 3
samosas = 2
paid_online = True
total = chai_price * cups + samosa_price * samosas
print("Customer:", customer)
print("Chai :", cups, "x", chai_price)
print("Samosa :", samosas, "x", samosa_price)
print("Total :", total)
print("Online :", paid_online)
print(type(total))
Customer: Ravi
Chai : 3 x 12
Samosa : 2 x 15.5
Total : 67.0
Online : True
<class 'float'>
int * int gives an int, but once a float joins the calculation the result is a float.
Practice
- Store your name, age, height in metres and whether you are a student in four variables, and print each with its type.
Solution
name = "Aarav"
age = 19
height = 1.72
is_student = True
print(name, type(name))
print(age, type(age))
print(height, type(height))
print(is_student, type(is_student))
Aarav <class 'str'>
19 <class 'int'>
1.72 <class 'float'>
True <class 'bool'>
- Predict, then check: what are the types of
7,7.0,"7"and7 / 7?
Solution
int, float, str, float (/ always gives a float: 1.0).
- Swap the values of
left = "tea"andright = "biscuit"in one line.
Solution
left, right = "tea", "biscuit"
left, right = right, left
print(left, right)
biscuit tea
- A plate of momos costs ₹60.5. Print the bill for 3 plates, rounded to 2 decimals.
Solution
momo_price = 60.5
plates = 3
print("Bill:", round(momo_price * plates, 2))
Bill: 181.5
Quick quiz
price = 10
price = "12"
print(type(price), 8 / 4)
A) <class 'int'> 2 B) <class 'str'> 2.0 C) <class 'int'> 2.0 D) Error
Answer
B) <class 'str'> 2.0. The second assignment replaces 10 with the string "12". A single / always gives a float.
Interview questions
- Is Python statically or dynamically typed? Dynamically typed: a name can refer to values of any type, and the type is checked when the code runs. It is also strongly typed:
"3" + 3is aTypeError, not silently converted. - Why is
0.1 + 0.2 != 0.3in Python? Floats are stored in binary floating point (IEEE 754), where 0.1 and 0.2 can't be represented exactly. Useround(),math.isclose(), or thedecimalmodule for money. - What is
None? A special singleton value meaning "no value"; its type isNoneType. - What does
a, b = b, ado internally? The right side builds a tuple(b, a)first, then it is unpacked intoaandb. - What naming convention does Python use for variables? snake_case (PEP 8).
Glossary
| Term | Meaning | Example |
|---|---|---|
| Variable | A name that refers to a value | chai_price |
| Value | The data itself | 12, "Ravi" |
| Assignment | Storing a value in a name with = |
chai_price = 12 |
| Reassignment | Giving an existing name a new value | chai_price = 15 |
| Keyword | A word reserved by Python; can't be a name | class, if, True |
| snake_case | Lowercase words joined by underscores | total_bill |
| Case sensitive | Capital and small letters differ | price ≠ Price |
| Data type | The kind of a value; decides what you can do with it | int, str |
int |
Integer: a whole number | 3, -7 |
float |
A number with a decimal point | 2.5 |
str |
String: text in quotes | "chai" |
bool |
Boolean: True or False |
shop_open = True |
type() |
Built-in function that returns a value's type | type(3) → <class 'int'> |
round() |
Rounds a number to given decimal places | round(0.30000000000000004, 2) → 0.3 |
| Binary | Base 2: numbers written with only 0 and 1 | 5 is 101 |
None / NoneType |
The "no value" value and its type | x = None |
| Dynamic typing | The type belongs to the value, so a name can hold any type | x = 5 then x = "five" |
| NameError | Using a name that was never created | chai_prise |
| TypeError | An operation on types that don't fit | "Cups: " + 3 |
| Concatenate | Join strings end to end | "10" + "5" → "105" |
Summary
- A variable is a name for a value;
=puts the value on the right into the name on the left. - Write a value once and use its name everywhere; one change updates everything.
NameErrormeans Python has never seen that name; it often suggests the right one.- Names: letters, digits,
_; no leading digit, no spaces, no keywords; snake_case. - Four basic types:
int,float,str,bool; ask withtype(). 0.1 + 0.2isn't exact (binary floats): show results withround()./always gives a float.True/Falseare capitalised;Nonemeans no value.- Variables are tags: a name can move to any value (dynamic typing);
a, b = b, aswaps.
Next episode: input and type conversion, letting the user type the number of cups.