Tuesday 9 April 2019

Python Tuples

Tuple

A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.

Example

Create a Tuple:
thistuple = ("apple""banana""cherry")
print(thistuple)
Show Python »
C:\Users\My Name>python demo_tuple.py
('apple', 'banana', 'cherry')        
                                     
                                     


Access Tuple Items

You can access tuple items by referring to the index number, inside square brackets:

Example

Return the item in position 1:
thistuple = ("apple""banana""cherry")
print(thistuple[1])
Show Python »
C:\Users\My Name>python demo_tuple1.py
banana                                
                                      
                                      


Change Tuple Values

Once a tuple is created, you cannot change its values. Tuples are unchangeable.

Example

You cannot change values in a tuple:
thistuple = ("apple""banana""cherry")
thistuple[1] = "blackcurrant"
# The values will remain the same:print(thistuple)
Show Python »
C:\Users\My Name>python demo_tuple2.py
('apple', 'banana', 'cherry')         
                                      
                                      



Loop Through a Tuple

You can loop through the tuple items by using a for loop.

Example

Iterate through the items and print the values:
thistuple = ("apple""banana""cherry")
for x in thistuple:
  print(x)
Show Python »
C:\Users\My Name>python demo_tuple_loop.py
apple                                     
banana                                    
cherry                                    
                                          
                                          

You will learn more about for loops in out Python For Loops Chapter.

Check if Item Exists

To determine if a specified item is present in a tuple use the in keyword:

Example

Check if "apple" is present in the tuple:
thistuple = ("apple""banana""cherry")
if "apple" in thistuple:
  print("Yes, 'apple' is in the fruits tuple")
Show Python »
C:\Users\My Name>python demo_tuple_in.py
Yes, 'apple' is in the fruits tuple     
                                        
                                        


Tuple Length

To determine how many items a tuple has, use the len() method:

Example

Print the number of items in the tuple:
thistuple = ("apple""banana""cherry")
print(len(thistuple))
Show Python »
C:\Users\My Name>python demo_tuple_length.py
3                                           
                                            
                                            


Add Items

Once a tuple is created, you cannot add items to it. Tuples are unchangeable.

Example

You cannot add items to a tuple:
thistuple = ("apple""banana""cherry")
thistuple[3] = "orange" # This will raise an errorprint(thistuple)
Show Python »
C:\Users\My Name>python demo_tuple_add.py                 
Traceback (most recent call last):                        
  File "demo_tuple_add.py", line 2, in <module>           
    thistuple[3] = "orange" # This will raise an error    
TypeError: 'tuple' object does not support item assignment

Remove Items

Note: You cannot remove items in a tuple.
 Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely:

Example

The del keyword can delete the tuple completely:
thistuple = ("apple""banana""cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists
Show Python »
C:\Users\My Name>python demo_tuple_del.py                                 
Traceback (most recent call last):                                        
  File "demo_tuple_del.py", line 3, in <module>                           
    print(thistuple) #this will raise an error because the tuple no longer
 exists                                                                   
NameError: name 'thistuple' is not defined                                
                                                                          
                                                                          


The tuple() Constructor

It is also possible to use the tuple() constructor to make a tuple.

Example

Using the tuple() method to make a tuple:
thistuple = tuple(("apple""banana""cherry")) # note the double round-bracketsprint(thistuple)
Show Python »
C:\Users\My Name>python demo_tuple3.py
('apple', 'banana', 'cherry')         
                                      
                                      


Tuple Methods

Python has two built-in methods that you can use on tuples.
MethodDescription
count()Returns the number of times a specified value occurs in a tuple
index()Searches the tuple for a specified value and returns the position of where it was found

1 comment: