Interview #439: Python - What are data types?
In Python, data types define the kind of value a variable can hold and determine what operations can be performed on that data.
Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-6232667387
Unlike statically typed languages (like Java), Python is dynamically typed, meaning:
x = 10 # int
y = "hello" # str
z = 3.14 # float
📦 Why Data Types Matter
Understanding data types is crucial because:
🔢 Main Categories of Python Data Types
Python has several built-in data types, broadly categorized as:
1. Numeric Types
2. Sequence Types
3. Set Types
4. Mapping Type
5. Boolean Type
6. Binary Types
7. None Type
Let’s explore each in detail.
1️⃣ Numeric Data Types
Used to store numerical values.
🔹 int (Integer)
Whole numbers (positive or negative)
a = 10
b = -5
🔹 float (Floating Point)
Decimal numbers
x = 3.14
y = -0.99
🔹 complex
Numbers with real and imaginary parts
z = 2 + 3j
2️⃣ Sequence Data Types
Used to store ordered collections of items.
🔹 str (String)
A sequence of characters (immutable)
name = "Kanchan"
Key points:
print(name[0]) # K
🔹 list
Ordered, mutable collection
my_list = [1, 2, 3, "hello"]
Key features:
my_list.append(4)
🔹 tuple
Ordered, immutable collection
my_tuple = (1, 2, 3)
Key features:
🔹 range
Represents a sequence of numbers
r = range(1, 5)
3️⃣ Set Data Types
Used for unordered collections of unique elements.
🔹 set
s = {1, 2, 3, 3}
Output:
{1, 2, 3}
Key features:
🔹 frozenset
Immutable version of set
fs = frozenset([1, 2, 3])
4️⃣ Mapping Type
🔹 dict (Dictionary)
Stores data in key-value pairs
Recommended by LinkedIn
person = {
"name": "Kanchan",
"age": 30
}
Key features:
5️⃣ Boolean Type
🔹 bool
Represents True or False
is_active = True
Used in:
print(10 > 5) # True
6️⃣ Binary Data Types
Used for handling binary data.
🔹 bytes (immutable)
b = b"hello"
🔹 bytearray (mutable)
ba = bytearray(5)
🔹 memoryview
Used for memory-efficient data handling
mv = memoryview(b"abc")
7️⃣ None Type
🔹 NoneType
Represents absence of value
x = None
Common use cases:
🔄 Type Checking & Conversion
✅ Check Data Type
x = 10
print(type(x)) # <class 'int'>
🔁 Type Casting
Convert one type to another:
int("10") # 10
float(5) # 5.0
str(100) # "100"
⚡ Mutable vs Immutable Types
🔒 Immutable Types
Cannot be changed after creation:
🔓 Mutable Types
Can be modified:
🚀 Practical Examples (Automation Perspective)
Example 1: API Response Handling
response = {
"status": 200,
"data": ["user1", "user2"]
}
Example 2: Test Data Storage
test_data = ("admin", "password123")
Tuple used because:
Example 3: Unique Values Validation
ids = {101, 102, 103}
Set ensures:
🎯 Interview Tips (Very Important)
🔹 Common Questions
Q: Is Python strongly typed? 👉 Yes (type matters), but dynamically typed.
Q: Difference between list and tuple?
Q: Why use set? 👉 To remove duplicates and perform fast membership checks.
Q: What is None? 👉 Represents absence of value (not 0, not empty string).
🧩 Final Thoughts
Python data types are simple but powerful. Mastering them helps you: