Python Tuples

Python Tuples are the collection of multiple data like the list. The main difference between the list and the tuple is that the value of a tuple element can’t be updated means tuple is unchangeable.
Python Tuples are declared like this:

Syntax

tuple_name=('a','d','e')

In python programming, tuples are written with the help of parentheses ()

Printing a Tuple

Printing a tuple is simple like printing a variable.

Example of printing a tuple

my_tuple = ("PHP", "Python", "Java")
print(my_tuple)
Tutorials Class - Output Window

(‘PHP’, ‘Python’, ‘Java’)

Topics to remember about Python Tuples

Tuples in Python are ordered and have a definite count. The elements in a tuple are indexed according to a definite sequence and the indexing of a tuple is done with 0 being the first index. We can slice the tuples, concatenate them, so many things.

Printing a specific element from the Tuples

Just like the list, we can access a specific element as well by specifying it’s index number.

Example to print a specific element from tuple

my_tuple = ("PHP", "Python", "Java")
print(my_tuple[1])
Tutorials Class - Output Window

Python

Printing elements in between some range from the tuple

Same as the list, we can access the element in some range as well.

Example of accessing elements in range from the tuple

my_tuple = ('T','u','t','o','r','i','a','l','s','C','l','a','s','s')
# elements 4th to 8th
print(my_tuple[3:8])
Tutorials Class - Output Window

(‘o’, ‘r’, ‘i’, ‘a’, ‘l’)

Modyfing or updating elements of a tuple

We can’t modify element from the tuple directly. If we try to do the so, then python will show the error.

Example

my_tuple = ("PHP", "Python", "java")
my_tuple[0] = "JavaScript"
print(my_tuple)
Tutorials Class - Output Window

Traceback (most recent call last):
File “even.py”, line 2, in
my_tuple[0] = “JavaScript”
TypeError: ‘tuple’ object does not support item assignment

Checking length of a Python Tuple

With the help of len() function we can check the length of the Tuple.

Example to count length of a tuple

my_tuple = ('T','u','t','o','r','i','a','l','s','C','l','a','s','s')
print(len(my_tuple))

Delete the tuple

In Python programmming, we can delete the whole tuple as well.

Example to delete the tuple

my_tuple = ("apple", "banana", "cherry")
del my_tuple
Tutorials Class - Output Window

Here you can see that the output is blank because we have deleted the tuple. So, it will not return anything.

Checking if any item exists in Python Tuple

Like list, we can easily check if a element is present in the tuple or not.

Example to check element is present

my_tuple = ("Ramesh", "Suresh", "Rakesh")
if "Ramesh" in my_tuple:
  print("Yes, 'Ramesh' is in this tuple")
else:
   print("No, 'Ramesh' is not in this tuple")
Tutorials Class - Output Window

Yes, ‘Ramesh’ is in this tuple

Counting how many times does a specific value exists in Python Tuple

With the help of count() method, we can also count the frequency of a element in a tuple.

Example to count the number of times does a value exists in a tuple.

my_tuple = (1,2,2,1,2,2,1,"Rakesh")
print(my_tuple.count(2))
Tutorials Class - Output Window

4

Here you can see, that specific value present 4 times in our tuple.

Finding the maximum value in the Tuple

Like the list, we can also find the maximum value in a tuple by using max() function.

Example to find out the maximum number in the tuple

my_tuple = (1,2,2,1,2,3)
print(max(my_tuple))
Tutorials Class - Output Window

3

Finding the minimum value in the Tuple

With the help if the min() we can also find the minimum value in a tuple.

Example to find out the minimum value from tuple

my_tuple = (1,2,2,1,2,3)
print(min(my_tuple))
Tutorials Class - Output Window

1

Finding the sum of values in the Tuple

We can easily the sum of all the values of a tuple by using sum() method.

Example to calculate the sum of tuple

my_tuple = (1,2,2,1,2,3)
print(sum(my_tuple))
Tutorials Class - Output Window

11

Finding the average of values in the Tuple

Like the list, we can calculate the average easily.

Example to calculate the average of tuple values

my_tuple = (1,2,2,1,2,3)
print(sum(my_tuple)/len(my_tuple))
Tutorials Class - Output Window

1.8333333333333333

Finding the position of any value in Python Tuple

In the Tuple, we can also check at what position does a specific element exist by using index() function.

Example to find out the position of a specific element in the tuple

my_tuple = ("Ramesh","Suresh","Kamlesh")
print(my_tuple.index("Suresh"))  # return 1, so the position will be second
Tutorials Class - Output Window

1


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.