
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to write functions in Python that accept any number of arguments
Problem
You want to write a function that accepts any number of input arguments.
Solution
The * argument in python can accepts any number of arguments. We will understand this with an example of finding out the average of any given two or more numbers. In the below example, rest_arg is a tuple of all the extra arguments (in our case numbers) passed. The function treats the arguments as a sequence in performing average calculation.
# Sample function to find the average of the given numbers def define_average(first_arg, *rest_arg): average = (first_arg + sum(rest_arg)) / (1 + len(rest_arg)) print(f"Output \n *** The average for the given numbers {average}") # Call the function with two numbers define_average(1, 2)
Output
*** The average for the given numbers 1.5
# Call the function with more numbers define_average(1, 2, 3, 4)
Output
*** The average for the given numbers 2.5
To accept any number of keyword arguments, use an argument that starts with **.
def player_stats(player_name, player_country, **player_titles): print(f"Output \n*** Type of player_titles - {type(player_titles)}") titles = ' AND '.join('{} : {}'.format(key, value) for key, value in player_titles.items()) print(f"*** Type of titles post conversion - {type(titles)}") stats = 'The player - {name} from {country} has {titles}'.format(name = player_name, country=player_country, titles=titles) return stats player_stats('Roger Federer','Switzerland', Grandslams = 20, ATP = 103)
Output
*** Type of player_titles - <class 'dict'> *** Type of titles post conversion - <class 'str'>
'The player - Roger Federer from Switzerland has Grandslams : 20 AND ATP : 103'
Here in above example, player_titles is a dictionary that holds the passed keyword arguments.
If you want a function that can accept both any number of positional and keyword-only arguments, use * and ** together
def func_anyargs(*args, **kwargs): print(args) # A tuple print(kwargs) # A dict
With this function, all of the positional arguments are placed into a tuple args, and all of the keyword arguments are placed into a dictionary kwargs.
- Related Articles
- How can we write any number between any two irrational number that is not recurring?
- How to use named arguments in JavaScript functions?
- How to pass arguments to anonymous functions in JavaScript?
- How to use Spread Syntax with arguments in JavaScript functions?
- Does main() method accept arguments other than string array, in java?
- How to write string functions in Java?
- How can I find the number of arguments of a Python function?
- What are optional arguments in JavaScript Functions?
- Random Number Functions in Python
- How to use variable number of arguments to function in JavaScript?
- Number of Lines To Write String in Python
- How to add command line arguments in Python?
- Variable number of arguments in C++
- How can you execute functions with multiple arguments at a terminal?
- How to pass arguments by value in Python function?
