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:

  1. Change health to 100.0 - what type is it now?
  2. Change player_name to 12345 (without quotes) - what happens?
  3. 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 int

Use 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! ๐Ÿ’ช