Categories: python

Python Tuple

What is Python Tuple?

A Python Tuple is a group of items that are separated by commas. The indexing, nested objects, and repetition of a tuple are somewhat similar to those of a list, but unlike a list, a tuple is immutable.

Example:


tuple_1 = ("Python", "tuples", "immutable", "object") 
tuple_2 = (23, 42, 12, 53, 64)  
tuple_3 = "Python", "Tuples", "Ordered", "Collection"
print(tuple_1)
print(tuple_2)
print(tuple_3) 
Output:
('Python', 'tuples', 'immutable', 'object')
(23, 42, 12, 53, 64)
('Python', 'Tuples', 'Ordered', 'Collection')
Empty Tuple:
Empty_tuple = ()  
print(Empty_tuple)
Output:
()
Indexing in Tuples:

The index operator [] can access an object within a tuple, with indexing in the tuple beginning at 0. Tuples can also use backward indexing; for example, the last element can be accessed using the index -1, and the following elements can be accessed using the indexes -2, -3, and so on.

tuple_1 = ("Python", "tuples", "immutable", "object") 
tuple_2 = (23, 42, 12, 53, 64)  
tuple_3 = "Python", "Tuples", "Ordered", "Collection"
print(tuple_1[2])
print(tuple_2[1])
print(tuple_3[0])
Output:
immutable
42
Python
Adding Elements to a Tuple:

The data contained in a tuple cannot be edited because tuples are immutable, but more data can certainly be added to a tuple. You can accomplish this by using the addition operator. For Instance,

tuple_2 = (23, 42, 12, 53, 64)  
tuple_2 =tuple_2 + (100,)

print(tuple_2)
print ((1, 2, 5, 8) + (2, 5, 4))
Output:
(23, 42, 12, 53, 64, 100)
(1, 2, 5, 8, 2, 5, 4)
Deleting a Tuple:

A tuple’s components cannot be altered, as was previously mentioned. As a result, we are unable to get rid of or remove tuple components. However, a tuple can be completely deleted using the keyword del. For instance,

del (tuple_2)
Slicing in Tuples:

Tuple slicing works the same as list slicing.

tuple_1 = ("Python", "Tuple", "Ordered", "Immutable", "Collection", "Objects")  
  
print("Elements between indices 1 and 3: ", tuple_1[1:3])  
 
print("Elements between indices 0 and -4: ", tuple_1[:-4])  
   
print("Entire tuple: ", tuple_1[:])  
Output:
Elements between indices 1 and 3:  ('Tuple', 'Ordered')
Elements between indices 0 and -4:  ('Python', 'Tuple')
Entire tuple:  ('Python', 'Tuple', 'Ordered', 'Immutable', 'Collection', 'Objects')
Repetition Tuples:

Multiplying a tuple by any integer x will simply create another tuple with all the elements from the first tuple being repeated x number of times.

tuple_2 = (23, 42, 12, 53, 64)  
print(tuple_2*3)
Output:
(23, 42, 12, 53, 64, 23, 42, 12, 53, 64, 23, 42, 12, 53, 64)
max() and min() function:

To find the maximum value in a tuple, we can use the max() function, while for finding the minimum value, min() function can be used.

tuple_2 = (23, 42, 12, 53, 64)  

print(min(tuple_2))
print(max(tuple_2))
Output:
12
64

Note: also read about Python Dictionary Methods

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

19 hours ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

3 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago