Variables, int, float, str, bool and None, converting between types, and the three errors every beginner meets. Run every example in your browser.
RetailMart product #1 is the Samsung Animi Max, listed at ₹1,25,812.07. A new analyst reads the price from a file and adds ₹100 handling.
The price comes out of a file looking perfectly normal: 125812.07. She stores it in a variable called price and moves on.
Adding 100 crashes with a TypeError. So she adds '100' instead. No error. The answer is 125812.07100: wrong, and silent.
Files hand Python text. The value was a str, not a number. float() converts it, and now the maths is real maths.
In SQL every column has a declared type. In Python the type travels with the value, so you have to stay awake.
| SQL column type | Python type | Example |
|---|---|---|
| INT / BIGINT | int | order_id = 1 |
| NUMERIC / DOUBLE PRECISION | float | price = 125812.07 |
| TEXT / VARCHAR | str | product_name = 'Samsung Animi Max' |
| BOOLEAN | bool | is_active = True |
| NULL | None | phone = None |
Write name = value. Python stores the value and remembers the name. Run it and watch the memory table below the cell.
One = means store, not "equals". The right side is worked out first.
qty = 2 + 3
Price is not price)snake_case namesunit_price, not xPython does not guess. A name that was never created, or spelled with another case, raises NameError.
name 'Price' is not defined tells you exactly what Python could not find.type() tells you what a value is. Try each one.
whole numbers: IDs, counts
order_id = 1
decimals: prices, ratios
price = 125812.07
text in quotes
city = 'Gwalior'
True or False
is_active = True
"no value", like NULL
phone = None
| Column | Python type | Why |
|---|---|---|
| orders.order_id | int | whole number, never 2.5 orders |
| orders.net_total | float | rupees and paise |
| stores.city | str | text, in quotes |
| a yes/no flag | bool | exactly True or False |
| missing phone | None | unknown, not zero and not empty text |
is None, never == None.Computers store decimals in binary, so some come out a tiny bit off. It even happens with real RetailMart prices.
Decimal; we meet it in the numbers topic.Data from files and websites arrives as text. Convert it before you do maths.
bool('False') is True. Any non-empty text is True. Only 0, empty text, None and empty collections are False.int() only accepts digits. Decimals and Indian-style commas raise ValueError.
replace), then convert. For '12.5' go through float() first.Python is strict: it will not mix str and int with +.
can only concatenate str (not "int") to str means one side is still text. Convert it.Samsung Animi Max: price ₹1,25,812.07, cost ₹88,068.45. Variables make the formula readable.
SELECT price - cost AS margin does for every row. In Module 2 pandas does it for all 6,036 products at once.Type the answer, press Run. Green means correct.
Store the price 125812.07 in a variable called price and print it.
price = 125812.07
print(price)Print the type of the value '125812' (with quotes).
print(type('125812'))price arrives as text. Print the total for qty = 3 as a number.
price = '125812'
qty = 3
print(int(price) * qty)Print the margin (price minus cost) rounded to 2 decimals.
price = 125812.07
cost = 88068.45
print(round(price - cost, 2))Predict before you run. These are the traps interviewers like.
Turn '1,25,812' into a number and add 1000.
text = '1,25,812'
print(int(text.replace(',', '')) + 1000)Predict first, then print bool(''), bool('False'), bool(0) separated by spaces.
print(bool(''), bool('False'), bool(0))Swap a and b in ONE line (no third variable), then print them.
a = 1
b = 2
a, b = b, a
print(a, b)y = x then x changes. Print what y is now.
x = 10
y = x
x = 20
print(y)int holds whole numbers, float holds decimals. 5 is an int, 5.0 is a float, and they compare equal (5 == 5.0 is True).0.30000000000000004. Round for display, or use Decimal for exact money.'1' + 1 is an error, it does not guess).None is the single 'no value' object, Python's NULL. Test with x is None.True. Any non-empty string is truthy, whatever it says.Click a card to check yourself.
type('7') show?<class 'str'>. Quotes make it text.'5' + 5?TypeError. Convert one side.int(3.99) gives?3. It cuts the decimals; it does not round.Price the same as price?value is Nonea, b = b, a does what?name = value stores; the right side is worked out first.
int, float, str, bool, None. Ask type() when unsure.
Convert with int(), float(), str() before maths.
Round for display. Exact money needs Decimal.
Test with is None. Not zero, not empty text.
NameError, TypeError, ValueError each say what is wrong.
Samsung Recusandae Lite: price ₹1,06,495.49, cost ₹74,546.84. Work in the cell below, then check against a classmate or in a 1:1.
'1,06,495.49'. Convert it to a float.type() of every variable you made.You are here: topic 1 of 14.
Arithmetic, integer division, modulo, powers, rounding, and exact money with Decimal. You will use every one of them on RetailMart orders.