Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! π
Data Types (str, int, float, bool)
Python has several built-in data types. The four most common are: str (text), int (whole numbers), float (decimal numbers), and bool (True/False). Understanding types is essential for writing correct programs.
Python Data Types
- str - Text: 'Hello', "World", '''multiline'''
- int - Whole numbers: 42, -10, 0, 1_000_000
- float - Decimal numbers: 3.14, -0.5, 1.0
- bool - True or False (capital T and F)
- NoneType - None (represents 'no value')
- type() - Check the type of any variable
- Python is dynamically typed - variables can change type
Data Types Code
# String
name = "Alice"
greeting = 'Hello'
multiline = """This is
a multiline string"""
# Integer
age = 25
negative = -10
big_number = 1_000_000 # underscores for readability
# Float
price = 9.99
pi = 3.14159
# Boolean
is_active = True
is_deleted = False
# NoneType
result = None
# Check types with type()
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(is_active)) # <class 'bool'>
print(type(result)) # <class 'NoneType'>
# f-strings for formatted output
print(f"{name} is {age} years old")Tip
Tip
Use type() to check any variable's type during debugging. Also use isinstance(value, int) for type checking in production code - it's more robust and handles inheritance.
Everything in Python is an object - use type() to check
Common Mistake
Warning
Floating point math is imprecise: 0.1 + 0.2 == 0.30000000000000004, not 0.3. For financial calculations, use the decimal module: from decimal import Decimal.
Practice Task
Note
Explore data types: (1) Create one variable of each type (str, int, float, bool, None). (2) Use type() on each. (3) Try 0.1 + 0.2 and observe the result. (4) Test bool() on 0, '', [], None.
Quick Quiz
Key Takeaways
- Python has several built-in data types.
- str - Text: 'Hello', "World", '''multiline'''
- int - Whole numbers: 42, -10, 0, 1_000_000
- float - Decimal numbers: 3.14, -0.5, 1.0