An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
Python Function Arguments
An argument is a value that is passed to a function when it is called. It might be a variable, value, or object passed to a function or method as input.
In python, there are 3 different types of arguments we can give a function.
Default arguments: arguments that are given default values
Positional arguments: arguments that can be called by their position in the function definition
Keyword arguments: arguments that can be called by their name.
Default argument
We can give a default argument by using an assignment operator =
. This will happen in the function declaration.
Example:
def find_dinner_total(food, drinks, sales_tax=0.055, tip): print(food + drinks * rate +tip)
Our function is called find_dinner_total()
which will calculate the total cost for dinner . In our example our default argument is sales_tax=0.055
.
We can either choose to call the function without providing a value (this will result in the default value to be assigned) OR we can overwrite the default argument by entering a different value.
Positional argument
def find_diner_total(food, drinks, sales_tax, tip): print(food + drinks * sales_tax + tip
The first parameter passed is food
, second drinks
, third sales tax
, and fourth is tip
.
When our function is called, the position of the arguments will be mapped based on the positions that are defined in the function declaration.
Example:
# $70 total for food# $30 total for drinks# sales tax 0.055# 15 tipdiner_bill(70, 30, 0.055, 15)
Keyword Arguments
Now if we used Keyword Arguments, where we refer to what each argument is assigned to in the function call.
Example:
dinner_bill(drinks=30, food=70, tip=15,sales_tax=0.055
Original Link: https://dev.to/kster/python-function-arguments-3m8i
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To