0% found this document useful (0 votes)
214 views

Python Tuple

1. A Python tuple is used to store immutable sequences of Python objects. Tuples are similar to lists but their values cannot be changed once set. 2. Tuples are defined using comma-separated values within parentheses. They can be indexed and sliced like lists but their items cannot be deleted individually since tuples are immutable. 3. Tuples are useful when the sequence of data must remain fixed, such as when using tuples as dictionary keys or in nested data structures like a list of tuples representing records. Common tuple operations include concatenation, repetition, membership testing, and iteration.

Uploaded by

Sumit Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views

Python Tuple

1. A Python tuple is used to store immutable sequences of Python objects. Tuples are similar to lists but their values cannot be changed once set. 2. Tuples are defined using comma-separated values within parentheses. They can be indexed and sliced like lists but their items cannot be deleted individually since tuples are immutable. 3. Tuples are useful when the sequence of data must remain fixed, such as when using tuples as dictionary keys or in nested data structures like a list of tuples representing records. Common tuple operations include concatenation, repetition, membership testing, and iteration.

Uploaded by

Sumit Tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Tuple

Python Tuple is used to store the sequence of immutable python objects. Tuple is similar
to lists since the value of the items stored in the list can be changed whereas the tuple is
immutable(not changable) and the value of the items stored in the tuple can not be
changed.

A tuple can be written as the collection of comma-separated values enclosed with the
small brackets. A tuple can be defined as follows.

T1 = (101, "Ayush", 22)  
T2 = ("Apple", "Banana", "Orange")   

Example
tuple1 = (10, 20, 30, 40, 50, 60)  
print(tuple1)  
count = 0  
for i in tuple1:  
    print("tuple1[%d] = %d"%(count, i));  

Example 2
tuple1 = tuple(input("Enter the tuple elements ..."))  
print(tuple1)  
count = 0  
for i in tuple1:  
    print("tuple1[%d] = %s"%(count, i));  

Output:

Enter the tuple elements ...12345


('1', '2', '3', '4', '5')
tuple1[0] = 1
tuple1[0] = 2
tuple1[0] = 3
tuple1[0] = 4
tuple1[0] = 5

However, if we try to reassign the items of a tuple, we would get an error as


the tuple object doesn't support the item assignment.

An empty tuple can be written as follows.

1. T3 = ()  

The tuple having a single value must include a comma as given below.
1. T4 = (90,)   

Tuple indexing and splitting


The indexing and slicing in tuple are similar to lists. The indexing in the tuple starts from
0 and goes to length(tuple) - 1.
The items in the tuple can be accessed by using the slice operator. Python also allows us
to use the colon operator to access multiple items in the tuple.
Consider the following image to understand the indexing and slicing in detail.

2.
3. Unlike lists, the tuple items 

Deleting the tuple

Unlike lists, the tuple items can not be deleted by using the del keyword as tuples are
immutable. To delete an entire tuple, we can use the del keyword with the tuple name.

Consider the following example.

tuple1 = (1, 2, 3, 4, 5, 6)  
print(tuple1)  
del tuple1[0]  
print(tuple1)  
del tuple1  
print(tuple1)  
Where use tuple
Using tuple instead of list is used in the following scenario.

1. Using tuple instead of list gives us a clear idea that tuple data is constant and must
not be changed.

2. Tuple can simulate dictionary without keys. Consider the following nested structure
which can be used as a dictionary.

1. [(101, "John", 22), (102, "Mike", 28),  (103, "Dustin", 30)]   

3. Tuple can be used as the key inside dictionary due to its immutable nature.

Basic Tuple operations


The operators like concatenation (+), repetition (*), Membership (in) works in the same
way as they work with the list. Consider the following table for more detail.

Let's say Tuple t = (1, 2, 3, 4, 5) and Tuple t1 = (6, 7, 8, 9) are declared.

Operator Description Example

Repetition The repetition operator enables the tuple elements to be T1*2 =


repeated multiple times. (1, 2,
3, 4, 5,
1, 2, 3,
4, 5)

Concatenation It concatenates the tuple mentioned on either side of the T1+T2 =


operator. (1, 2,
3, 4, 5,
6, 7, 8,
9)

Membership It returns true if a particular item exists in the tuple otherwise print (2
false. in T1)
prints
True.

Iteration The for loop is used to iterate over the tuple elements. for i in
T1:

print(i)
Python Tuple inbuilt functions

SN Function Description

1 cmp(tuple1, tuple2) It compares two tuples and returns true if tuple1 is greater than
tuple2 otherwise false.

2 len(tuple) It calculates the length of the tuple.

3 max(tuple) It returns the maximum element of the tuple.

4 min(tuple) It returns the minimum element of the tuple.

5 tuple(seq) It converts the specified sequence to the tuple.

Nesting List and tuple


We can store list inside tuple or tuple inside the list up to any number of level.

Lets see an example of how can we store the tuple inside the list.

1. Employees = [(101, "Ayush", 22), (102, "john", 29), (103, "james", 45), (104, "B
en", 34)]  
2. print("----Printing list----");   
3. for i in Employees:  
4.     print(i)  
5. Employees[0] = (110, "David",22)  
6. print();  
7. print("----Printing list after modification----");  
8. for i in Employees:   
9.     print(i)  

You might also like