** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
positional_argument
def menu(food, drink, dessert):
print('food =', food)
print('drink =', drink)
print('dessert = ', dessert)
menu('beef', 'wine', 'ice cream')
result
food = beef
drink = wine
dessert = ice cream
This time it is entered properly, but
drink
to ʻice cream, I dare to mistake
dessert for
wine`.
argument
def menu(food, drink, dessert):
print('food =', food)
print('drink =', drink)
print('dessert =', dessert)
menu('beef', 'ice cream', 'wine')
result
food = beef
drink = ice cream
dessert = wine
I want to prevent such mistakes.
keyword_argument
def menu(food, drink, dessert):
print('food =', food)
print('drink =', drink)
print('dessert =', dessert)
menu(food='beef', dessert='ice cream', drink='wine')
result
food = beef
drink = wine
dessert = ice cream
By setting the keyword argument, it was printed properly without writing in order.
default_argument
def menu(food='beef', drink='wine', dessert='ice cream'):
print('food =', food)
print('drink =', drink)
print('dessert =', dessert)
menu()
result
food = beef
drink = wine
dessert = ice cream
If you set the default arguments and pass no arguments The set default argument is returned.
default_argument
def menu(food='beef', drink='wine', dessert='ice cream'):
print('food =', food)
print('drink =', drink)
print('dessert =', dessert)
menu(food='chicken', drink='orange juice')
result
food = chicken
drink = orange juice
dessert = ice cream
After setting the default arguments If you pass keyword arguments only for those you want to change from the default arguments, Only that part is changed and returned.
Recommended Posts