In the article, we discuss Tuple In Python

Tuple In Python

Tuple In Python
Tuple In Python

Tuple In Python

Python programming language has various data types including lists, sets, dictionaries, etc. Python also comes with a collections package that has specialized data structures. Tuple in python is also one of the collection data types that is popular. In this article, we will learn about tuples in detail with examples.

we discuss below topics

  • What Is A Tuple?
  • Accessing Items In A Tuple
    • Indexing
    • Slicing
  • Changing A Tuple
  • Concatenating Two Tuples
  • Deleting A Tuple
  • Tuple Methods
  • List vs Tuple
  • Iterating Through A Tuple
  • Tuple Constructor
  • Membership Test In A Tuple

What Is A Tuple?

A tuple is an immutable data type in python, almost similar to a list in python in terms of indexing and having duplicate members. It is a collection data type that stores python objects separated by commas. Following is an example of how we can create or declare a tuple in python.

#creating a tuple
a = ('python', 'developer')
#another approach
b = 'python' , 'developer'
print(a)
print(b)
Output: ('python' , 'developer')
        ('python' , 'developer')

Accessing Items In A Tuple

Accessing items in a tuple works similar to a list, we can access elements in a list using indexes. We can specify the index value and it will return the item stored at that particular index value.

Indexing 

It is a data structure technique to effectively retrieve information from a data structure. In python, several data types support indexing like lists, string, etc.

For example, let’s just say we have a tuple with 5 natural numbers as members. So the indexing will start with the value 0 where 1 will be stored and it will go until the end of the tuple i.e 5 and the index value at 5 will be 4.

Take a look at the example below to understand how we can access elements in a tuple using indexing

a = ('developers', 'python' , 'data structure' , 'collections')
print(a[1])
print(a[0])
Output: python
        developers

As you can see in the above example, we are able to get the elements stored at the index values 1 and 3. Similarly, we can access any value inside a tuple using index values.

Negative Indexing 

In python, we can use negative indexing as well to access elements in a tuple or any other data type that supports indexing.

a = (1,2,3,4,5,6,7,8,9,10)
print(a[-4])
print(a[-1])
Output: 7
        10

Slicing

It is a technique in which we use the slicing operator‘:’ to get a range of elements from a tuple or any other data type that supports indexing for accessing elements.

a = (1,2,3,4,5,6,7,8,9,10)
print(a[1:8])
print(a[1:])
print(a[:5])
Output: (2,3,4,5,6,7,8)
        (2,3,4,5,6,7,8,9,10)
        (1,2,3,4,5)

In the above example, the index value before the slicing operator is the starting index and the index value after the slicing operator is the value that will not be included in the output.

Only until the value before the ending index will be included in the output. We can even use the negative index values with the slicing operator to get the range of values from the tuple.

a = (1,2,3,4,5,6,7,8,9,10)
print(a[-8:])
Output: (3,4,5,6,7,8,9,10)

Changing A Tuple

Even though tuples in python are immutable in nature, a nested object in a tuple can be changed. Or in general, a tuple in python can be reassigned with a different value.

a = (1,2,3,[4,5])
a[3][0] = 14
print(a)
#reassigning the value
a = ('pyhton', 'developer')
print(a)
Output: (1,2,3,[14,5])
        ('python', 'developer')

Concatenating Two Tuples

Joining two tuples is a very easy task. You just to assign the addition of the two tuples to another variable and it will return the concatenated tuple with the values of both the tuples. Consider the example below to understand this.

a = (1,2,3,4,5)
b = (6,7,8,9,10)
c = a + b
print(c)
Output: (1,2,3,4,5,6,7,8,9,10)

As you can see in the example, the concatenated tuple contains the values of both the tuples a and b.

Deleting A Tuple

Being an immutable data type, a tuple in python does not allow any changes and you cannot even remove an element from a tuple after the declaration. But there is a keyword ‘del’ which will delete the tuple altogether.

a = (1,2,3,4,5)
del a
print(a)

You will get a Name error if you run the above program because there is no tuple named as present since we have deleted it.

Tuple Methods

Following are the tuple methods that we can use while working with a tuple in python.

  • count: Returns the count of the items.
  • index: It returns the index of the item specified.
a = (1,2,1,3,1,3,1,2,1,4,1,5,1,5)
print(a.count(1))
print(a.index(5))
Output: 7
        11

List vs Tuple

ListTuple
Used for homogenous data typesGenerally used for heterogeneous data types
Mutable in natureImmutable in nature, which helps in faster iteration
Does not have immutable elementsImmutable elements can be used as a key for a dictionary
No guarantee that the data is write-protectedImplementing a tuple with data that does not change guarantees that it is write-protected
Tuple In Python

Iterating Through A Tuple

Using a for loop we can iterate through a tuple in python. The following example shows how we can iterate through a tuple using a for loop.

a = ("developer", "for data science", "for Artificial Intelligence")
for i in a:
    print("python", i)
Output: python developer
        python for data science
        python for artificial intelligence

Tuple Constructor

It is possible to create a tuple using a tuple() constructor as well. We can even use the tuple constructor to change a list to a tuple.

a = [1,2,3,4,5]
b = tuple(a)
print(b)
c = tuple(('python', 'developer'))
print(c)
Output: (1,2,3,4,5)
        ('python', 'developer')

Membership Test In A Tuple

Using the membership operator ‘in’ in python we can check whether an element is present in a tuple or not. The following example shows how we can check if an element is present in a tuple or not.

a = (1,2,3,4,5,6,7,8,9,10)
 
print(6 in a)
 
print(15 in a )
Output: True 
        False

we have come to the end of our article where we have learned how we can use tuple in python and how we can access the elements in a tuple using indexes with various other examples.

Source: Various

Also Checkout Inheritance In Python With Examples

If you intreasted in Entertainment genre MOVIES STREAMING DATES

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *