
- 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
Variable-length arguments in Python
You may need to process a function for more arguments than you specified while defining the function. These arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.
Syntax
Syntax for a function with non-keyword variable arguments is this −
def functionname([formal_args,] *var_args_tuple ): "function_docstring" function_suite return [expression]
An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call.
Example
#!/usr/bin/python # Function definition is here def printinfo( arg1, *vartuple ): "This prints a variable passed arguments" print "Output is: " print arg1 for var in vartuple: print var return; # Now you can call printinfo function printinfo( 10 ) printinfo( 70, 60, 50 )
Output
When the above code is executed, it produces the following result −
Output is: 10 Output is: 70 60 50
- Related Articles
- Demonstrating variable-length arguments in Java
- How to use variable-length arguments in a function in Python?
- Variable length arguments for Macros in C
- Command Line and Variable Arguments in Python?
- Variable Arguments (Varargs) in C#
- Variable number of arguments in C++
- What are variable arguments in java?
- Variable number of arguments in Lua Programming
- How to Count Variable Numbers of Arguments in C?
- Variable Length Argument in C
- Required arguments in Python
- Keyword arguments in Python
- Default arguments in Python
- How to use variable number of arguments to function in JavaScript?
- Python - Convert 1D list to 2D list of variable length

Advertisements