Python Tuples

Introduction

 
In this chapter, we will discuss how to use Python Tuples.
 

What are Tuples?

 
Tuples are sequences that are used to store a sequence of values belonging to any type.
 
 
Tuples are almost the same as Lists but the major difference is,
  • Tuples are immutable and Lists are mutable.
  • Tuples are depicted through ( ) and Lists are depicted through [ ].
     
    Understanding Tuples In Python
     
Understanding Tuples In Python
 
NOTE
 
Tuples are immutable (cannot be modified) i.e., you cannot change elements of the tuple. You cannot perform item-assignment in immutable types.
 

Creating Tuples

 
There are many ways of creating tuples, you need to put the number of expressions in round brackets to indicate the start and end of the tuple and separated by commas. Below are the various ways of creating Tuples,
 
Understanding Tuples In Python
 
Understanding Tuples In Python
 

Empty tuple

 
T1 and T2 tuple represent Empty Tuple.
 
T= ( ) or T= (value,…) ; This construct is called Tuple Display Construct.
 

Single Element tuple

 
T3, T4 and T5 tuple from the above example represent Single Element Tuple.
 
In T3, (1) is treated as an integer value, so it stores an integer, not a tuple.
 
T4 and T5, will both create tuples.
 

Long tuple

 
T6 is treated as a Long Tuple, as ut takes many values, you can split it across several lines.
 

Nested tuple

 
If a tuple contains an element which is a tuple itself then it is Nested Tuple, T7 is a Nested Tuple.
 
Tuple T7 hs three values: 11, 12 and (13,14)
 

Tuples from Existing Sequences

 
Tuple T8 is created from another sequence- a string “hello”. It generates individual elements from the individual.
 
Tuple T9 is created from another sequence- a list L. It generates individual elements from the individual elements of the passed list L
 

Taking Input from Keyboard

 
Tuple T10 is created by using each of the character inputs.
 
tuple( ) around input( ), even if you do not put a parenthesis, it will create a tuple using individual characters as elements.
 

TUPLE OPERATIONS

 
Understanding Tuples In Python
 

Joining/ Concatenation

 
The + operator, concatenation operator is used to joining two tuples.
 
Understanding Tuples In Python
 
We can join more than two tuples to form a new tuple.
 
Understanding Tuples In Python
 
When + operator is used both the operands should be of tuple types. You cannot add a number or any other value to a tuple.
 
Understanding Tuples In Python
 
In case you want to add just one element into the tuple, then you must follow the below type,
 
Understanding Tuples In Python
 

Repeating/ Replicate

 
You can use * operator to replicate a tuple a specified number of times.
 
Understanding Tuples In Python
 

Slicing the Tuples

 
Slicing refers to extracting a subpart of the mentioned sequence. Tuple- slices are the same as the List- slices.
 
Syntax
 
seq= T[start:stop]
 
Tuple slice seq has elements of tuple T on indexes start, start+1, start+2… ,stop-1
 
Understanding Tuples In Python
 
Tuple slice is a tuple itself.
 
Giving upper limit or lower limit way too high or low will return a tuple within the range of the maximum or minimum size.
 
Understanding Tuples In Python
 
Tuples also support slice steps too.
 
Syntax
 
seq= T[start:stop:step]
 
Understanding Tuples In Python
 

Comparing Tuples

 
You can compare two tuples using operators (>, >=, <, <=, !=). Python compares individual elements of two tuples.
 
Understanding Tuples In Python
 

Unpacking Tuples

 
Packing- Creating tuples from a set of values.
 
Unpacking- Creating individual values from a tuple’s element.
 
 

Deleting Tuples

 
The del statement is used to delete elements and objects. But, since tuples are immutable as discussed above, individual items of a tuple cannot be deleted.
 
Understanding Tuples In Python
 
But you can delete a complete tuple with the del statement:
 
Understanding Tuples In Python
 
The error indicates that the tuple mentioned does not exist now, i.e., it has been deleted.
 

TUPLE METHODS

 
Understanding Tuples In Python
 

len()

 
This method returns the length of a tuple, so it returns the count of the number of elements present in the tuple.
 
Understanding Tuples In Python
 

max()

 
This method returns the element from the tuple having the maximum value.
 
Understanding Tuples In Python
 

min()

 
This method returns the element from the tuple having minimum value.
 
Understanding Tuples In Python
 

index()

 
This method returns the index of the mentioned and existing element.
 
Understanding Tuples In Python
 

count()

 
This method returns the number of times the mentioned element occurred.
 
Understanding Tuples In Python
 

tuple()

 
This method is also a constructor. It can be used to create tuples from different values.
 
Entering argument is optional, which returns a tuple and no argument returns an empty tuple.
 
Understanding Tuples In Python
 

Conclusion

 
In the next chapter, we will start learning Python Dictionary.
Author
Aashina Arora
335 5.3k 544.4k
Next » Python Dictionary