Module 1 Β· Lesson 3 of 5

Data Types

Every value in Python has a type that tells Python what kind of data it is and what operations you can perform with it. Understanding types is key to avoiding bugs.

βœ“ Identify Python's four core data types
βœ“ Use type() to inspect any value
βœ“ Predict the result of comparison expressions
βœ“ Understand and fix TypeErrors

The Four Core Types

Python has many data types, but these four are the foundation. Almost everything you'll do in the first few units builds on them.

str β€” String
Text data, wrapped in quotes
"hello"   'Python'   "42"
int β€” Integer
Whole numbers, no decimal
42   -7   0   1000
float β€” Floating Point
Numbers with a decimal point
3.14   -0.5   100.0
bool β€” Boolean
True or False β€” that's it
True   False
πŸ’‘ Key Concept

Quotes make all the difference. 42 is an integer, but "42" is a string. They look similar to us, but Python treats them completely differently.

Inspecting Types with type()

Not sure what type a value is? Wrap it in type() to find out:

types.py
print(type("hello"))   # β†’ <class 'str'>
print(type(42))        # β†’ <class 'int'>
print(type(3.14))      # β†’ <class 'float'>
print(type(True))      # β†’ <class 'bool'>
print(type("True"))    # β†’ <class 'str'>  ← Quotes make it a string!

# Works on variables too:
score = 99
print(type(score))     # β†’ <class 'int'>
πŸ”

Challenge 1: Type Inspector

Easy

Use type() to investigate each mystery value. Set each answer to the correct type name as a string.

Instructions: For each mystery value, set the answer variable to "str", "int", "float", or "bool".
Look at each value carefully. Does it have quotes? Then it's a str, regardless of what's inside. Does it have a decimal point (without quotes)? That's a float. True and False without quotes are bool.

Booleans & Comparisons

Booleans are the simplest type β€” just True or False. They become incredibly powerful when you start using comparison operators, which produce boolean results:

compare.py
print(10 > 5)       # β†’ True
print(3 == 3)       # β†’ True   (== checks equality)
print(3 == 3.0)     # β†’ True   (Python compares the values)
print("a" == "A")   # β†’ False  (case-sensitive!)
print(7 != 7)       # β†’ False  (!= means "not equal")
print(5 >= 5)       # β†’ True   (greater than or equal to)

# The result of a comparison IS a boolean:
result = 10 > 3
print(result)        # β†’ True
print(type(result))  # β†’ <class 'bool'>
βš–οΈ

Challenge 2: True or False?

Easy

Predict the boolean result of each expression before running it. Set each answer to True or False (no quotes β€” actual booleans!).

Instructions: Replace each None with True or False based on what you think the expression evaluates to. Then run to check.
Remember: == is case-sensitive for strings. != means "not equal." 3.0 == 3 compares values across types (they're equal!). For strings, < compares alphabetically (like dictionary order).

When Types Collide: TypeError

Python is strict about mixing types. Try to add a string and an integer, and you'll get a TypeError:

errors.py
# ❌ This will crash:
age = 25
print("I am " + age + " years old")
# TypeError: can only concatenate str (not "int") to str

# βœ… Fix: convert the int to a string first
print("I am " + str(age) + " years old")
# β†’ I am 25 years old
⚠️ Common Trap

The + operator does different things depending on type. For numbers, it adds. For strings, it concatenates (joins). But you can't mix them β€” Python won't guess what you mean.

πŸ—‚οΈ

Challenge 3: Type Sorter

Medium

Eight values are mixed together. Sort them by creating four lists β€” one for each type. Put each value's variable letter into the correct list.

Instructions: Look at values a through h. Classify each into the correct list as a string letter. Example: if a is a string, add "a" to the strings list.
Think carefully: "False" has quotes so it's a string. True and False without quotes are booleans. 0 is an integer. "" is an empty string (still a string!). 3.14 and -7.0 have decimal points so they're floats. 100 is an integer.
🧟

Challenge 4: Frankenstein

Stretch

The code below has three TypeError bugs caused by mixing incompatible types. Find and fix each one so the program runs and produces the expected output.

Instructions: Fix the three lines marked with # BUG so the code runs without errors. You'll need to use type conversion functions like str() or int(). Don't change the print text β€” just fix the type mismatches.
Bug 1: Wrap score in str() to concatenate it with strings. Bug 2: Wrap bonus in int() so Python can add it as a number. Bug 3: "⭐" * 2.5 doesn't work β€” you can only repeat strings by an integer. Wrap stars in int().