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.
type() to inspect any valueThe 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.
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:
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
Use type() to investigate each mystery value. Set each answer to the correct type name as a string.
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:
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?
Predict the boolean result of each expression before running it. Set each answer to True or False (no quotes β actual booleans!).
== 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:
# β 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
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
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.
"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
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.
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().