Unit 11.1A · Term 1

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

int

Integer

Whole numbers without a decimal point.

42 -7 0 Typical use: age, quantity, counter, index.
float

Floating-point number

Numbers that contain a decimal component.

3.14 -0.5 2.0 Typical use: temperature, measurement, price.
str

String

A sequence of characters stored as text.

"Hello" 'A' "123" Typical use: names, messages, IDs, text input.
bool

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.

Choose the Python type.

type() Inspector

Variable


                                Check
                                


                                Result
                                

                            

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 int(value)
str str(value)
float float(value)
bool bool(value)
Conversion is possible only when Python can interpret the source value in a valid way.
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.

Predict the result category.

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

User types 16 The keyboard supplies characters.
input() "16" Python returns a string.
int() 16 Explicit conversion to integer.
Calculation 16 + 1 Numeric addition is now possible.
Result 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.

Work it out before revealing.

Truthy & Falsy Values

When bool() is used, some values convert to False and others to True.

Falsy in this lesson

0 0.0 "" False

Truthy 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") # True

Choose 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.

Choose the result category.

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 in 5.0.
  • Quotes indicate str, even in "123".
  • True and False without quotes are bool.
  • int() truncates toward zero; it does not round.
  • input() returns str unless you explicitly convert it.
  • Python 3: / returns float.
  • When asked for an expression result, give both the value and type if required.

Graded Tasks

Remember

Name the four basic Python data types studied in this lesson and give two examples of each.

Understand

Explain why "5" and 5 behave differently when used with the + operator.

Apply

Choose a suitable data type for: student age, product price, username and whether an account is active.

Apply

Write code that asks the user for their birth year, converts the input to an integer and calculates an age.

Apply

Correct the following so that it works: value = int("12.5"). Give one reasonable solution.

Analyze

Predict the value and type of: "5" + "3", 5 + 3, 5.0 + 3, int("5") + 3, 10 / 2, "5" * 3.

Create

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
17 → int 17.0 → float "17" → str False → bool

Q2: x = "8", y = 2. State the output and its type for int(x) + y. [2]

Reveal marking points
Output/value: 10 Type: int

Q3: Write the result of int(-4.9) and explain why. [2]

Reveal marking points
-4 int() truncates toward zero; it does not round.

Q4: What is the output of print(type(10 / 2))? [1]

Reveal marking point
<class 'float'>

Q5: Explain why int("3.14") fails but float("3.14") succeeds. [2]

Reveal marking points
"3.14" is not a valid integer-form string, so direct conversion with int() raises ValueError. It is a valid floating-point representation, so float("3.14") produces 3.14.

Self-Check Quiz

Six quick checks Click each answer to reveal
1. What type does input() return?
str — even if the user types digits.
2. What is the result of int(4.7)?
4 — int() truncates toward zero.
3. What is type(True)?
<class 'bool'>
4. What is bool("")?
False — the string is empty.
5. What is bool("False")?
True — it is a non-empty string.
6. What is the output of print(type(10 / 2))?
<class 'float'> because / returns a float in Python 3.