In this article you will learn about Python Tuple Concept
Python Tuple Concept – Before moving ahead, let’s know about Python List
Tuple – It is a collection and contains elements in ordered and unchangeable form. It represents with round brackets.
Creation of Tuple
Example 1- Creating a Tuple.
x = (1,2,3) print(x)
Allow Duplicates Items
Tuple Duplicates Items – Since, Tuple items are indexed therefore, it allows duplicates items.
x = ('a', 'b', 'c', 'd', 'a', 'e') print(x)
Tuple Type()
Tuple Type() – In Python, Tuple is defined as class <tuple>
Example – Define Tuple Type()
x = ('a' , 'b' , 'c' , 'd') print(type(x))
Tuple Data Types
Tuple Items – Tuple items can be any type of Data Types.
Example – Tuple items with data type str, int and boolean.
x = ('a' , 'b' , 'c' , 'd') y = (1 , 2 , 3 , 4 , 5) z = (True , False) print('Tuple str' , x) print('Tuple int' , y) print('Tuple Boolean' , z)
Accessing Tuple Item
Access Tuple elements – Elements can be accessed by defining index number in the square brackets.
H | E | L | L | O |
1 | 2 | 3 | 4 | 5 |
Example 2- Access element ‘c’ of Tuple.
x = ('a', 'b', 'c', 'd') print(x[2])
Note: In beginning index number starts from zero (0).
Negative Indexing
Negative Indexing – It starts from end with the index number -1 and beginning with 0.
H | E | L | L | O |
-5 | -4 | -3 | -2 | -1 |
Example 3- Get second last element ‘c’ of Tuple.
x = ('a' , 'b' , 'c' , 'd') print(x[-2])
Note: In end index numbers starts from minus one (-1).
Indexing Range
Range of indexes – It specified range of index number to get particular element from start to end. Specifying range returns new list of specified elements.
Example 4- Get particular elements from index number 1 to 2.
x = ('Welcome' , 'in' , 'programming' , 'language') print(x[1:3])
Note: Starting index number (1) is included but ending number (3) is excluded.
Negative Indexing with Range
Range of negative indexes – It specified range of negative indexing number. It starts from end with index number -1.
Example 5- Start search from end and get particular elements from start to end.
x = ('Welcome', 'in', 'programming', 'language') print(x[-4:-1])
Tuple Updates
Tuples are unchangeable, or immutable. Once a tuple created its value cannot be changed. But it is possible to convert a tuple into a list and vice versa.
Example 6- Convert a tuple into list.
x = ('Welcome' , 'in' , 'programming' , 'language') print("before updating Tuple type is:" , x) print(type(x)) z = list(x) print("After updating Tuple type" , z) print(type(z))
Loop in Tuple
Loop in Tuple – A loop in Python is considered as iterating throughout elements. A Tuple can loop through Tuple.
Example 7- Use of ‘for’ loop to looping through Tuple.
z = ('Welcome' , 'in' , 'programming' , 'language') for x in z: print(x)
Note: We will learn more about ‘for’ loop later in detail.
Check if elements exist
Check element exits – To check particular element exits, use ‘in’ keyword.
Example 8- Check if element ‘programming’ exits.
x = ('Welcome' , 'in' , 'programming' , 'language') print('programming' in x)
Tuple Length
Length of list – To get length of Tuple use ‘len’ function.
Example 9- Use len() function to get length.
x = ('Welcome' , 'in' , 'programming' , 'language') print(len(x))
Tuple is immutable. Once a Tuple created its element cannot be changed.
Checking if element can add
Example 10- Checking if element can add in Tuple.
x = ('Welcome' , 'programming' , 'language') x[1] = 'in' print(x)
If you find anything incorrect in above discussed topic and have any further question, please comment down below.