Variable Length Arguments
Swarooprani Manoor
Mentored 2K+ Students | 20 Years of Experience in Education & Mentorship | Passionate About Python Coding
Greetings!!
Welcome back to another exciting edition of Python Programming!
We will explore a special feature associated with Python's User defined functions - Variable Length Arguments.
Imagine you have written a function to add 2 integers
def add_two_numbers(num1, num2):
? print("Addition of ", num1, " and ", num2, " is ", num1 + num2)
num1 = int(input("Provide one integer: "))
num2 = int(input("Provide another integer: "))
add_two_numbers(num1,num2)
Output
Provide one integer: 88
Provide another integer: 78
Addition of 88 and 78 is 166
That's fantastic! It works for finding sum of any two given integers.
Now, a new requirement arises and your code should be able to find sum of 3 numbers also.
So you write one more function which can take 3 arguments and provide the sum.
def add_two_numbers(num1, num2):
? print("Addition of ", num1, " and ", num2, " is ", num1 + num2)
def add_three_numbers(num1, num2, num3):
? print("Addition of ", num1, " and ", num2, " and ", num3, " is ", num1 + num2+num3)
num1 = int(input("Provide one integer: "))
num2 = int(input("Provide another integer: "))
print("Do you wish to add one more integer? (y/n) ")
choice=input()
if choice == 'y':
? num3 = int(input("Provide the third integer: "))
? add_three_numbers(num1, num2, num3)
else:
? add_two_numbers(num1, num2)
Output
Provide one integer: 55
Provide another integer: 11
Do you wish to add one more integer? (y/n)
y
Provide the third integer: 22
Addition of 55 and 11 and 22 is 88
This works both for addition of two integers as well as three integers. Because there are 2 different versions which are designed to find sum of two integers and 3 integers independently.
But what if another requirement arises wherein your program should also be capable of adding 4 integers also.
What will you do now?
Obviously you think of writing another function add_four_numbers which takes 4 arguments and find sum of 4 arguments.
Wait!!!!! How many versions are you going to implement in your program like this? This doesn't seem a good idea to proceed with such practice anymore.
Is there something which can save us in such scenarios?
YES!!!! That's called variable length arguments.
This is a fantastic feature offered by Python Programming. It gives us flexibility of shrinking/expanding the number of arguments in the same function. No need to write multiple functions to accommodate variable length of arguments.
领英推荐
Let's see how can we do it.
def add_numbers(*numbers):
? sum = 0
? for i in numbers:
? ? sum = sum + i
? print("Sum is ", sum)
add_numbers(66,22)
add_numbers(70,56,58)
add_numbers()
add_numbers(9,78,90,5,35,1)
Output
Sum is 88
Sum is 184
Sum is 0
Sum is 218
Isn't it flexible! It works for any number of integers.
While fixed-length arguments are useful in specific situations, they may have some drawbacks in practical scenarios:
a. Limited Flexibility: Functions with fixed-length arguments can only accept a pre-defined number of inputs. If you need to handle a varying number of values, you'll have to define multiple functions with different argument counts.
b. Code Repetition: Writing separate functions for different argument counts can lead to code repetition and maintenance overhead. This can make the codebase harder to manage and update.
c. Inconvenience for Users: Users of your functions may find it inconvenient to remember the exact number and order of arguments to pass when calling the function.
Is it possible to combine Fixed Length and Variable Length Arguments?
def print_details(name, *scores):
print("Name:", name)
print("Scores:", scores)
print_details("Sujatha", 90, 85, 78, 92)
Output:
Name: Sujatha Scores: (90, 85, 78, 92)
Closing Remarks
Variable length arguments are a powerful feature in Python that makes functions more flexible and adaptable. It allows you to create functions that can accept any number of input values, making your code more efficient and user-friendly.
With this newfound knowledge of variable length arguments, you can now create functions that work with a variety of inputs and impress your friends with your Python skills! Keep exploring and experimenting with Python, and the possibilities will be endless!
Happy coding!
?????????????- Om ??? Shanti ?? With Over 30+ years in IT Experience and looking for new adventures now! - Systems Analyst Programmer, Leicester, England, UK ????
1 年????????