Quest 2: Data Types - Shapes of Information
Discover the Different Kinds of Data in Python
๐ท QUEST 2 | Difficulty: Beginner | Time: 5 minutes
๐ Complexity Level: Beginner โญ
Perfect for students with no programming experience. This lesson introduces fundamental concepts that are essential for all programming. Great for college fair visitors and first-time coders!
๐ป Interactive Options:
- ๐ Open in JupyterLite - Full Jupyter environment in your browser
- โถ๏ธ Run code directly below - All code cells on this page are editable and runnable
- ๐ฅ Download Notebook - For use in local Jupyter or Google Colab
๐ Introduction: The Shape-Shifting Treasure
Youโve discovered a mysterious treasure room with different types of treasures: gold coins (numbers), ancient scrolls (text), magical crystals (true/false), and floating price tags (decimals). Each type of treasure has unique properties and uses!
Just like treasures come in different forms, data in Python comes in different types. Understanding data types helps you know what you can and canโt do with your information.
๐บ Story Time: You find a magic box that only accepts certain types of items. You canโt add a scroll to a pile of gold coinsโtheyโre different types! Similarly, in Python, you canโt always mix different data types without converting them first.
๐ก Explanation: The Main Data Types
Python has several built-in data types. Here are the most important ones:
๐ข Integers (int)
Whole numbers, positive or negative, with no decimal point.
age = 15
score = 1000
temperature = -5๐ Floats (float)
Numbers with decimal points.
price = 19.99
pi = 3.14159
temperature = 98.6๐ Strings (str)
Text enclosed in quotes (single or double).
name = "Alex"
message = 'Hello, World!'
quote = "To code or not to code"โ Booleans (bool)
True or False values (note the capital T and F!).
is_game_over = False
has_key = True
is_raining = False๐ How to Check Data Types:
Use the type() function to see what type something is:
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("Hello")) # <class 'str'>
print(type(True)) # <class 'bool'>๐ฎ Activity: The Data Type Detective
Run this code to explore different data types:
๐ฏ Challenge: Try these experiments:
- Change
healthto100.0- what type is it now? - Change
player_nameto12345(without quotes) - what happens? - What happens if you write
is_alive = "True"(with quotes)?
Run the code after each change and observe!
๐จโ๐ป Code Example: Working with Different Types
Different types support different operations:
๐ก Watch Out!: Mixing types can cause errors:
# This works:
result = "Score: " + str(100)
# This doesn't work:
# result = "Score: " + 100 # Error! Can't add string and intUse str(), int(), or float() to convert between types!
๐งฉ Puzzle Time!
What will happen with this code? Try to predict, then run it:
๐ Solution Explained:
The output is:
String addition: 10 + 20 = 1020
Number addition: 10 + 20 = 30
Why?
- When you add strings (
"10" + "20"), Python concatenates (joins) them together โ"1020" - When you add integers (
10 + 20), Python performs mathematical addition โ30
The + operator behaves differently depending on the data type! This is why understanding types is so important.
Bonus: What if you wanted to add those string numbers? Youโd convert them first:
result = int("10") + int("20") # Result: 30๐ฏ Key Takeaways
โจ Quest 2 Complete! โจ
Youโve learned:
โ
Python has different data types: int, float, str, bool
โ
Use type() to check what type a value is
โ
Different types support different operations
โ
You can convert between types using int(), float(), str()
โ
The same operator can behave differently with different types
Next Quest: Ready to make decisions? Try Quest 3: Conditionals!
๐ Try This at Home!
Create a simple โAbout Meโ program using different data types:
# All about you!
name = "Your Name" # string
age = 15 # integer
height = 5.5 # float (in feet)
loves_coding = True # boolean
favorite_number = 42 # integer
# Print everything with their types
print(f"Name: {name} (type: {type(name).__name__})")
print(f"Age: {age} (type: {type(age).__name__})")
# Add more...Experiment with converting types and see what happens!
๐ฑ Keep Going! Youโre building your Python foundation. Each quest makes you stronger! ๐ช