Tuples – PYTHON

Share via:

Tuples

Tuple:
➔ A tuple is an inbuilt data type which is an ordered, immutable and lightweight collection of elements in python.
➔ The term immutable means once the tuple is created you can’t modify the size or the elements which are present in the tuple.
➔ The term ordered means the position of each element is fixed in the way that they are inserted.
➔ Tuples are considered as light weight because they consume less memory when compared with the sequence data types like lists.
➔ Tuples are enclosed in parentheses, and their elements can be accessed using index positions, starting from 0.

 

Creation of tuples.
You can create a tuple by placing all sequence elements in between parentheses.
Syntax: Tuple_name= (sequence of elements)
Examples:

 

Accessing the tuples
The elements in tuples can be accessed through indexing.
Tuples support both positive (starts with zero) and negative indexing (start with -1) which is from the back of the tuple i.e. 90 in the below example.
Examples:

 

Slicing the tuples
It is a way of extracting a part of the tuple with specified index ranges.
You can perform slicing with both the positive and negative index.
Syntax: Tuple_name[start :end :difference]
start: index from where you want to start slicing, it is inclusive.
end: index before where slicing ends, it is exclusive.
difference: it is the interval between the elements. By default, the value will be 1.
Examples:
names=(“anil”,”akash”,”arjun”,”akhil”,”bhupal”,”dheeraj”,”mahesh”)
If you want all the names starts with “a” i.e. from index 0 to index 3, then use names [0:4]
If you want names alternatively from beginning to till the end, then use names [0:6:2]

 

Deletion of tuple
You can’t delete the elements in tuple, but you can delete the entire tuple using del keyword.
Example:

 

Build in methods for tuples.
1. len ( ):
This method is used to get the length of the tuple i.e total number of elements in the tuple.
If the tuple has one more tuple inside it, then the inside tuple is considered as a single element irrespective of the number of elements in it.
Examples:

 

2. count ( ):
Count method is used to find the number of occurrences of a specified element in the given tuple.
The element can be of numbers or strings.
Examples:

 

3. index ( ):
This method is used to get the index of the first occurrence of the specified element in the tuple.
It will raise a value error when the specified value is not present in the given tuple.
Examples:

 

4. Tuple concatenation
It is the concatenation of 2 or more tuples into one single tuple.
You have to use the plus operator ( + ) to perform concatenation.
The order of the elements will stay in the way you add them.
Examples:

 

5. Tuple repetition
It is the way of cloning the data of a given tuple a number of times.
You have to use the repetition operator ( * ) to perform repetition.
Examples:

 

6. Sorted
It is a built-in function that is used to sort the elements of the same datatype.
If the elements in the tuple are not of the same data type while using a sorted function, then it will raise an error called TypeError.
This function works for both the numbers and strings as well.
Numbers are sorted in ascending order and Strings will be sorted in the alphabetical order.
As the tuples are immutable, they give a list as the output for this function.
Examples:

 

7. Math functions
As the tuple elements can be iterable you can use the functions like min(),max(),sum() on tuples.
Example:

 

Author    : Venkat Vinod Kumar Siram 
LinkedIn : https://www.linkedin.com/in/vinodsiram/
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.
KTExperts is always active on social media platforms.
Facebook  : https://www.facebook.com/ktexperts
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform
Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment