Python Tuple

  • December 1, 2022
  • python
create table
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

Leave a Reply

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