A tuple can be defined as a sequence of values that can be any type, and they are indexed by integers. When compared to a list a tuple is immutable which means we cannot change the values of individual elements. The general form of tuple definition can be written as:
Example 1 Define a tuple with 5 elements of different value types and then check the type of the variable to which the tuple is assigned. Then try to create two tuples containing only one element (item) with and without a comma after the item.
Solution: Using previously defined general tuple definition we will create tuple.
Example 2 Construct a tuple using the built-in function tuple. Use a tuple of a string typed by the user.
Solution: Using tuple function with empty parenthesis creates an empty tuple.
Example 2 Create a tuple and using for loop print every tuple element. Using a slice print the first 5 elements.
Solution: In this example we will also use user input. From the user input, a tuple will be created using the built-in tuple command and finally, all tuple elements will be shown as the output.
tuple_name = item_1, item_2,...,item_nwhere tuple_name is the variable name that contains the tuple, and the sequence of items (item_1, item_2,...,item_n) are tuple elements. Using this form we have defined a tuple however, it is common to enclose tuples in parentheses. So the general form of common tuple definition can be written as:
tuple_name = (item_1, item_2, ..., item_n)A tuple can be created using Python built-in function tuple. The general definition for creating a tuple using the tuple function can be written in the following form:
tuple_name = tuple()As you can see after the tuple function there is a set of brackets. Since there are not any arguments written inside these brackets this will create an empty tuple.
Example 1 Define a tuple with 5 elements of different value types and then check the type of the variable to which the tuple is assigned. Then try to create two tuples containing only one element (item) with and without a comma after the item.
Solution: Using previously defined general tuple definition we will create tuple.
tup = ('a', 1, 'b', 2.1, True)which will generate the output given below.
print("tup = {}".format(tup))
print("type(tup) = {}".format(type(tup))
tup = ('a', 1, 'b', 2.1, True)Now let's create two new variables tup1 and tup2 that are equal to the only first element of the original tuple (tup). The key difference between tuple t1 and t2 is that after the first element t1 will have a comma and tup2 won't have a comma.
type(tup) = < class 'tuple'>
tup1 = ('a', )The output of the previous code is shown below.
tup2 = ('a')
print("type(tup1) = {}".format(type(tup1)))
print("type(tup2) = {}".format(type(tup2)))
type(tup1) = < type 'tuple' >As seen from the output the tup1 variable is a tuple type, and tup2 is a string. In case of tup2 variable definition it was defined without comma. So, Python treats ('a') as an expression with a string in parentheses.
type(tup2) = < type 'str' >
Example 2 Construct a tuple using the built-in function tuple. Use a tuple of a string typed by the user.
Solution: Using tuple function with empty parenthesis creates an empty tuple.
t = tuple()The output is given below:
print("t = {}".format(t))
t = ()The script for asking the user to input the string and convert it into a tuple is given below.
USER_INPUT = input("Enter a string:> ")Output:
t = tuple(USER_INPUT)
print("t = {}".format(t))
Enter a string:> This is my first string converted to tuple.
t = ('T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'm', 'y', ' ', 'f', 'i', 'r', 's', 't', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'c', 'o', 'n', 'v', 'e', 'r', 't', 'e', 'd', ' ', 't', 'o', ' ', 't', 'u', 'p', 'l', 'e')
Example 2 Create a tuple and using for loop print every tuple element. Using a slice print the first 5 elements.
Solution: In this example we will also use user input. From the user input, a tuple will be created using the built-in tuple command and finally, all tuple elements will be shown as the output.
USER_INPUT = input("Enter a string:> ")OUTPUT:
t = tuple(USER_INPUT)
for i in t:
print("t[{}] = {}".format(i, t[i]))
Enter a string:> This is awesome!
t[0] = T
t[1] = h
t[2] = i
t[3] = s
t[4] =
t[5] = i
t[6] = s
t[7] =
t[8] = a
t[9] = w
t[10] = e
t[11] = s
t[12] = o
t[13] = m
t[14] = e
t[15] = !