Logical Exercises
These exercises focus on deep understanding of Python data types, implicit behavior, type casting, mutability, and edge cases.
Think before running the code.
Exercise 1: Predict the Output (Type Confusion)
python
x = "10"
y = 20
z = x * y
print(z)
print(type(z))
Exercise 2: Boolean Truth Table (Hidden Rules)
python
values = [0, 1, "", " ", [], [1], None]
for v in values:
print(v, "=>", bool(v))
Exercise 3: Mutable vs Immutable (Critical Concept)
python
a = [1, 2, 3]
b = a
b.append(4)
print(a)
print(b)
Exercise 4: Tuple Trap
python
t = (1, 2, [3, 4])
t[2].append(5)
print(t)
Exercise 5: Same Value, Different Type
python
a = 10
b = 10.0
c = "10"
print(a == b)
print(a == c)
print(type(a) == type(b))
Exercise 6: Dictionary Key Collision
python
data = {
1: "int",
1.0: "float",
True: "bool"
}
print(data)
print(len(data))
Exercise 7: Set with Mixed Data Types
python
s = {1, 1.0, True, "1"}
print(s)
print(len(s))
Exercise 8: Type Casting Chain
python
x = bool("False")
y = int(x)
z = str(y)
print(x, y, z)
print(type(z))
Exercise 9: Range vs List
python
r = range(5)
l = list(r)
print(type(r))
print(type(l))
print(r == l)
Exercise 10: Nested Type Conversion
python
x = list(set([1, 2, 2, 3, 4, 4]))
x.sort()
print(x)
Exercise 11: Bytes vs String
python
a = "Python"
b = a.encode()
print(a == b)
print(type(a))
print(type(b))
Exercise 12: NoneType Logic
python
x = None
if x:
print("True")
else:
print("False")
Exercise 13: List Multiplication Trap
python
matrix = [[0]] * 3
matrix[0][0] = 1
print(matrix)
Exercise 14: Float Precision Problem
python
x = 0.1 + 0.2
print(x == 0.3)
print(x)
Exercise 15: Identity vs Equality
python
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b)
print(a is b)
Exercise 16: Complex Data Type Check
python
x = complex(0)
print(bool(x))
Exercise 17: String to List Logic
python
x = list("123")
y = list(map(int, x))
print(x)
print(y)
Exercise 18: Frozen Data Type Logic
python
s = frozenset([1, 2, 3])
# s.add(4)
What happens and why?
Exercise 19: Dict from Zip (Length Mismatch)
python
keys = ["a", "b", "c"]
values = [1, 2]
d = dict(zip(keys, values))
print(d)
Exercise 20: Final
python
x = [1, 2, 3]
y = tuple(x)
z = set(y)
print(x == list(y))
print(y == tuple(z))
print(type(z))
If you can explain WHY each output behaves that way (not just the output),
your data type fundamentals are strong.