Interview #439: Python - What are data types?

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:

  • You don’t need to explicitly declare a variable’s type.
  • The type is assigned automatically at runtime.

x = 10        # int
y = "hello"   # str
z = 3.14      # float        

📦 Why Data Types Matter

Understanding data types is crucial because:

  • They control memory usage
  • They define valid operations
  • They affect performance
  • They help avoid runtime errors


🔢 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:

  • Immutable (cannot change after creation)
  • Supports indexing & slicing

print(name[0])   # K        

🔹 list

Ordered, mutable collection

my_list = [1, 2, 3, "hello"]        

Key features:

  • Allows duplicates
  • Can store mixed data types
  • Mutable (can modify)

my_list.append(4)        

🔹 tuple

Ordered, immutable collection

my_tuple = (1, 2, 3)        

Key features:

  • Faster than lists
  • Cannot be modified


🔹 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:

  • No duplicates
  • Unordered
  • Mutable


🔹 frozenset

Immutable version of set

fs = frozenset([1, 2, 3])        

4️⃣ Mapping Type


🔹 dict (Dictionary)

Stores data in key-value pairs

person = {
    "name": "Kanchan",
    "age": 30
}        

Key features:

  • Keys must be unique
  • Mutable
  • Fast lookups


5️⃣ Boolean Type


🔹 bool

Represents True or False

is_active = True        

Used in:

  • Conditions
  • Comparisons

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:

  • Default values
  • Placeholder for missing data


🔄 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:

  • int
  • float
  • str
  • tuple
  • frozenset


🔓 Mutable Types

Can be modified:

  • list
  • dict
  • set
  • bytearray


🚀 Practical Examples (Automation Perspective)


Example 1: API Response Handling

response = {
    "status": 200,
    "data": ["user1", "user2"]
}        

  • dict → response
  • list → data
  • int → status


Example 2: Test Data Storage

test_data = ("admin", "password123")        

Tuple used because:

  • Data should not change


Example 3: Unique Values Validation

ids = {101, 102, 103}        

Set ensures:

  • No duplicate IDs


🎯 Interview Tips (Very Important)

🔹 Common Questions

Q: Is Python strongly typed? 👉 Yes (type matters), but dynamically typed.

Q: Difference between list and tuple?

Article content

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:

  • Write cleaner code
  • Avoid bugs
  • Design better automation frameworks
  • Handle API/UI data effectively

Article content


To view or add a comment, sign in

More articles by Software Testing Studio | WhatsApp 91-6232667387

Others also viewed

Explore content categories