PYTHON TUPLE

Tuple is a data structure. Tuple is a sequence or container which store the items of different data types in a parenthesis (). Tuple is ordered and immutable (not changeable). The main difference between list and tuple is we can change, update, remove items in the list, but in tuple it is not possible.  

Operations on tuple are faster compared to list, because tuple are immutable

Define Tuple

we can define tuple in two ways

animals = ('rabbit', 'cat', 'dog')

OR

animals = tuple(('rabbit', 'cat', 'dog'))

print tuple

print method will print items in the tuple

animals = ('rabbit', 'cat', 'dog') 
print(animals) 

Output: ('rabbit', 'cat', 'dog')

length of tuple

len() will give number of items in the tuple

animals = ('rabbit', 'cat', 'dog') 
print(len(animals)) 

Output: 3

Tuple index

index is a number to specify the position of element in the tuple. With the help of index, we can print the item. If n items are there, 0 to n-1 are the index positions. 

Following is the tuple of animals . In this tuple 3 items ('rabbit', 'cat', 'dog') are there, 0, 1,2 are the respective index positions.

animals = ('rabbit', 'cat', 'dog') 
print(animals[0]) 
print(animals[1]) 
print(animals[2]) 

Output: rabbit 
            cat 
            dog 

Index of particular item in the tuple

We can find the index of particular item in the tuple if it exists, if item is repeated it will give first occurrence

animals = ('rabbit', 'cat', 'dog') 
print(animals.index('cat'))

Output: 1

Negative index

Negative index will start from the last position

animals = ('rabbit', 'cat', 'dog') 
print(animals[-1]) 

Output: dog 

animals = ('rabbit', 'cat', 'dog') 

                    -3       -2     -1        are the negative index positions 

count

count will give the how many times the item is there in the tuple

animals = ('rabbit', 'cat', 'dog','cat') 
print(animals.count('cat'))

Output: 2

output is 2 because of two cats in tuple

Tuple Concatenation

We can add two tuples with the help of + operator

animals = ('rabbit', 'cat', 'dog') 
wild animals = ('tiger', 'lion', 'wolf') 
print(animals + wild_animals)

Output: ('rabbit', 'cat', 'dog', 'tiger', 'lion', 'wolf')

Tuple Repetition

We can repeat the tuple with help of * operator

animals = ('rabbit', 'cat', 'dog') 
    print(animals * 2)

Output: ('rabbit', 'cat', 'dog', 'rabbit', 'cat', 'dog') 

Tuple Slicing

Slicing the tuple is nothing but creating sub tuple from an existing tuple

Syntax for slicing the tuple

tuple_name[start:stop:step]

Default values :

         start is 0 (0 index position), stop last position, step is 1

animals = ('rabbit', 'cat', 'dog', 'tiger', 'lion', 'wolf') 
print(animals[::])

Output: ('rabbit', 'cat', 'dog', 'tiger', 'lion', 'wolf')  

Printing tuple from a specified position to last. In the following code we are printing from 3rd index to last item 

animals = ('rabbit', 'cat', 'dog', 'tiger', 'lion', 'wolf') 
print(animals[3:])

Output: ('tiger', 'lion', 'wolf')

Printing tuple from starting to specified position

animals = ('rabbit', 'cat', 'dog', 'tiger', 'lion', 'wolf') 
print(animals[:4]) 

Output: ('rabbit', 'cat', 'dog', 'tiger')

We can print the tuple from specified position to specified positionPrinting tuple from index position 1 to 4 (4th index excluded)

animals = ('rabbit', 'cat', 'dog', 'tiger', 'lion', 'wolf') 
print(animals[1:4])

Output: ('cat', 'dog', 'tiger')

using step in slicing

start at 1st index, stop at 5th index and step up 2. So it will take cat at 1st index, then step 2 positions up at 3rd index position it will take tiger, then 5th index position     (but it will not include last position)

animals = ('rabbit', 'cat', 'dog', 'tiger', 'lion', 'wolf') 
print(animals[1:5:2])

Output: ('cat', 'tiger')

Reversing tuple with slicing

animals = ('rabbit', 'cat', 'dog', 'tiger', 'lion', 'wolf') 
print(animals[::-1]) 

Output: ('wolf', 'lion', 'tiger', 'dog', 'cat', 'rabbit')

 Contact Us:

                  codecalculatenr@gmail.com

 

About Us : 

        Code Calculate is the biggest online platform