May 2021
Intermediate to advanced
142 pages
3h 37m
English
Python allows you to specify arguments with defaults in your function signatures. For example, consider the use_exclamation argument in the print_greeting function:
| | def print_greeting(name, use_exclamation=False): |
| | greeting = "Hello " + name |
| | if use_exclamation: |
| | greeting += "!" |
| | print(greeting) |
In the above example, the use_exclamation argument to the print_greeting function has a default value of False. Since use_exclamation defaults to False, print_greeting("David") outputs Hello David (with no trailing ! character). If we provided a value for the use_exclamation argument, however, the output changes. print_greeting("David", use_exclamation=True) outputs Hello David! because the ...