Lesson 4 ❮ Lesson List ❮ Top Page
❯ 4.1 Functions
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 9m 2s
☷ Interactive readings 5m
✑ Practice 4.1 (G Colab) 20m
A function is a block of code that only runs when it is called.
In Python, a function is defined using the def keyword. Similar to for and if, the indented lines after that will be part of that function.
To call that function, use the function name followed by parenthesis ().
Information can be passed into functions as arguments.
Arguments are added after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
In a function, you can add as many arguments as you want, just separate them with a comma.
Some data or parameters might be incomplete. Adding a default value to a function can help with this. If we call the function without argument, it will use the default value:
You can send any data types of argument to a function (string, number, list, dictionary, etc.), and it will be treated as the same data type inside the function.
Don't use spaces around the = sign when used to indicate a keyword argument.
When a function has many arguments, add an extra indentation to distinguish arguments from the rest.
Functions should be in lower case (same format as variables).
The argument with default value has to be written last. In addition to that, when calling a function, the number of argument has to be at least the same as the non-default values.
You can fix the errors by moving mark before grade="?" and add an extra argument in print_stats("Andy"). You can also just add a default value to mark.