Python · essentials
How can a few names express a real prediction?
Lesson 1 of 8 · Values, names, operators and units
Feature units and types determine what a price expression means, before syntax is introduced.
Loading local progress…
Need a quick refresher?
Prerequisite recall: None; optional Algebra variables recall. Algebra functions · Statistics. Direct entry is allowed.
Never written code? Start with Python from zero: six short parts, from = versus == to deploying a model.
Values, names and the units you forgot
1 · Observe
A listing quotes square feet; your model wants square metres
| Word | What it means |
|---|---|
| Type | What kind of value it is: int, float, str, bool. |
| Name (variable) | A label pointing at a value. |
| Float | A decimal number, stored as a binary approximation. |
| Unit | What a number measures, such as sq ft or sq m. |
| Tolerance | How close two floats may be and still count as equal. |
A flat is advertised at 1,075 sq ft for ₹62.5 lakh. Your model was trained on square metres. If you feed it the raw number, it will happily produce a nonsense price, because Python does not know what the numbers mean. The first skill is knowing what each value is, what it is called, and what it measures.
2 · Express
Types, names and operators
Every value has a type: int for whole numbers, float for decimals, str for text, bool for True or False. A name such as price points at a value, and rebinding the name does not change the old value. Operators combine values, and the type of the result depends on the operator.
3 · Visualize
What the listing gives
| Quantity | Value |
|---|---|
| Area, quoted | 1,075 sq ft |
| Area in sq m (× 0.092903) | 99.87 |
| Price per sq ft | ₹5,814 |
| Price per sq ft if divided by sq m by mistake | ₹62,581 |
The last row is the silent bug: dividing by the wrong unit gives a number ten times too big, and nothing crashes.
See the numbers behind this picture
| Calculation | ₹ per sq ft |
|---|---|
| Correct (sq ft) | 5,814 |
| Wrong unit (sq m) | 62,581 |
4 · Discover
Floats and names
0.1 + 0.2 == 0.3 is False, because floats store binary approximations. Compare floats with a tolerance. And after backup = price, followed by price = price * 1.05, backup still holds 62.5 while price is 65.625: assignment points a name at a value, it does not link two names.
5 · Apply
Two worked examples
Example 1, unit conversion. 1,075 × 0.092903 = 99.87 sq m. Keep the unit in the name: area_sqm, not area.
Example 2, integer division. Splitting 7 flats between 2 agents: 7 // 2 = 3 each and 7 % 2 = 1 left over.
6 · Where it goes wrong
Three ways it goes wrong
7 · In Python
Values and names in code
Here is how to implement it, step by step. The code below does exactly these steps, and every line is editable.
- Write each value in a named variable, with the unit in the name (area_sqft).
- Convert units explicitly: sq m = sq ft × 0.092903.
- Compute price per sq ft from the matching unit.
- Compare floats with a tolerance, not ==.
- Remember assignment points a name at a value: backup = price does not link them.
area_sqft = 1075 # a listing quotes square feet
area_sqm = area_sqft * 0.092903
price_lakh = 62.5
rate = price_lakh * 100000 / area_sqft
print("type of each name:", type(area_sqft).__name__, type(area_sqm).__name__, type(price_lakh).__name__, type(True).__name__, type("Pune").__name__)
print("area in sq m:", round(area_sqm, 2), "| price per sq ft in rupees:", round(rate, 1))
# division, floor division and remainder
print("7 / 2 =", 7 / 2, "| 7 // 2 =", 7 // 2, "| 7 % 2 =", 7 % 2, "| 2 ** 10 =", 2 ** 10)
# floats are approximate
print("0.1 + 0.2 == 0.3 ->", 0.1 + 0.2 == 0.3, "| equal within a tolerance of 1e-9:", abs((0.1 + 0.2) - 0.3) < 1e-9)
# names point at values; rebinding does not change the old value
price = 62.5
backup = price
price = price * 1.05
print("price", round(price, 3), "| backup", backup)
# mixing units is a silent bug
print("rupees per sq ft if someone divides by sq m by mistake:", round(price_lakh * 100000 / area_sqm, 1))type of each name: int float float bool str
area in sq m: 99.87 | price per sq ft in rupees: 5814.0
7 / 2 = 3.5 | 7 // 2 = 3 | 7 % 2 = 1 | 2 ** 10 = 1024
0.1 + 0.2 == 0.3 -> False | equal within a tolerance of 1e-9: True
price 65.625 | backup 62.5
rupees per sq ft if someone divides by sq m by mistake: 62580.9Edit and run this code in your browser
Live Python
Types, operators, a float surprise and a unit slip
Runs in your browser on your device. Nothing you type is sent anywhere, and nothing here changes your saved progress.
Press Ctrl+Enter (⌘+Enter on Mac) to run. Tab moves focus to the next control, as usual; use spaces to indent.
Why this matters
Feature units and types determine what a price expression means, before syntax is introduced.
Mathematical intuition: Variables and function evaluation; numbers carry feature/target units.
Terminology: assignment, int, float, str, bool, expression, operator, precedence.
Worked starting point: area=2; weight=100; bias=50; prediction=area*weight+bias gives 250 (₹ lakh); '2'*3 gives '222', not 6.
Now change the conditions: Change numeric area to text and explain why repetition is not a prediction.
Common misconception: Assignment is not algebraic equality; a numeric-looking string is not a number.
Revisit the unchanged original P01 lesson and its worked explanations. Module practice uses the same P01 question/experiment semantics; copying original evidence remains opt-in from the overview.
Classify numeric, text and boolean feature values.
The stored value's type determines which operations mean numeric arithmetic, text processing or a yes/no decision.
Worked P01 reference
rooms = '3'- str — text
Success means: Choose the type of a new customer record field.
Classify numeric, text and boolean feature values.
Not passed yet. Version A · 0 of 2 correct answers in a row.
What Python type does rooms hold in this feature record?
rooms = '3'Evaluate a weighted expression with correct types, order and units.
Multiplication precedes addition: 2 × 100 + 50 = 250 (₹ lakh). The units and numeric types make it meaningful; quoted text repeats.
Worked P01 reference
area = 2
weight = 100
bias = 50
prediction = area * weight + bias
text_example = '2' * 3- 250 (₹ lakh), numeric prediction
- Multiply numeric area by weight (₹ lakh/unit), then add bias (₹ lakh). '2' * 3 repeats text, not a price.
Success means: Predict a changed feature rule and reject string repetition as multiplication.
Evaluate a weighted expression with correct types, order and units. — a key decision: choose, then explain
Not passed yet. Version A · 0 of 2 correct answers in a row.
Area is in area units, weight in ₹ lakh/unit and bias in ₹ lakh. Predict the house-price rule, then justify its types, order and units. Both parts must be correct.
area = 2
weight = 100
bias = 50
prediction = area * weight + bias
text_example = '2' * 3Distinguish reassignment from an equality comparison.
The assignment stores 3. Comparing it with 2 gives False without changing it.
Worked P01 reference
area = 2
area = area + (1)
comparison = area == 2- Final numeric area: 3
- False; == does not reassign area.
Success means: Predict the final value after two assignments versus ==.
Distinguish reassignment from an equality comparison.
Not passed yet. Version A · 0 of 2 correct answers in a row.
Trace the stored feature value and the comparison result. Does == change the value?
area = 2
area = area + (1)
comparison = area == 2Choose parentheses and safe numeric conversion for a supplied expression.
Both (float(area)+1)*100 and float(area)*100+100 give 300 for this numeric text. Neither accepts 'unknown': float conversion raises ValueError, not a zero price.
Worked P01 reference
area = '2'
prediction = area + 1 * 100
invalid_area = 'unknown'- Repair the numeric expression: (float(area) + 1) * 100 · What if area is 'unknown'?: ValueError: the text is not a supported number.
Success means: Repair a preset precedence or conversion slot.
Choose parentheses and safe numeric conversion for a supplied expression.
Not passed yet. Version A · 0 of 2 correct answers in a row.
The intended feature rule adds one area unit, then multiplies by 100 (₹ lakh/unit). Repair it, and identify what invalid text does.
area = '2'
prediction = area + 1 * 100
invalid_area = 'unknown'Scoped lesson reset
Independent assessment · Essentials notebook. Neither lesson completion nor notebook use awards mastery.
Make the idea yours
If a price is stored in thousands rather than rupees, why must the units of the calculation change too?
Explain the mechanism and give a small example. A matching output alone does not establish why it happened.
Check your explanation against the key distinction
Assignment is not algebraic equality; a numeric-looking string is not a number.
Read a larger ML expression
Equation X-Ray · Read the structure, then explain it
Mathematics as a language
One rule, many possibilities
A trip has a 3-rupee booking fee and costs 2 rupees per kilometre. How can one statement describe any permitted distance?
y = 2x + 3
Read the relationship before trying to solve it. This is an illustrative example, separate from the live experiment.
Choose a building block to inspect its role.
Read the operations: Multiply distance by its rate, then add the fee. If a bill is known, solving asks which distance makes both sides equal.
Build the expression from smaller operations
Choose a valid construction in the stated order. This exercise organizes dependencies; it does not claim every implementation must evaluate independent branches in this order.
What problem could this relationship represent?
A booking fee plus a distance charge is one possible interpretation; a fixed production setup cost plus a per-item charge has the same structure.
Context we still need: Symbols alone do not establish currency, physical meaning or whether negative distances are allowed.
Connect the operations to code
bill = 2 * distance_km + 3Read the code as a map of operations. It is not executed here; imports, shapes and numerical safeguards depend on the actual lesson.
Explain a changed situation
Review the five-level equation-reading journey →
Your notes and reveals are temporary. This reading practice does not change lesson completion or saved assessment evidence.
Carry the idea forward
Before continuing, explain what changed, why the code produced that result, and which assumptions mattered.