Tuples & Its Operations in Python. Topic – 18

Tuples & It's Operations In Python

Python Tuples:

Tupples is an ordered collection where we can store the data item. They store multiple items in a single variable. Tuple items are separated by a comma and enclosed with the first bracket(). Tuple is immutable. It means if create a tuple you can’t modify the items inside the tuple. Let’s understand through an example.

tuple1 = (1, 2, 2, 3, 5, 4, 6)
tuple2 = ("Red", "Green", "Blue")
print(tuple1)
print(tuple2)
Python

The Output Of This Code Will Be:

Output
(1, 2, 2, 3, 5, 4, 6)
('Red', 'Green', 'Blue')
Python

Example 2

details = ("Shivam", 21, "cybercript", 9.8)
print(details)
Python

Tuples are similar to the list but there is a major difference. Tuples are unchangable. It means we can’t modify the value. When using tuples, it is typically to protect data that should not be changed later.

The Output Of This Code Will Be:

Output
('Shivam', 21, 'cybercript', 9.8)
Python

Tuple Indexes:

Insight the tuple every element has a unique index number. The first item has an index of [0], the second item [1], the third item [2], and so on. Let’s see an example:

country = ("Spain", "Italy", "India")
#            [0]      [1]      [2]
Python

Accessing Tuple Items:

We already know that a tuple item has a unique index number. We can access the value using an index number. We can access it through positive Index numbers and also negative Index numbers. Let’s understand through an example:

Positive Indexing:

country = ("Spain", "Italy", "India")
#            [0]      [1]      [2]
print(country[0])
print(country[1])
print(country[2])
Python

The Output Of This Code Will Be:

Output
Spain
Italy
India
Python

Negative Indexing:

Negative indexing is also used to access items from the end of the tuple. The last item has an index of [-1], the second last item [-2], the third last item [-3], and so on. Let’s see an example:

country = ("Spain", "Italy", "India", "England", "Germany")
#            [0]      [1]      [2]       [3]        [4]
print(country[-1]) # Similar to print(country[len(country) - 1])
print(country[-3])
print(country[-4])
Python

The Output Of This Code Will Be:

Output
Germany
India
Italy
Python

Check for Item:

We can check if there available any specific items available through the keyword.

country = ("Spain", "Italy", "India", "England", "Germany")
if "Germany" in country:
    print("India is present.")
else:
    print("India is absent.")
Python

The Output Of This Code Will Be:

Output
India is present.
Python

Example 2:

country = ("Spain", "Italy", "India", "England", "Germany")
if "Russia" in country:
    print("Russia is present.")
else:
    print("Russia is absent.")
Python

The Output Of This Code Will Be:

Output
Russia is absent.
Python

Range of Index:

We can print the items of the tuple by specifying a range, where you want to start, where you want to end, and if you want to skip elements in between the range.

Syntax:

Output
Tuple[start : end : jumpIndex]
Python

Note: jumpIndex is optional. If we do not specify anything, by default this value is ‘0’

Printing elements within a particular range:

animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[3:7])     # Positive indexes
print(animals[-7:-2])   # Negative indexes
Python

The Output Of This Code Will Be:

Output
('mouse', 'pig', 'horse', 'donkey')
('bat', 'mouse', 'pig', 'horse', 'donkey')
Python

Here, we provide the index of the element from where we want to start and the index of the element till which we want to print the values. Note: The element of the end index provided will not be included.

Printing all elements from a given index till the end:

animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[4:])      # Positive index
print(animals[-4:])     # Negative index
Python

When we do not provide any end index number, the interpreter prints the values from start to end.

The Output Of This Code Will Be:

Output
('pig', 'horse', 'donkey', 'goat', 'cow')
('horse', 'donkey', 'goat', 'cow')
Python

Printing all elements from start to a given index:

animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[:6])      # Positive index
print(animals[:-3])     # Negative index
Python

When we did not provide any start index, the interpreter printed all values from start to end.

The Output Of This Code Will Be:

Output
'cat', 'dog', 'bat', 'mouse', 'pig', 'horse')
('cat', 'dog', 'bat', 'mouse', 'pig', 'horse')
Python

Print alternate values:

animals = ("cat", "dog", "bat", "mouse", "pig", "horse", "donkey", "goat", "cow")
print(animals[::2])     # Positive index
print(animals[-8:-1:2]) # Negative index
Python

Here, we are not provided any start and end index value, which means all values are considered. But we provide a jump index that’s why every second element will be printed.

The Output Of This Code Will Be:

Output
('cat', 'bat', 'pig', 'donkey', 'cow')
('dog', 'mouse', 'horse', 'goat')
Python

If you don’t know the “Tuples & Its Operations in Python” just click on read more.

I hope you understand “Tuples & Its Operations in Python”  Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

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

ABOUT ME
Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog