Tuple in Python
Tuple in Python
Tuples consists of a number of values separated by comma and enclosed within parentheses. Tuple is similar to list, values in a list can be changed but not in a tuple.
The term Tuple is originated from the Latin word represents an abstraction of the sequence of numbers:
single(1), double(2), triple(3), quadruple(4), quintuple(5), sextuple(6), septuple(7), octuple(8), ..., n‑tuple, ...,
Advantages of Tuples over list
1. The elements of a list are changeable (mutable) whereas the elements of a tuple are unchangeable (immutable), this is the key difference between tuples and list.
2. The elements of a list are enclosed within square brackets. But, the elements of a tuple are enclosed by paranthesis.
3. Iterating tuples is faster than list.
Creating Tuples
Creating tuples is similar to list. In a list, elements are defined within square brackets, whereas in tuples, they may be enclosed by parenthesis. The elements of a tuple can be even defined without parenthesis. Whether the elements defined within parenthesis or without parenthesis, there is no differente in it's function.
Syntax:
# Empty tuple
Tuple_Name = ( )
# Tuple with n number elements
Tuple_Name = (E1, E2, E2 ……. En)
# Elements of a tuple without parenthesis
Tuple_Name = E1, E2, E3 ….. En
Example
>>> MyTup1 = (23, 56, 89, 'A', 'E', 'I', "Tamil")
>>> print(MyTup1)
(23, 56, 89, 'A', 'E', 'I', 'Tamil')
>>> MyTup2 = 23, 56, 89, 'A', 'E', 'I', "Tamil"
>>> print (MyTup2)
(23, 56, 89, 'A', 'E', 'I', 'Tamil')
(i) Creating tuples using tuple( ) function
The tuple( ) function is used to create Tuples from a list. When you create a tuple, from a list, the elements should be enclosed within square brackets.
Syntax:
Tuple_Name = tuple( [list elements] )
Example
>>> MyTup3 = tuple( [23, 45, 90] )
>>> print(MyTup3)
(23, 45, 90)
>>> type (MyTup3)
<class ‘tuple’>
(ii) Creating Single element tuple
While creating a tuple with a single element, add a comma at the end of the element. In the absence of a comma, Python will consider the element as an ordinary data type; not a tuple. Creating a Tuple with one element is called “Singleton” tuple.
Example
>>> MyTup4 = (10)
>>> type(MyTup4)
<class 'int'>
>>> MyTup5 = (10,)
>>> type(MyTup5)
<class 'tuple'>
Accessing values in a Tuple
Like list, each element of tuple has an index number starting from zero. The elements of a tuple can be easily accessed by using index number.
Example
>>> Tup1 = (12, 78, 91, “Tamil”, “Telugu”, 3.14, 69.48)
# to access all the elements of a tuple
>>> print(Tup1)
(12, 78, 91, 'Tamil', 'Telugu', 3.14, 69.48)
#accessing selected elements using indices
>>> print(Tup1[2:5])
(91, 'Tamil', 'Telugu')
#accessing from the first element up to the specified index value
>>> print(Tup1[:5])
(12, 78, 91, 'Tamil', 'Telugu')
# accessing from the specified element up to the last element.
>>> print(Tup1[4:])
('Telugu', 3.14, 69.48)
# accessing from the first element to the last element
>>> print(Tup1[:])
(12, 78, 91, 'Tamil', 'Telugu', 3.14, 69.48)
Update and Delete Tuple
As you know a tuple is immutable, the elements in a tuple cannot be changed. Instead of altering values in a tuple, joining two tuples or deleting the entire tuple is possible.
Example
# Program to join two tuples
Tup1 = (2,4,6,8,10)
Tup2 = (1,3,5,7,9)
Tup3 = Tup1 + Tup2
print(Tup3)
Output
(2, 4, 6, 8, 10, 1, 3, 5, 7, 9)
To delete an entire tuple, the del command can be used.
Syntax:
del tuple_name
Example
Tup1 = (2,4,6,8,10)
print("The elements of Tup1 is ", Tup1)
del Tup1
print (Tup1)
Output:
The elements of Tup1 is (2, 4, 6, 8, 10)
Traceback (most recent call last):
File "D:/Python/Tuple Examp 1.py", line 4, in <module>
print (Tup1)
NameError: name 'Tup1' is not defined
Tuple Assignment
Tuple assignment is a powerful feature in Python. It allows a tuple variable on the left of the assignment operator to be assigned to the values on the right side of the assignment operator. Each value is assigned to its respective variable.
Example
>>> (a, b, c) = (34, 90, 76)
>>> print(a,b,c)
34 90 76
# expression are evaluated before assignment
>>> (x, y, z, p) = (2**2, 5/3+4, 15%2, 34>65)
>>> print(x,y,z,p)
4 5.666666666666667 1 False
Note that, when you assign values to a tuple, ensure that the number of values on both sides of the assignment operator are same; otherwise, an error is generated by Python.
Returning multiple values in Tuples
A function can return only one value at a time, but Python returns more than one value from a function. Python groups multiple values and returns them together.
Example : Program to return the maximum as well as minimum values in a list
def Min_Max(n):
a = max(n)
b = min(n)
return(a, b)
Num = (12, 65, 84, 1, 18, 85, 99)
(Max_Num, Min_Num) = Min_Max(Num)
print("Maximum value = ", Max_Num)
print("Minimum value = ", Min_Num)
Output:
Maximum value = 99
Minimum value = 1
Nested Tuples
In Python, a tuple can be defined inside another tuple; called Nested tuple. In a nested tuple, each tuple is considered as an element. The for loop will be useful to access all the elements in a nested tuple.
Example
Toppers = (("Vinodini", "XII-F", 98.7), ("Soundarya", "XII-H", 97.5),
("Tharani", "XII-F", 95.3), ("Saisri", "XII-G", 93.8))
for i in Toppers:
print(i)
Output:
('Vinodini', 'XII-F', 98.7)
('Soundarya', 'XII-H', 97.5)
('Tharani', 'XII-F', 95.3)
('Saisri', 'XII-G', 93.8)
- (1,3)
- (0,3)
- (1,0)
- (3,1)
- [1, 2, 3]
- (1, 2, 3)
- {1, 2, 3}
- {}
- print(t[3])
- t[3] = 45
- print(max(t))
- print(len(t))
What will be the output of the following Python code?
-
>>>t=(1,2,4,3)
-
>>>t[1:3]
- (1, 2)
- (1, 2, 4)
- (2, 4)
- (2, 4, 3)
What will be the output of the following Python code?
-
>>>t=(1,2,4,3)
-
>>>t[1:-1]
- (1, 2)
- (1, 2, 4)
- (2, 4)
- (2, 4, 3)
What will be the output of the following Python code?
-
>>>t = (1, 2, 4, 3, 8, 9)
-
>>>[t[i] for i in range(0, len(t), 2)]
- [2, 3, 9]
- [1, 2, 4, 3, 8, 9]
- [1, 4, 8]
- (1, 4, 8)
What will be the output of the following Python code?
-
d = {"john":40, "peter":45}
-
d["john"]
- 40
- 45
- “john”
- “peter”
What will be the output of the following Python code?
-
>>>t = (1, 2)
-
>>>2 * t
- (1, 2, 1, 2)
- [1, 2, 1, 2]
- (1, 1, 2, 2)
- [1, 1, 2, 2]
What will be the output of the following Python code?
-
>>>t1 = (1, 2, 4, 3)
-
>>>t2 = (1, 2, 3, 4)
-
>>>t1 < t2
- True
- False
- Error
- None
What will be the output of the following Python code?
-
>>>my_tuple = (1, 2, 3, 4)
-
>>>my_tuple.append( (5, 6, 7) )
-
>>>print len(my_tuple)
- 1
- 2
- 5
- Error
What will be the output of the following Python code?
-
numberGames = {}
-
numberGames[(1,2,4)] = 8
-
numberGames[(4,2,1)] = 10
-
numberGames[(1,2)] = 12
-
sum = 0
-
for k in numberGames:
-
sum += numberGames[k]
-
print len(numberGames) + sum
- 30
- 24
- 33
- 12
- Tuple
- Integer
- List
- Both tuple and integer
- Error, tuple slicing doesn’t exist
- [2,3]
- (2,3,4)
- (2,3)
What will be the output of the following Python code?
>>> a=(1,2,(4,5)) >>> b=(1,2,(3,4)) >>> a<b
- False
- True
- Error, < operator is not valid for tuples
- Error, < operator is valid for tuples but not if there are sub-tuples
What will be the output of the following Python code?
>>> a=("Check")*3 >>> a
- (‘Check’,’Check’,’Check’)
- * Operator not valid for tuples
- (‘CheckCheckCheck’)
- Syntax error
What will be the output of the following Python code?
>>> a=(2,3,4) >>> sum(a,3)
- Too many arguments for sum() method
- The method sum() doesn’t exist for tuples
- 12
- 9
Is the following Python code valid?
>>> a=(1,2,3,4) >>> del a
- No because tuple is immutable
- Yes, first element in the tuple is deleted
- Yes, the entire tuple is deleted
- No, invalid syntax for del method
- Array of tuples
- List of tuples
- Tuples of lists
- Invalid type
What will be the output of the following Python code?
>>> a=(0,1,2,3,4) >>> b=slice(0,2) >>> a[b]
- Invalid syntax for slicing
- [0,2]
- (0,1)
- (0,2)
Is the following Python code valid?
>>> a,b,c=1,2,3 >>> a,b,c
- Yes, [1,2,3] is printed
- No, invalid syntax
- Yes, (1,2,3) is printed
- 1 is printed
Is the following Python code valid?
>>> a,b=1,2,3
- Yes, this is an example of tuple unpacking. a=1 and b=2
- Yes, this is an example of tuple unpacking. a=(1,2) and b=3
- No, too many values to unpack
- Yes, this is an example of tuple unpacking. a=1 and b=(2,3)
What will be the output of the following Python code?
>>> a=(1,2) >>> b=(3,4) >>> c=a+b >>> c
- (4,6)
- (1,2,3,4)
- Error as tuples are immutable
- None
What will be the output of the following Python code?
>>> a,b=6,7 >>> a,b=b,a >>> a,b
- (6,7)
- Invalid syntax
- (7,6)
- Nothing is printed
What will be the output of the following Python code?
>>> import collections >>> a=collections.namedtuple('a',['i','j']) >>> obj=a(i=4,j=7) >>> obj
- a(i=4, j=7)
- obj(i=4, j=7)
- (4,7)
- An exception is thrown
Is the following Python code valid?
>>> a=2,3,4,5 >>> a
- Yes, 2 is printed
- Yes, [2,3,4,5] is printed
- No, too many values to unpack
- Yes, (2,3,4,5) is printed
- [(1, 2), (2, 4), (3, 9)]
- [(2,4),(1,2),(3,9)]
- Error because tuples are immutable
- Error, tuple has no sort attribute
- (‘D’, ‘H’, ‘U’)
- (‘A’, ‘D’, ‘H’,’U’,’N’,’I’,’C’)
- (‘D’,’H’,’U’,’N’,’I’,’C’)
- (‘D’,’H’,’U’,’N’,’I’,’C’)
- (2 , 4), (4 , 5)
- (3 , 9)
- (2, 4, 1, 5)
- Invalid Syntax
- [4,5, 6
- [7]
- [1, 2,3, 4]
- 7
- error
- 6
- 1
- 3
- list
- set
- tuple
- dict
- (5,1,6,2)
- (5,1,7,6)
- (5,1,7,6,2)
- Error
- In Python, a tuple can contain only integers as its elements.
- In Python, a tuple can contain only strings as its elements.
- In Python, a tuple can contain both integers and strings as its elements.
- In Python, a tuple can contain either string or integer but not both at a time.
- (3,)
- (2, )
- (1,)
- (0,)
- Yes, list mutable and tuple immutable
- No, list and tuple both are mutable
- No, list and tuple both are in immutable
- No, just opposite, list immutable and tuple mutable
- List of tuple
- Tuple of List
- Array of Tuples
- Invalid Type
- Tuples have structure; lists have an order
- Tuples are homogeneous, lists are heterogeneous.
- Tuples are immutable, lists are mutable.
- All of them