Python Data Types — Complete Beginner-to-Interview Guide
Introduction
Python is a dynamically typed language, meaning:
-
You don’t need to declare data types explicitly
-
The type is decided at runtime
Example:
Python provides built-in data types to store and manipulate different kinds of data efficiently.
Categories of Python Data Types
Python data types are commonly grouped into:
-
Numeric Types
-
Text Type
-
Sequence Types
-
Set Types
-
Mapping Type
-
Boolean Type
-
None Type
1️⃣ Numeric Data Types
a) int (Integer)
Stores whole numbers (positive, negative, zero).
Retrieve / Use
b) float
Stores decimal numbers.
c) complex
Stores complex numbers.
2️⃣ Text Data Type — str
Stores text (immutable).
Access characters
String operations
3️⃣ Sequence Data Types
a) list — Ordered & Mutable
Used when:
-
Data can change
-
Order matters
Access
Add
Update
Remove
b) tuple — Ordered & Immutable
Used when:
-
Data should not change
⚠️ Cannot modify:
c) range
Used for sequences of numbers.
4️⃣ Set Data Types
a) set — Unordered & Unique
Used when:
-
You want unique values
-
Order doesn’t matter
Add / Remove
b) frozenset — Immutable Set
Cannot modify:
5️⃣ Mapping Data Type — dict
Stores key–value pairs.
Access
Add / Update
Remove
Iterate
6️⃣ Boolean Data Type — bool
Stores True or False.
Used in conditions:
7️⃣ None Data Type — None
Represents absence of value.
8️⃣ Checking Data Types
Use type():
Use isinstance() (recommended):
9️⃣ Mutable vs Immutable (Very Important)
| Type | Mutable |
|---|---|
| int, float, str | ❌ |
| list, dict, set | ✅ |
| tuple, frozenset | ❌ |
Example:
🔟 Common Interview Examples
Store user info
Store multiple users
Unique values
1️⃣1️⃣ Summary Table
| Data Type | Use Case |
|---|---|
| int | Whole numbers |
| float | Decimal values |
| str | Text |
| list | Ordered, changeable data |
| tuple | Fixed data |
| set | Unique values |
| dict | Key–value data |
| bool | Conditions |
| None | No value |
1️⃣2️⃣ Interview One-Line Summary ⭐
Python data types define how data is stored and manipulated, ranging from numeric and text types to collections like lists, sets, and dictionaries, with mutable and immutable behaviors.
No comments:
Post a Comment