Imagine Python as a bustling city, and data types are the citizens living inside it.
Each citizen behaves differently:
- Some are simple and predictable π€
- Some store many things at once π§Ί
- Some are strict, others flexible
- Some donβt like duplicates
- Some come in pairs
In this chapter, youβll meet Pythonβs most important data types β the true stars of your code.
1. Introduction to Data Types
A data type tells Python what kind of value you’re working with.
Is it a number?
Text?
A list of items?
A yes/no answer?
Every value in Python has a type, even if you don’t explicitly specify it.
x = 10
print(type(x))
Python replies:
<class 'int'>
2. Why Are Data Types Important?
Great question!
Because they help Python:
β Understand what operations are allowed
You can add numbers but not mix numbers and paragraphs:
10 + 5 # Works
"hello" + 3 # Nope
β Use memory efficiently
An integer takes less space than a list.
β Prevent confusion
Itβs like labeling boxes in storage β otherwise chaos.
β Build predictable programs
If data types are wrong, bugs appear like unexpected guests at a party.
3. Basic Data Types in Python
Time to meet the simplest personalities!
3.1 Integers (The Whole Number Squad)
Integers are whole numbers β no decimals, no fractions.
score = 250
temperature = -5
year = 2025
Theyβre great for counting, indexing, or doing math.
3.2 Floating-Point Numbers (The Decimal Gang)
Floats are numbers with a decimal point.
rating = 4.9
pi = 3.14159
height = 5.8
Python handles decimals quite well, though sometimes floating-point math can get quirky:
0.1 + 0.2 # Surprise!
3.3 Strings (The Text Storytellers)
Strings store text β anything inside quotes:
name = "Amit Sharma"
message = "Hello Python!"
You can:
- Combine strings
- Slice them
- Repeat them
- Measure them
Strings are fun and incredibly powerful.
3.4 Booleans (True or False Heroes)
Booleans answer simple questions:
is_logged_in = True
is_admin = False
Python also treats things like this:
0β False""β False[]β False- Everything else β True
Booleans power conditions, decisions, and flow control.
4. Advanced Data Types (The Superheroes!)
These types can hold multiple values at once β like containers or collections.
4.1 Lists β The Flexible Backpack π
Lists can store multiple items, and they can be of mixed types:
shopping_list = ["Milk", "Eggs", 10, True]
You can:
- Add
- Remove
- Change
- Sort
Lists are mutable: you can modify them anytime.
4.2 Tuples β The Locked Suitcase π
Tuples look like lists, but once created, you canβt change them.
coordinates = (28.61, 77.23)
Why use tuples?
- Theyβre faster
- Theyβre more secure
- Useful for fixed data
4.3 Sets β The Unique Collection π’
Sets store unique items β no duplicates allowed.
unique_colors = {"red", "blue", "green", "red"}
print(unique_colors) # 'red' appears only once
Great for:
- Removing duplicates
- Membership checks
- Math operations (union, intersection)
4.4 Dictionaries β The Smart Phonebook π
Dictionaries store things in key-value pairs:
person = {
"name": "Amit Sharma",
"age": 30,
"email": "amit@example.com"
}
Access values using keys:
print(person["name"])
Dictionaries are incredibly useful for real-world data structures.
5. Type Conversion (Changing Personalities!)
Python lets you convert from one type to another β but only when it makes sense.
β String β Integer
age = "30"
age_number = int(age)
β Integer β String
msg = "You are " + str(age_number)
β Float β Integer
value = 9.8
whole = int(value) # 9
β List β Set
items = [1, 2, 2, 3]
unique_items = set(items)
β Integer β Float
x = 5
y = float(x) # 5.0
β οΈ Beware: converting an invalid value causes an error:
x = "hello"
int(x) # ValueError
6. Practical Examples
Here are some fun, real-world examples:
Example 1: Calculate Expenses
food = 120.50
transport = 50
misc = 30.25
total = food + transport + misc
print("Total expenses:", total)
Example 2: Count Unique Visitors
visitors = ["Amit", "Sunita", "Amit", "Raj"]
unique_visitors = set(visitors)
print("Unique visitors:", len(unique_visitors))
Example 3: Store User Profile
user = {
"name": "Amit Sharma",
"age": 30,
"is_verified": True,
}
print(user["name"])
Example 4: Convert String Input
num = "42"
result = int(num) * 2
print(result) # 84
Example 5: Tuple for Coordinates
location = (19.07, 72.87)
print("Latitude:", location[0])
π Wrap-up
By now, you understand:
β What data types are
β Why they matter
β Basic types: int, float, string, bool
β Advanced types: list, tuple, set, dict
β How to convert between them
β Practical examples
You’re now ready for the next chapter:
π 1.3 β Operators in Python!

Leave a Reply