1 / 32
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

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

TypeMeaningExampletype() returns
intWhole numbers42, -7, 0<class 'int'>
floatDecimal numbers3.14, -0.5, 1.0<class 'float'>
strText (characters)"hello", "123"<class 'str'>
boolTrue or False onlyTrue, 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

OperatorMeaningExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division (float)10 / 33.333...
//Floor division10 // 33
%Modulo (remainder)10 % 31
**Exponentiation2 ** 8256
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

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal5 != 3True
>Greater than10 > 7True
<Less than3 < 1False
>=Greater or equal5 >= 5True
<=Less or equal4 <= 3False
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:

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?

A3
B1
C3.33
D0
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)?

AError
Bhahaha
Cha3
D3ha
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...

Next chapter: Conditionals — using these comparison operators to make Python make decisions.