Python Data Types
What are Data Types?
-
Data Types define the kind of value a variable can hold in Python.
-
They tell the computer what type of data we are working with, so it knows how to store it in memory and what operations can be done.
Think of data types like different containers in a kitchen:
-
A bottle for liquids (like
float) -
A jar for cookies (like
list) -
A box for books (like
dict)
Python has different data types to store different kinds of information.
Numeric Types
-
int → Whole numbers
age = 21 # int print(age) -
float → Decimal numbers
price = 99.99 # float print(price) -
complex → Numbers with real & imaginary part
num = 2 + 3j # complex print(num)
Text Type (str)
-
Strings are used for text inside quotes.
name = "Harry Potter" print(name)
Boolean Type (bool)
-
Only two values: True or False
is_student = True passed = False print(is_student, passed)
Collection Types
-
list → Ordered, changeable, allows duplicates
fruits = ["apple", "banana", "cherry"] print(fruits[0]) # apple -
tuple → Ordered, unchangeable, allows duplicates
numbers = (1, 2, 3) print(numbers[1]) # 2 -
dict → Key-value pairs
student = {"name": "Ali", "age": 20} print(student["name"]) # Ali -
set → Unordered, unique values
colors = {"red", "blue", "green"} print(colors)
Key Takeaway
-
Numbers → int, float, complex
-
Text → str
-
True/False → bool
-
Collections → list, tuple, dict, set
