Build awareness and adoption for your software startup with Circuit.

Python Tuple: Tuple Functions and Tuple Operations in Python

A brief discussion on Python tuples with examples.

Do you know what is a Python Tuple? In this article, we will discuss the Python Tuple with examples in brief.

Python Tuple is one of the main data types categorized under sequences.

What is a tuple in Python?

A Tuple in Python is represented as a list of values separated with commas enclosed within parentheses ( round brackets). It can store values of any data type.

For example, (“mango”, “apple”, “dragon fruit”, ” grapes “) is a tuple.

Tuple Operations in Python

It is the computation or actions applied to the variable containing tuple data types in an expression.

Like List manipulation, Tuple manipulation in Python can be done using various operators like concatenation (+), repetition (*), slicing of the tuple, and membership operators( in /not in). So, Let’s understand each operator in brief.

1. Concatenation operator (+)

The (+) operator is used to add to two tuples.

The syntax of the given operation is: Tuple1+Tuple2

t1=(12, 34, 56)
t2=(78, 90)
print(t1+t2)

#Output
(12, 34, 56, 78, 90)

2. Repetition operator (*)

Like string and list, (*) operator replicates the element of the tuple of specified times.

The syntax of the given operation: Tuple*n

t1=(12, 34, 56)
print( t1*3)

#Output
(12, 34, 56, 12, 34, 56, 12, 34, 56)

3. Comparison Operator

Python offers standard comparison operators like ==,<, >, != to compare two lists.

For comparison, two tuples must-have elements of comparable types, otherwise, you will get an error.

Python gives the result of comparison operators as True or False and moreover, it compares tuple list element by element. It compares the first element if they are the same then will move to the next, and so on.

4. Membership Operator (in, not in)

The membership operator checks whether an element exists in the given tuple sequence.

  • in: Return True if an element exists in the given tuple; False otherwise
  • not in: Return True if an element does not exist in the given tuple; False otherwise.
t1=(12, 34, 56, 78, 90)
#membership operator
56 in t1
12 not in t1

#Output
True
False

Tuple Functions in Python

Python offers many built-in methods and functions that perform specific actions on the given tuple. Let’s discuss these functions:

1. The max() method

This tuple method returns the element which has a maximum value from the given tuple.

Syntax: max( sequence)

t1 =( 12, 34, 56, 78 )
print( max(t1))

#Output
78

2. The min() method

The min method() returns the element which has a minimum value from the given tuple.

Syntax: min( sequence)

t1 =( 12, 34, 56, 78 )
print( min(t1))

#Output
12

3. The index() method

The index() method returns the index of an existing element of the given tuple.

Syntax: <tuple>. index( <name>)

t1= ( 12, 34, 56, 78)
t1.index(56)

#Output
2

4. The count() method

The count() method returns the count of an element in a given sequence i.e., it counts occurrences of an element in the given tuple.

Syntax: <tuple>.count(<element>)

t1= (2, 4, 6, 7, 8, 3, 4, 6 ,7 , 29, 9 , 4)
t1.count(4)

#Output
3

5. The tuple() method

The tuple() method is a constructor method that is used to create tuples from different types of values.

Syntax: tuple()

list1=[12,34,56]
t1= tuple( list1)
print(t1)

#Output
(12,34, 56)

Frequently Asked Questions

What is the difference between tuple and list?

The major difference between a Python tuple and the list is that list is mutable which means it can be changed and we can modify its values. whereas a tuple is immutable, hence it can not be changed. That’s why they perform different operations and functions.

Conclusion

Python provides various data types with different functions that make coding in Python quite easier. Similarly, Python Tuple makes code more efficient, easier, and moreover, easy to understand by its various functions and operations.




Continue Learning