Variable Length Arguments

Variable Length Arguments

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!




Hitendrakumar Govindbhai Mistry

?????????????- Om ??? Shanti ?? With Over 30+ years in IT Experience and looking for new adventures now! - Systems Analyst Programmer, Leicester, England, UK ????

1 年

????????

要查看或添加评论,请登录

Swarooprani Manoor的更多文章

  • 'self' is just a convention

    'self' is just a convention

    Yes! In Python, self is just a convention, not a strict keyword. You can technically use any valid parameter name in…

  • How to Find the Most Frequent Item in a List Using Python

    How to Find the Most Frequent Item in a List Using Python

    Imagine you run an online store, and you want to find out which product is selling the most. Manually counting…

  • Python Formatting Tricks for Beginners

    Python Formatting Tricks for Beginners

    Ever wondered how to print things neatly in Python? ?? Sometimes, we need our numbers and text to align perfectly. But…

  • Python Lists: Organizing Your Data Just Got Easier

    Python Lists: Organizing Your Data Just Got Easier

    What do a shopping list, your weekly schedule, and a list of movies to watch have in common? They all require…

  • Augmented Assignment Operators

    Augmented Assignment Operators

    Python is known for its simplicity and efficiency, and one of the ways it achieves this is through augmented assignment…

  • What is list comprehension?

    What is list comprehension?

    Imagine you just took a test and want to see who scored above 70. How can you quickly give them bonus points? Let’s…

  • What’s map()?

    What’s map()?

    It’s a tool that lets you apply a function to each item in an iterable effortlessly. Python’s function lets you apply a…

  • Python pillow

    Python pillow

    Welcome to the world of Python Pillow, the fun way to play with pictures in Python! The Pillow library in Python is a…

  • Python File Handling: Confirming File Existence

    Python File Handling: Confirming File Existence

    One of the fundamental tasks when working with files in Python is checking if a file exists before performing any file…

  • for vs while: When to use which?

    for vs while: When to use which?

    and loops are both control flow structures used in programming to execute a block of code repeatedly. Each has its own…

社区洞察

其他会员也浏览了