codingstreets
Search
Close this search box.
python-tuple-concept

Introduction to Python Tuple Concept with Practical Examples

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)
python-tuple-concept

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)
python-tuple-concept

Tuple Type()

Tuple Type() – In Python, Tuple is defined as class <tuple>

Example – Define Tuple Type()

x = ('a' , 'b' , 'c' , 'd')
print(type(x))
python-tuple-concept

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)
python-tuple-concept

Accessing Tuple Item

Access Tuple elements – Elements can be accessed by defining index number in the square brackets.

HELLO
12345
Index number

Example 2- Access element ‘c’ of Tuple.

x = ('a', 'b', 'c', 'd')
print(x[2])
python-tuple-concept
As it is shown clearly that it retuned element of index number 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.

HELLO
-5-4-3-2-1
Negative Indexing

Example 3- Get second last element ‘c’ of Tuple.

x = ('a' , 'b' , 'c' , 'd')
print(x[-2])
python-tuple-concept
As it is shown clearly that it retuned element of index number -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])
python-tuple-concept
As it is shown clearly that it returned particular elements from index number 1 to 2.

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])
python-tuple-concept
As it is shown clearly that it returned string between the range.

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))
python-tuple-concept
As it is shown clearly that type of Tuple has changed from <class ‘tuple’> to <class ‘list’> after using list method. 

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)
python-tuple-concept
As it shown clearly that every element is looped through ‘for’ loop and returned one by one.

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)
python-tuple-concept
As it is shown clearly that character ‘programming’ is in string therefore, it returned True.

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))
python-tuple-concept
As it is shown clearly that it returned length of string.

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)
python-tuple-concept
As it is shown clearly that element cannot be inserted if once Tuple is created.    

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on