Programming · Grade 10 · Chapter 6
Data Types & Operators
Why Python cares whether something is a number or text — and how operators behave differently depending on type.
Learning objectives
What We'll Cover
- Understand Python's four core data types:
int, float, str, bool
- Use arithmetic, comparison, and logical operators correctly
- Convert between data types with type casting
- Use
type() to inspect what type a value is
- Identify and fix TypeError bugs
- Understand operator precedence (order of operations)
The bug
"5" + 3 = ???
A student writes a program to add two numbers entered by the user. They input 5 and 3. The result is "53". What happened?
Think first: input() always returns a string — even if the user typed a number. So Python added "5" + "3" = "53" (string concatenation), not 5 + 3 = 8.
The four types
Python's Core Data Types
| Type | Meaning | Example | type() returns |
| int | Whole numbers | 42, -7, 0 | <class 'int'> |
| float | Decimal numbers | 3.14, -0.5, 1.0 | <class 'float'> |
| str | Text (characters) | "hello", "123" | <class 'str'> |
| bool | True or False only | True, False | <class 'bool'> |
Inspecting types
The type() Function
Use type() to check what data type a value or variable is at runtime.
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
x = input("Enter: ")
print(type(x)) # Always <class 'str'>
Quick check · 1
What type does this expression produce? 7 / 2
Aint (3)
Bfloat (3.5)
Cstr ("3.5")
DTypeError
Click to reveal answer
Note: In Python 3, / always produces a float. Use // for integer (floor) division.
Arithmetic operators
The Arithmetic Operators
| Operator | Meaning | Example | Result |
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division (float) | 10 / 3 | 3.333... |
| // | Floor division | 10 // 3 | 3 |
| % | Modulo (remainder) | 10 % 3 | 1 |
| ** | Exponentiation | 2 ** 8 | 256 |
Predict the output
What Does Each Print?
Class challenge — predict before running:
print(17 % 5) # ?
print(17 // 5) # ?
print(2 ** 10) # ?
print(9 / 3) # ?
print(9 // 3) # ?
Answers: 2, 3, 1024, 3.0, 3
Type casting
Converting Between Types
Use built-in functions to convert values between types. This is essential when working with input().
# String → Integer
age = int(input("Age: "))
# String → Float
price = float(input("Price: "))
# Integer → String (for concatenation)
msg = "Score: " + str(score)
# Float → Integer (truncates decimal)
whole = int(3.99) # = 3, not 4
Quick check · 2
What does int(3.99) return in Python?
A4 (rounds up)
B3 (truncates towards zero)
C3.99 unchanged
DError
Click to reveal answer
Comparison operators
Comparison Operators — Always Return bool
| Operator | Meaning | Example | Result |
| == | Equal to | 5 == 5 | True |
| != | Not equal | 5 != 3 | True |
| > | Greater than | 10 > 7 | True |
| < | Less than | 3 < 1 | False |
| >= | Greater or equal | 5 >= 5 | True |
| <= | Less or equal | 4 <= 3 | False |
Common mistake: Using = (assignment) instead of == (equality check) inside a condition.
Logical operators
Logical Operators: and, or, not
x = 10
# and: both must be True
print(x > 5 and x < 20) # True
# or: at least one must be True
print(x < 5 or x == 10) # True
# not: reverses the boolean
print(not x > 5) # False
Predict the output
True or False?
The class votes thumbs up (True) or down (False) before revealing:
print(10 > 5 and 3 == 4) # ?
print(10 > 5 or 3 == 4) # ?
print(not (5 == 5)) # ?
print(True and False) # ?
Answers: False, True, False, False
Operator precedence
Order of Operations (PEMDAS in Python)
Python evaluates operators in a specific order. Same as mathematics — with some additions:
- 1st:
() Parentheses
- 2nd:
** Exponentiation
- 3rd:
* / // % Multiplication/Division
- 4th:
+ - Addition/Subtraction
- 5th:
== != > < >= <= Comparisons
- 6th:
not, then and, then or
Rule: When in doubt, use parentheses to make your intent explicit.
Quick check · 3
What does 3 + 4 * 2 evaluate to in Python?
A14 (left to right)
B11 (* evaluated before +)
CError
D24
Click to reveal answer
String type behaviour
How Operators Behave on Strings
String + String = Concatenation
"Hello" + " World"→
"Hello World"
"ha" * 3→
"hahaha"
❌ String + Number = TypeError
"Score: " + 5
TypeError: can only concatenate str (not "int") to str
Fix:
"Score: " + str(5)or:
f"Score: {5}"
Debugging activity
Fix the Type Errors
Find and fix the bug in each snippet:
# Bug 1
years = input("Years: ")
print("Months: " + years * 12)
# Bug 2
price = 29.99
print("Price is: " + price)
# Bug 3
a = "5"
b = "3"
print(a + b) # Expects 8, gets "53"
Fixes: 1) int(years)*12 2) str(price) or f-string 3) int(a)+int(b)
Quick check · 4
Which type would the expression 5 == 5.0 produce?
Aint
Bfloat
Cbool (True)
DTypeError
Click to reveal answer
Note: Python compares int and float values — 5 == 5.0 is True. Both are numerically equal.
Booleans in depth
Everything Has a Boolean Value
In Python, any value can be evaluated as True or False. This is called truthiness.
Truthy values
Any non-zero number:
1, -1, 3.14
Any non-empty string:
"hello"
Any non-empty list:
[1,2,3]
Falsy values
Zero:
0, 0.0
Empty string:
""
None
Empty list:
[]
bool(0) → False | bool(42) → True | bool("") → False | bool("hi") → True
Guided practice
Grade Calculator
Live-code a program that asks for a score (0–100) and prints the percentage rounded to 1 decimal place.
score = int(input("Score (0-100): "))
total = 100
percentage = (score / total) * 100
print(f"Your score: {score}/{total}")
print(f"Percentage: {percentage:.1f}%")
Note: :.1f inside an f-string formats a float to 1 decimal place.
Quick check · 5
What is 10 % 3 in Python?
Click to reveal answer
Modulo returns the remainder: 10 = 3×3 + 1, so 10 % 3 = 1. Used heavily in programming for cycling, checking even/odd, etc.
Real-world application
Why Data Types Matter in Real Systems
Banking
Financial calculations must use the right types.
"50" - 30 crashes the transfer system. Type validation is a security requirement.
AI Models
Neural networks operate on float tensors. Feeding string data without conversion causes the entire training pipeline to fail.
Games
Score, health, and coordinates must be numbers. A string score breaks comparison logic and leaderboard sorting.
Web Forms
Web applications must validate and cast all user input — form fields return strings, but databases may expect integers or booleans.
Challenge
Build a Temperature Converter
Write a program to convert between Celsius and Fahrenheit.
celsius = float(input("Enter temperature in °C: "))
fahrenheit = (celsius * 9 / 5) + 32
print(f"{celsius}°C = {fahrenheit:.1f}°F")
Extension: Add the reverse converter. Stretch: Ask the user which direction to convert and handle both cases.
Quick check · 6
What is the output of: print("ha" * 3)?
Click to reveal answer
Exit ticket
Reflection: The Type System
Think about everything we covered today about data types and operators.
Discuss or write: Why do you think Python requires explicit type conversion instead of just automatically figuring it out? What problems could arise if Python silently converted types for you?
Before you go
Today We Learned...
- Python has 4 core types:
int, float, str, bool
- Operators behave differently depending on the type (
+ adds numbers, concatenates strings)
- Always convert
input() values with int() or float() when you need a number
- Comparison operators return
bool; logical operators combine booleans
- Operator precedence follows PEMDAS — use parentheses when unsure
Next chapter: Conditionals — using these comparison operators to make Python make decisions.