π Lesson: Understanding Python Data Types
Data types are the foundation of any programming language they tell Python what kind of data itβs working with and how to handle it. In Python, every piece of data is an object, and every object has a type.
Python is dynamically typed, meaning:
- You donβt need to declare a variableβs type ahead of time.
- The type is determined when the program runs.
age = 5 # int
pi = 3.14 # float
name = "Alice" # str
Core Data Types
int
β Integer
Whole numbers like 10
, -3
, or 0
.
score = 100
year = 2025
float
β Floating Point Number
Numbers with decimal points.
price = 19.99
temperature = -3.5
str
β String
Text data enclosed in quotes.
message = "Hello, Python!"
initial = 'A'
bool
β Boolean
True or False values used in logic.
is_logged_in = True
is_complete = False
Collection Data Types
list
β Ordered, Mutable Collection
colors = ["red", "green", "blue"]
colors.append("yellow")
tuple
β Ordered, Immutable Collection
dimensions = (1920, 1080)
dict
β Dictionary (Key-Value Pairs)
person = {"name": "Bob", "age": 30}
print(person["name"])
set
β Unordered, Unique Items
unique_ids = {1, 2, 3, 2}
print(unique_ids) # {1, 2, 3}
Type Checking and Conversion
type(3.14) # <class 'float'>
str(42) # "42"
int("10") # 10
bool("") # False
π€ Why Data Types Matter
- They prevent bugs (e.g., trying to add a string to an integer).
- They make your code easier to read and understand.
- They enable Python to optimize performance behind the scenes.
π§ͺ Quick Challenge!
a = "5"
b = 3
print(a * b) # What does this output?
Answer: "555"
β the string "5" is repeated 3 times!