Data Types & Type Conversion
Every value in Python has a type. The type determines
how Python interprets the value, what operations are possible, and how the
value behaves in expressions. This lesson focuses on four basic types:
int, float, str and bool,
plus conversion between them.
Learning Objectives
- 11.1.1.3 Distinguish between data types in Python
- 11.1.1.4 Convert data types of variables
Craig'n'Dave — The Use of Basic Data Types
Cambridge IGCSE 8.1 · Basic data types, choosing suitable types and casting. Casting begins at approximately 03:25.
Conceptual Anchor
The Container Analogy
Think of data types as different types of containers. A measuring cup stores a number, an envelope stores text, and a light switch stores one of two logical states. Conversion changes how Python represents a value — but only when that value can sensibly be converted.
100
int
"100"
str
Same characters, different behaviour
100 + 200 gives 300,
while "100" + "200" gives "100200".
The type controls the meaning of the + operation.
Python Type Atlas
Integer
Whole numbers without a decimal point.
42
-7
0
Typical use: age, quantity, counter, index.
Floating-point number
Numbers that contain a decimal component.
3.14
-0.5
2.0
Typical use: temperature, measurement, price.
String
A sequence of characters stored as text.
"Hello"
'A'
"123"
Typical use: names, messages, IDs, text input.
Boolean
A logical value with only two possibilities.
True
False
Typical use: status flags, conditions, yes/no states.
| Type | Description | Examples | Check with |
|---|---|---|---|
int |
Whole numbers | 42, -7, 0 |
type(42) → <class 'int'> |
float |
Numbers with a decimal component | 3.14, -0.5, 2.0 |
type(3.14) → <class 'float'> |
str |
Text — a sequence of characters | "Hello", 'A', "123" |
type("Hi") → <class 'str'> |
bool |
Logical value | True, False |
type(True) → <class 'bool'> |
What Type Is It?
Look carefully at the value itself. Quotes, decimal points and Boolean keywords are important clues.
type() Inspector
Type Conversion
Type conversion — often called casting — creates a value represented as another type.
| Function | Converts to | Example | Result |
|---|---|---|---|
int() |
Integer | int("42") |
42 |
int() |
Integer — truncates a float toward zero | int(3.99) |
3 |
float() |
Float | float("3.14") |
3.14 |
float() |
Float | float(5) |
5.0 |
str() |
String | str(42) |
"42" |
bool() |
Boolean | bool(0) |
False |
bool() |
Boolean | bool(1) |
True |
int("42") # 42 ✓
float("3.14") # 3.14 ✓
str(42) # "42" ✓
int("hello") # ValueError ✗
int("3.14") # ValueError ✗Casting Lab
Predict what the expression produces. Some expressions return a value; others fail because the conversion is invalid.
int() Truncates — It Does Not Round
int(3.99) → 3
Moves toward zero.
int(-3.99) → -3
Also moves toward zero.
Compare with round()
int(3.99) gives 3.
round(3.99) gives 4.
They perform different operations.
Why input() Usually Needs Conversion
16
The keyboard supplies characters.
"16"
Python returns a string.
16
Explicit conversion to integer.
16 + 1
Numeric addition is now possible.
17
Correct numeric output.
# input() always returns str
age_text = input("Enter your age: ")
print(type(age_text)) # <class 'str'>
# Convert before arithmetic
age = int(age_text)
print(age + 1)
# Common compact form
age = int(input("Enter your age: "))Automatic vs Explicit Conversion
Automatic numeric promotion
result = 5 + 2.0 # Python combines int + float # as a float result result == 7.0 type(result) == float
Explicit conversion
x = int("5")
y = float(5)
z = str(42)
# The programmer explicitly
# requests a new type.
Python 3 division
The / operator returns a float.
Therefore 10 / 2 gives 5.0.
Floor division // behaves differently:
10 // 2 gives 5.
Expression Predictor
Predict both the value and its type.
Truthy & Falsy Values
When bool() is used, some values convert to
False and others to True.
Falsy in this lesson
0 0.0 "" FalseTruthy examples
42 -1 "Hi" "False" "0"Important misconception
bool("False") is True.
The value is a non-empty string.
Python does not read the word inside the string and reinterpret it as the Boolean value
False.
bool(0) # False
bool(42) # True
bool("") # False
bool("Hi") # True
bool(0.0) # False
bool(-1) # True
bool("False") # True
bool("0") # TrueChoose the Best Data Type
Student age
Whole number of years.
int
Temperature
May include decimal values.
float
Student name
Text containing characters.
str
Is logged in?
Only two logical states are required.
bool
Number of books
A count of whole items.
int
Price
Can contain a decimal component.
float
Worked Examples
Conversion Error Lab
Decide whether the expression is valid or which error would occur.
Pitfalls & Common Errors
Invalid String Conversion
int("hello") and int("3.14") raise
ValueError. The text must have a valid integer form for direct
conversion with int().
"100" Is Not 100
"100" + "200" gives "100200",
while 100 + 200 gives 300.
input() Returns str
x = input("Number: ") gives text.
Use x = int(input("Number: ")) when an integer is needed.
bool("False") Is True
Any non-empty string is truthy in this context, even strings such as
"False" and "0".
int() Does Not Round
int(7.9) gives 7.
It truncates toward zero rather than rounding to the nearest integer.
/ Returns float
10 / 2 gives 5.0 in Python 3,
so type(10 / 2) is float.
String Repetition Is Valid
"5" * 3 gives "555".
The left operand is a string, so Python repeats the text.
Pro-Tips for Exams
Type Questions Strategy
- A decimal point usually indicates
float, even in5.0. - Quotes indicate
str, even in"123". TrueandFalsewithout quotes arebool.int()truncates toward zero; it does not round.input()returnsstrunless you explicitly convert it.- Python 3:
/returnsfloat. - When asked for an expression result, give both the value and type if required.
Graded Tasks
Name the four basic Python data types studied in this lesson and give two examples of each.
Explain why "5" and 5 behave differently
when used with the + operator.
Choose a suitable data type for: student age, product price, username and whether an account is active.
Write code that asks the user for their birth year, converts the input to an integer and calculates an age.
Correct the following so that it works:
value = int("12.5").
Give one reasonable solution.
Predict the value and type of:
"5" + "3",
5 + 3,
5.0 + 3,
int("5") + 3,
10 / 2,
"5" * 3.
Write a temperature converter:
input Celsius, convert it to float,
calculate Fahrenheit using F = C × 9/5 + 32,
and display the result.
Exam Practice
Q1: State the type of each value:
17, 17.0, "17", False. [4]
Reveal marking points
Q2:
x = "8",
y = 2.
State the output and its type for int(x) + y. [2]
Reveal marking points
Q3: Write the result of int(-4.9) and explain why. [2]
Reveal marking points
Q4: What is the output of print(type(10 / 2))? [1]
Reveal marking point
Q5: Explain why int("3.14") fails but float("3.14") succeeds.
[2]
Reveal marking points
Self-Check Quiz
input() return?
str — even if the user types digits.
int(4.7)?
4 — int() truncates toward zero.
type(True)?
<class 'bool'>
bool("")?
False — the string is empty.
bool("False")?
True — it is a non-empty string.
print(type(10 / 2))?
<class 'float'> because / returns a float in Python 3.