Saturday, October 4, 2025
HomeLanguagesTuple as function arguments in Python

Tuple as function arguments in Python

Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different cases can arise.

   Case 1: fnc(a, b) – Sends a and b as separate elements to fnc.

   Case 2: fnc((a, b)) – Sends (a, b), whole tuple as 1 single entity, one element.

   Case 3: fnc(*(a, b)) – Sends both, a and b as in Case 1, as separate integers.

The code below demonstrates the working of all cases :

Python3




# Python3 code to demonstrate working of 
# Tuple as function arguments
  
# function with default arguments 
def fnc(a=None, b=None):
    print("Value of a : " + str(a))
    print("Value of b : " + str(b))
  
# Driver code
if __name__ == "__main__" :
    
  # initializing a And b
  a = 4
  b = 7
  
  # Tuple as function arguments
  # Case 1 - passing as integers
  print("The result of Case 1 : ")
  fnc(a, b)
  
  # Tuple as function arguments
  # Case 2 - Passing as tuple
  print("The result of Case 2 : ")
  fnc((a, b))
  
  # Tuple as function arguments
  # Case 3 - passing as pack/unpack 
  # operator, as integer
  print("The result of Case 3 : ")
  fnc(*(a, b))


Output :

The result of Case 1 : 
Value of a : 4
Value of b : 7
The result of Case 2 : 
Value of a : (4, 7)
Value of b : None
The result of Case 3 : 
Value of a : 4
Value of b : 7
RELATED ARTICLES

Most Popular

Dominic
32334 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6704 POSTS0 COMMENTS
Nicole Veronica
11868 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6819 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS