List
List
▪ Tuples are the sequence or series values of different types separated by commas
(,) and enclosed in paranthesis().
▪ Just like strings and lists, values in tuples can also be accesed by their index
values, which are integers starting from 0.
▪ Major differences from lists are
▪ Immutable
▪ Enclosed in parentheses
▪ A tuple with a single element must have a comma inside the parentheses:
a = (11,)
▪ >>> mytuple = (11, 22, 33)
▪ >>> mytuple[0]
11
▪ >>> mytuple[-1]
33
▪ Method -1 without constructor
▪ # empty tuple Method-2 using tuple constructor
▪ tup= () # empty tuple
▪ # tuple of integers my_tup = tuple()
▪ my_tup = (1, 2, 3) # tuple of integers
▪ # tuple with mixed datatypes my_tup = tuple((1, 2, 3))
▪ my_tup = (1, "Hello", 3.4)
▪ # nested tuple
▪ my_tup = (“welcome", (8, 4, 6))
▪ Tuple can also be created without parenthesis
▪ >>>tuple =4.9,6,’house’
▪ >>>print(tuple)
▪ (4.9, 6, ‘house’)
▪ Using square brackets
▪ >>>tup1 = (‘Physics’,’chemistry’,’mathematics’)
▪ >>>tup2 = (10,20,30,40,50)
▪ >>>print(tup1[1])
▪ Chemistry # Output
▪ >>>print(tup2[4])
▪ 50 # Output
▪ >>>print (tup1[-2])
▪ chemistry # Output
▪ Using slicing
▪ In order to print the continuous values in a tuple slicing can be used.
▪ >>>tup1 = (‘Physics’,’chemistry’,’mathematics’)
▪ >>>tup2 = (10,20,30,40,50)
▪ >>>tup2[1:4]
▪ (20, 30, 40) # Output
▪ >>>tup1[:1]
▪ (‘Physics’,) # Output
▪ >>>tup1[:2]
▪ (‘Physics’, ‘chemistry’) # Output
▪ Tuples are immutable. The values or items in the tuple cannot be changed once
it is declared.
▪ If we want to change the values, we have to create a new tuple.
▪ # declaring a tuple
▪ >>>tup = (12, 15, “Python”, 2.3)
▪ # change the 3rd element “Python” to “Hello”
▪ >>>tup[2] = “Hello”
▪ TypeError: ‘tuple’ object does not support item assignment
▪ It allows the assignment of values to a tuple of variables on the left side of the
assignment from the tuple of values on the right side of the assignment.
▪ The number of variables in the tuple on the left of the assignment must match
the number of elements/items in the tuple on the right of the assignment
▪ # creating a tuple
▪ >>>Anu = (‘221’,’Anu’,’Rahul’,’Delhi’,1971,’Jaipur Gwalior’)
▪ # tuple assignment
▪ >>>(id,fst_name,lst_name,city,year_of_birth,birth_place) = Anu
▪ >>>print (id)
▪ 221 # Output
▪ >>>print (fst_name)
▪ Anu # Output
▪ Using tuple assignment two values x,y can be swapped without using a
temporary variable
▪ >>>x = 3
▪ >>>y = 4
▪ >>>x , y = y , x # Using tuple assignment
▪ >>>print(x)
▪ 4 # Output
▪ >>>print(y)
▪ 3 # Output
▪ 1. Concatenation
▪ The concatenation operator works in tuples in the same way as it does in lists.
▪ This operator concatenates two tuples. This is done by the + operator in Python
▪ >>>t1 = (1,2,3,4)
▪ >>>t2 = (5,6,7,8)
▪ >>>t3 = t1 + t2
▪ >>>print(t3)
▪ (1, 2, 3, 4, 5, 6, 7, 8) # Output
▪ 2. Repetition
▪ The repetition operator works as its name suggests; it repeats the tuples a given
number of times.
▪ Repetition is performed by the* operator in Python.
▪ >>>tuple = (‘ok’,)
▪ >>>tuple * 5
▪ (‘ok’, ‘ok’, ‘ok’, ‘ok’, ‘ok’) # Output
▪ >>>(‘Hello’,) * 3
▪ (‘Hello’, ‘Hello’, ‘Hello’) # Output
▪ 3. in Operator
▪ The in operator also works on tuples.
▪ It tells user that the given element exists in the tuple or not.
▪ It gives a Boolean output, that is, TRUE or FALSE.
▪ If the given input exists in the tuple, it gives the TRUE as output, otherwise
FALSE.
▪ >>>tuple = (10,20,30,40)
▪ >>>20 in tuple
▪ True # Output
▪ >>>50 in tuple
▪ False # Output
▪ 4. Iteration
▪ Iteration can be done in tuples using for loop. It helps in traversing the tuple.
▪ >>>tuple = (1,2,3,4)
for x in tuple:
... print (x)
Output:
1
2
3
4
Function Description
len(tuple) It returns the length of a tuple
zip(tuple1, tuple2) It ‘zips’ elements from two tuples into a list of tuples.
max(tuple) It returns the largest value among the elements in a tuple
min(tuple) It returns the smallest value among the elements in a tuple
tuple(seq) It converts a list into a tuple.