0% found this document useful (0 votes)
4 views8 pages

Python Data Types: Key Concepts Explained

The document provides a series of 10 questions and answers about Python data types, covering topics such as mutability, the difference between strings and lists, dictionary keys, sets, boolean types, numeric types, empty collections, type conversion, mutable default arguments, and sequence operations. Each question is accompanied by a clear explanation and examples to illustrate the concepts. Additionally, the author offers to create a table summarizing Python data types for easier memorization.

Uploaded by

sindidervishi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Python Data Types: Key Concepts Explained

The document provides a series of 10 questions and answers about Python data types, covering topics such as mutability, the difference between strings and lists, dictionary keys, sets, boolean types, numeric types, empty collections, type conversion, mutable default arguments, and sequence operations. Each question is accompanied by a clear explanation and examples to illustrate the concepts. Additionally, the author offers to create a table summarizing Python data types for easier memorization.

Uploaded by

sindidervishi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Perfect! 😄 Let’s go step by step.

I’ll give answers


and examples for all 10 tricky Python data type
questions so it’s easy to understand.

1️⃣ Tuple & Mutability


Question: Explain why a tuple can contain a list or
another mutable object even though tuples are
immutable. Give an example.
Answer:
 Tuples themselves are immutable, meaning you
cannot change which items are in the tuple.
 But the objects inside can be mutable (like
lists), so you can change the content of the list
inside the tuple.
Example:
my_tuple = (1, 2, [3, 4])
my_tuple[2].append(5)
print(my_tuple) # Output: (1, 2, [3, 4, 5])

2️⃣ Strings vs Lists


Question: Why can’t you change a single character
of a string using indexing, but you can do it with a
list?
Answer:
 Strings are immutable, so you cannot change
characters.
 Lists are mutable, so you can change any item.
Example:
s = "hello"
# s[0] = "H" # ❌ Error

lst = ["h", "e", "l", "l", "o"]


lst[0] = "H"
print(lst) # Output: ['H', 'e', 'l', 'l', 'o']

3️⃣ Dictionary Keys


Question: Why must dictionary keys be immutable?
What happens if you try to use a list as a key?
Answer:
 Keys must be immutable because Python uses
them to locate values efficiently.
 Mutable objects like lists can change, which
would break the dictionary lookup.
Example:
my_dict = {[1,2]: "value"} # ❌ Error: unhashable
type: 'list'
my_dict = {(1,2): "value"} # ✅ Works because
tuple is immutable
4️⃣ Set & Uniqueness
Question: If you create a set with duplicate items,
why does Python automatically remove duplicates?
How is this different from a list?
Answer:
 Sets store only unique elements, so duplicates
are removed automatically.
 Lists can have duplicates and keep the order.
Example:
my_set = {1, 2, 2, 3}
print(my_set) # Output: {1, 2, 3}

my_list = [1, 2, 2, 3]
print(my_list) # Output: [1, 2, 2, 3]

5️⃣ Boolean Type


Question: In Python, True and False are instances
of which type? How are they treated in arithmetic?
Answer:
 True and False are of type bool.
 They behave like numbers: True = 1, False = 0.
Example:
print(type(True)) # <class 'bool'>
print(True + True) # Output: 2
print(False + 5) # Output: 5

6️⃣ Numeric Types


Question: If x = 5 and you do x += 1, does Python
change the original integer object 5?
Answer:
 No, integers are immutable.
 x += 1 creates a new integer object 6 and
assigns it to x.
Example:
x=5
print(id(x)) # id of 5
x += 1
print(id(x)) # id of 6 → different

7️⃣ Empty Collections


Question: What is the difference between {}, [], (),
and set()?
Answer:
 {} → empty dictionary
 [] → empty list
 () → empty tuple
 set() → empty set
Example:
print(type({})) # <class 'dict'>
print(type([])) # <class 'list'>
print(type(())) # <class 'tuple'>
print(type(set())) # <class 'set'>

8️⃣ Type Conversion


Question: What happens if you try to convert a list
into a tuple or a string into a list?
Answer:
 Python allows conversion between types.
 Converting creates a new object of the target
type.
Example:
lst = [1, 2, 3]
tpl = tuple(lst)
print(tpl) # Output: (1, 2, 3)

s = "hello"
lst2 = list(s)
print(lst2) # Output: ['h', 'e', 'l', 'l', 'o']
9️⃣ Mutable Default Argument
Question: Why is it dangerous to use a mutable
data type as a default argument in a function?
Answer:
 The same mutable object is shared every time
the function is called.
 Changes persist across calls, which may cause
bugs.
Example:
def add_item(item, my_list=[]):
my_list.append(item)
return my_list

print(add_item(1)) # [1]
print(add_item(2)) # [1, 2] → not expected!
Solution: Use None as default:
def add_item(item, my_list=None):
if my_list is None:
my_list = []
my_list.append(item)
return my_list
10️⃣ Sequence Operations
Question: Can you perform the same operations
(indexing, slicing) on strings, lists, and tuples?
Answer:
 Yes, all sequence types support indexing and
slicing.
 But only lists are mutable; strings and tuples
cannot be changed.
Example:
s = "hello"
lst = [1, 2, 3, 4]
tpl = (5, 6, 7)

print(s[1:4]) # 'ell'
print(lst[1:3]) # [2, 3]
print(tpl[0:2]) # (5, 6)

# lst[0] = 10 ✅ Works
# s[0] = 'H' ❌ Error
# tpl[0] = 10 ❌ Error

If you want, I can also make a super clear table of


all Python data types, showing:
 Type name
 Syntax
 Mutable/Immutable
 Examples
…so you can memorize everything quickly 😄
Do you want me to make that table?

You might also like