Q.Example 10.1: Create tuples of different kinds and display them — tuple1, a tuple of integers; tuple2, a tuple of mixed data types; tuple3, a tuple with a list as an element; tuple4, a tuple with a tuple as an element. Also show the incorrect and correct way of assigning a single element to a tuple (element followed by a comma), and that a comma-separated sequence without parentheses is treated as a tuple by default.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Demonstrates the four kinds of tuples (integers, mixed, list-inside, tuple-inside) plus the two classic rules: a one-element tuple needs a trailing comma, and bare comma-separated values form a tuple automatically.
The idea. A tuple is an ordered, immutable sequence that places no restriction on element types — numbers, strings, lists, even other tuples can sit side by side. What actually constructs the tuple is the comma; the parentheses are mostly for readability. That single fact explains both special cases in this example: (20) has no comma (so it is just the number 20), and 1, 2, 3 has commas (so it is a tuple even without parentheses).
Program:
tuple1 = (2, 4, 6, 8, 10) # tuple of integers
print(tuple1)
tuple2 = ('Economics', 87, 'Accountancy', 89.6) # tuple of mixed data types
print(tuple2)
tuple3 = (10, 20, 30, [40, 50]) # tuple with a list as an element
print(tuple3)
tuple4 = (1, 2, 3, (4, 5)) # tuple with a tuple as an element
print(tuple4)
tuple5 = (20) # INCORRECT way - this is just an integer
print(type(tuple5))
tuple6 = (20,) # CORRECT way - element followed by a comma
print(type(tuple6))
seq = 1, 2, 3 # a comma separated sequence is a tuple by default
print(seq)
print(type(seq))
Verified output:
(2, 4, 6, 8, 10)
('Economics', 87, 'Accountancy', 89.6)
(10, 20, 30, [40, 50])
(1, 2, 3, (4, 5))
<class 'int'>
<class 'tuple'>
(1, 2, 3)
<class 'tuple'>
Line by line:
tuple1— homogeneous integers;tuple2— mixed str/int/float in one sequence.tuple3— a list inside a tuple: the tuple itself stays immutable, but that inner list can still be mutated (tuple3[3].append(60)is legal!). …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.