Unit 11.1A · Term 1

Data Types & Type Conversion

Every value in Python has a type that determines what operations can be performed on it. Understanding data types and knowing how to convert between them is fundamental to writing correct programs.

Learning Objectives

  • 11.1.1.3 Distinguish between data types in Python
  • 11.1.1.4 Convert data types of variables

Lesson Presentation

11.1A-lesson-02-data-types.pdf · Slides for classroom use

Conceptual Anchor

The Container Analogy

Think of data types as different types of containers. A glass holds liquid (numbers), an envelope holds letters (strings), and a light switch is either on or off (boolean). You can pour liquid from a glass into a measuring cup (type conversion), but you can't pour a letter into a glass — the types must be compatible.

Rules & Theory

Python Data Types

Type Description Examples Check with
int Whole numbers (no decimals) 42, -7, 0 type(42)<class 'int'>
float Decimal numbers 3.14, -0.5, 2.0 type(3.14)<class 'float'>
str Text (sequence of characters) "Hello", 'A', "123" type("Hi")<class 'str'>
bool Logical value: True or False True, False type(True)<class 'bool'>
# Checking types x = 10 y = 3.14 name = "Ali" is_student = True print(type(x)) # <class 'int'> print(type(y)) # <class 'float'> print(type(name)) # <class 'str'> print(type(is_student)) # <class 'bool'>

Type Conversion Functions

Function Converts to Example Result
int() Integer int("42") 42
int() Integer (truncates) int(3.99) 3 (not rounded!)
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

Important: int() truncates, not rounds!

int(3.99) gives 3, not 4. It simply removes the decimal part. To round, use round(3.99) which gives 4.

Worked Examples

1 Distinguishing Types

# What type is each value? a = 100 # int — whole number b = 100.0 # float — has decimal point c = "100" # str — in quotes d = True # bool — True/False keyword # Note: "100" is NOT the same as 100! print(type("100")) # <class 'str'> print(type(100)) # <class 'int'>

2 Converting User Input

# input() always returns a string! age_str = input("Enter your age: ") # e.g., "16" print(type(age_str)) # <class 'str'> # Convert to int for calculations age = int(age_str) print(type(age)) # <class 'int'> print("Next year you'll be:", age + 1) # Next year you'll be: 17 # One-line version age = int(input("Enter your age: "))

3 Mixing Types in Expressions

# int + float → float (automatic promotion) result = 5 + 2.0 print(result) # 7.0 print(type(result)) # <class 'float'> # int / int → float (in Python 3) result = 10 / 3 print(result) # 3.3333333333333335 # String + number → ERROR! # print("Age: " + 16) # TypeError! print("Age: " + str(16)) # Age: 16 ✓

4 Boolean Conversion Rules

# What values are "truthy" vs "falsy"? print(bool(0)) # False — zero is falsy print(bool(42)) # True — any non-zero is truthy print(bool("")) # False — empty string is falsy print(bool("Hi")) # True — non-empty string is truthy print(bool(0.0)) # False print(bool(-1)) # True — even negative numbers!

Pitfalls & Common Errors

Converting Invalid Strings

int("hello") or int("3.14") will crash with a ValueError. You can only convert strings that look like valid integers: int("42") ✓. For decimals, use float("3.14") first, then int() if needed.

Confusing "100" and 100

The string "100" and the integer 100 look similar but behave very differently. "100" + "200" gives "100200" (concatenation), while 100 + 200 gives 300 (addition).

Forgetting input() Returns String

x = input("Number: ") gives a string, even if the user types 5. You must convert: x = int(input("Number: ")).

Pro-Tips for Exams

Type Questions Strategy

  • If it has a decimal point, it's a float (even 5.0)
  • If it's in quotes, it's a str (even "123")
  • If it's True or False (capitalized), it's bool
  • int() truncates (cuts off decimal), it does NOT round
  • Python 3: 10 / 3 = 3.333... (float), 10 // 3 = 3 (int)

Graded Tasks

Remember

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

Understand

Explain why int(7.9) returns 7 and not 8.

Apply

Write a program that asks the user for their birth year (as a string), converts it to an integer, and calculates their age.

Analyze

Predict the output and type of each: "5" + "3", 5 + 3, 5.0 + 3, int("5") + 3

Create

Write a temperature converter: input Celsius as a string, convert to float, calculate Fahrenheit (F = C × 9/5 + 32), and output the result.

Self-Check Quiz

1. What type does input() always return?
Click to reveal answer
2. What is the result of int(4.7)?
Click to reveal answer
3. What is type(True)?
Click to reveal answer
4. What is bool("")?
Click to reveal answer
5. What is the output of print(type(10 / 2))?
Click to reveal answer