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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
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 :
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