Lesson 10/48 ยท ๐Ÿ Your First Python
๐Ÿ Your First PythonLesson 10/48
Phase 1 ยท Your First Python20 min

Types & Type Conversion

Why "5" + 3 crashes Python, and how to fix it

Every value in Python has a type, a category that tells Python what kind of data it is and what you can do with it.
Think of it like storage containers: a jar holds liquid, a box holds solid items. You wouldn't pour water into a box. Python enforces similar rules about its data.
The 4 basic types you'll use constantly:
  • int, whole numbers: 5, -3, 100, 0
  • float, decimal numbers: 3.14, -0.5, 2.0
  • str, text (always in quotes): "hello", 'Python', "42"
  • bool, True or False (exactly those, capital T/F): True, False

  • Notice: 42 is an int, but "42" is a str. The quotes make all the difference.
    Checking types with type()python
    print(type(42))        # <class 'int'>
    print(type(3.14))      # <class 'float'>
    print(type("hello"))   # <class 'str'>
    print(type(True))      # <class 'bool'>
    print(type("42"))      # <class 'str'>  โ† not an int!
    The #1 beginner error: mixing types
    ```pythonage = "25"print("In 5 years you'll be " + age + 5)`
    This crashes with: TypeError: can only concatenate str (not "int") to str
    The problem: age is a str ("25"), and 5 is an int. Python doesn't know how to combine them with +. You must convert first.
    Type conversion, turning one type into another:
  • int("42") โ†’ converts the string "42" to the integer 42
  • float("3.14") โ†’ converts to the float 3.14
  • str(42) โ†’ converts the integer 42 to the string "42"
  • bool(0) โ†’ converts to False (0 and "" and [] are falsy)
  • Fixing the type errorpython
    age = "25"    # age is a string
    years = 5     # years is an int
    
    # Wrong: "25" + 5 crashes
    # Right: convert age to int first
    future_age = int(age) + years
    print("In 5 years you'll be " + str(future_age))
    # โ†’ In 5 years you'll be 30
    When to convert:
  • Use int() or float() when you need to do math with something that's stored as a string
  • Use str() when you need to concatenate a number into a string message
  • Use type() when you're confused about what type a variable is
  • ๐Ÿค”Quick Check

    What type is the value "100"?

    ๐Ÿค”Quick Check

    Which line will cause a TypeError?

    Practice Exercises

    0/3 solved
    Exercise 1 of 3easyGuided
    โฑ 00:00

    What's My Type?

    Print the type of each of these values on a separate line:- 42- 3.7- "programming"- False
    Expected output:<class 'int'><class 'float'><class 'str'><class 'bool'>
    solution.py
    1 / 3
    Exercise 2 of 3easy
    โฑ 00:00

    Fix the TypeError

    The code below crashes with a TypeError. Fix it so it prints:You are 20 years old.
    solution.py
    2 / 3
    Exercise 3 of 3easy
    โฑ 00:00

    Price Calculator

    You have two prices stored as strings (from a database).Convert them to floats, add them, then print the total.
    price1 = "19.99"price2 = "34.50"
    Expected output:54.49
    solution.py
    3 / 3
    Solve all 3 exercises to unlock completion