Defined Functions
DEF greet_user (): Print ( " the Hello " ) greet_user () # after the definition of the functions and classes PEP8 two blank spaces
1. passing arguments to functions
def greet_user(username): print("Hello, " + username.title() + "!") greet_user('ges')
2. arguments and parameters
When the function definition parentheses called variable parameter, eg: username; function call or a variable value becomes the argument in parentheses, EG: ' GES '.
The argument values passed to the function call parameter, run the function thereof.
Pass arguments
1. Location argument
Function call, each argument in the function call is associated to the function definition in a parameter, based on the associated argument sequential manner - Location argument.
def describe_pet(animal_type, pet_name): print("\nI have a " + animal_type + '.') print("My " + animal_type + "'s name is " + pet_name.title() + ".") describe_pet('dog', 'pipi')
operation result:
I have a dog. My dog's name is Pipi.
Argument position: automatically from left to right the first real function call parameters passed to the first parameter, the second argument to the second parameter, sequentially. . .
- Function can be called multiple times: In order to improve efficiency, then define a function that can be called multiple times, just using different arguments can be.
- Note that the order of the arguments: the order of the arguments should be consistent with the order of the parameter, or will farce.
2. Keyword argument
describe_pet(animal_type='dog', pet_name='pipi') describe_pet(pet_name='pipi', animal_type='dog')
Run a result of the two function call code is exactly the same.
Passed to the parameter name - value pairs, when the function call without regard to the order of arguments.
3. Default value
def describe_pet(pet_name, animal_type='dog'): print("\nI have a " + animal_type + '.') print("My " + animal_type + "'s name is " + pet_name.title() + ".") describe_pet(pet_name='wangwang') # 使用默认值 describe_pet('wangwang') # 使用默认值 &Argument position describe_pet (pet_name = ' Mimi ' , animal_type = ' CAT ' ) # override the default value of the parameter, the new argument value transfer
operation result:
I have a dog. My dog's name is Wangwang. I have a dog. My dog's name is Wangwang. I have a cat. My cat's name is Mimi.
4. Equivalent function calls
Position can be mixed arguments, argument a keyword and default values, forming a plurality of operation results consistent function call - referred to as the equivalent function call.
5. Avoid argument error
def describe_pet(pet_name, animal_type): print("\nI have a " + animal_type + '.') print("My " + animal_type + "'s name is " + pet_name.title() + ".") describe_pet()
operation result:
Traceback (most recent call last): File "C:\Users\yxf\Desktop\python_pycharm\test_def.py", line 6, in <module> describe_pet() TypeError: describe_pet() missing 2 required positional arguments: 'pet_name' and 'animal_type'
When the arguments and parameters do not match, an error message will be operating details, according to the code modification message.