TUPLES in Python

 



A tuple is an order sequence of elements or a collection of data items enclosed within parentheses (), but the main difference is that Tuples are immutable which means once the tuple is created we cannot add or delete elements from them.

Tuples can hold values of any data type like int, string, float,  Boolean, or other Tuples.

In the below example, we have created an empty tuple, if we check the type of a tuple then a tuple will be returned.



Unlike lists, we cannot add or remove elements to a Tuple




the individual element of a Tuple is known as the fields of a Tuple, 
we can use the index number to Access the individual elements of a Tuple. In the below example, the index value [0] will bring in the value of 'Alabama'  likewise the index value [1] will bring in the value of 'Alaska'



since tuples are immutable, If we try to update the value of a Tuple then we will get an error as shown below.



We can also perform slicing operations on tuples in the below example "tuple1[0:3]"
will print out all the index ranges from index [0] to index [2] Please note that the index [3] will not be printed


We can use the tuple function to create a tuple of strings.


We can also check the presence of an element in a tuple using the in-operator






No comments: