Data types & math operations
Numbers and text behave completely differently in computer memory. '123' + '456' is '123456', while 123 + 456 is 579. Understanding types prevents subtle bugs and unlocks math calculations.
- Distinguish strings, integers, floats, and booleans.
- Convert types using int(), float(), and str().
- Calculate bill splits with percentage tips (Tip Calculator logic).
Primitive types, type conversion, and math operators.
Python supports int, float, bool, and str. Type casting is done via int(), float(), str(), and bool(). Division with / always returns a float in Python 3; use // for floor division.
Primitive types & type conversion
Python features 4 primary primitive types: str (text), int (whole numbers), float (decimals), and bool (True/False). Convert between them explicitly.
The Tip Calculator project logic
The iconic Day 2 project: calculate the total bill with a percentage tip and split the cost evenly among people.
Memory Trace: Tip Calculator Math & Division
Watch how float arithmetic accumulates the total bill and splits it.
| Variable | Value in Memory | Data Type |
|---|---|---|
| bill | 150.0 | float |
Stores 150.0 with decimal precision.
bill = 150.0🔧 Float Arithmetic & Conversion Bug
A billing script was supposed to calculate a 15% tip on a $100 bill, but concatenated string characters instead of computing math.
Predict The Output (2 Questions)
Your lightbulb moment
The key takeaway
Primitive types govern memory representation. Use type converters when reading data, and use float operations for currency math.
Make it yours
Try changing the tip percentage to 18% and split among 4 people to test how the per-person total recalculates.
Try editing the code in the playground above in Python, JavaScript, Java, or C++, and use the step-by-step Dry-Run Table to inspect how variables mutate in memory. If you get stuck, click the 🤖 AI Doubt Solver button on your screen for instant explanations!