|
37 | 37 | "outputs": [], |
38 | 38 | "source": [ |
39 | 39 | "def identity(a, b): # only positional arguments\n", |
40 | | - " return a, b" |
| 40 | + " return a, b # we can return more than one thing" |
41 | 41 | ] |
42 | 42 | }, |
43 | 43 | { |
|
99 | 99 | "metadata": {}, |
100 | 100 | "source": [ |
101 | 101 | "::: {.callout-important }\n", |
102 | | - "Python functions **always** return a value, even without a `return` statement, in which case the return value will be `None`.\n", |
| 102 | + "Python functions **always** return a value, **even without** a `return` statement, in which case the return value will be `None`.\n", |
103 | 103 | ":::" |
104 | 104 | ] |
105 | 105 | }, |
|
150 | 150 | "id": "10383a76-442f-4ece-9a44-cbfcae681403", |
151 | 151 | "metadata": {}, |
152 | 152 | "source": [ |
153 | | - "This three definitions are equivalent:" |
| 153 | + "These three definitions are equivalent:" |
154 | 154 | ] |
155 | 155 | }, |
156 | 156 | { |
|
169 | 169 | " return\n", |
170 | 170 | " \n", |
171 | 171 | "def f():\n", |
172 | | - " print(\"hello\")\n", |
173 | | - " # The return statement is implicitly here " |
| 172 | + " print(\"hello\")" |
174 | 173 | ] |
175 | 174 | }, |
176 | 175 | { |
|
186 | 185 | "id": "cd15c9c6-2067-48ca-b35b-a715d2a762ad", |
187 | 186 | "metadata": {}, |
188 | 187 | "source": [ |
189 | | - "We can write a function with arbitrary arguments.\n", |
| 188 | + "We can write a function with an arbitrary (undefined) number of arguments.\n", |
190 | 189 | "For that, we use the syntax `*args`.\n", |
191 | 190 | "The `args` will be put into a tuple:" |
192 | 191 | ] |
|
347 | 346 | "general(1, 2, first=\"hello\", second=\"world\")" |
348 | 347 | ] |
349 | 348 | }, |
| 349 | + { |
| 350 | + "cell_type": "markdown", |
| 351 | + "id": "4f6cdcf8-c9f6-46e7-9cba-cfa35e2a67a9", |
| 352 | + "metadata": {}, |
| 353 | + "source": [ |
| 354 | + "::: {.callout-tip}\n", |
| 355 | + "Use key-word (named) arguments by default, it will make your code much more readable, maintainable and easier to debug.\n", |
| 356 | + ":::" |
| 357 | + ] |
| 358 | + }, |
350 | 359 | { |
351 | 360 | "cell_type": "markdown", |
352 | 361 | "id": "38e5d4a8-0df5-4c34-9dab-67f93dab46af", |
353 | 362 | "metadata": {}, |
354 | 363 | "source": [ |
355 | 364 | "## Functions are values\n", |
356 | 365 | "\n", |
357 | | - "We can assign functions to variables and pass them around, like any other object (people call this to have \"functions as first-class citizen\")." |
| 366 | + "We can assign functions to variables and pass them around, like any other object (people call this to have \"_functions as first-class citizen_\" in the language)." |
358 | 367 | ] |
359 | 368 | }, |
360 | 369 | { |
|
647 | 656 | "def early():\n", |
648 | 657 | " if 1 > 0:\n", |
649 | 658 | " return \"first condition\"\n", |
650 | | - " if 2 > 0: # This code will never be evaluated\n", |
| 659 | + " if 2 > 0: # This code will never be reached \n", |
651 | 660 | " return \"second condition\"" |
652 | 661 | ] |
653 | 662 | }, |
|
887 | 896 | "source": [ |
888 | 897 | "## Exercises\n", |
889 | 898 | "1) Write a function called `intro` that takes two positional arguments, name and age, and prints a sentence introducing a person. The function should return nothing.\n", |
890 | | - "2) Repeat it, but adding an optional argument, city. The function should now return the introducing string (consider handling the city)\n", |
| 899 | + "2) Repeat it, but adding an optional argument, city. The function should now return the introducing string and handle the city input too.\n", |
891 | 900 | "3) Write a function called `sort_dict_by_value` that takes a dictionary and returns a dictionary sorted by value.\n", |
892 | 901 | "4) Write a function that takes an arbitrary number of key-word __only__ arguments representing pairs (name, age) and returns a list of tuples sorted by age.\n", |
893 | 902 | "For example:\n", |
|
937 | 946 | "name": "python", |
938 | 947 | "nbconvert_exporter": "python", |
939 | 948 | "pygments_lexer": "ipython3", |
940 | | - "version": "3.10.13" |
| 949 | + "version": "3.13.3" |
941 | 950 | } |
942 | 951 | }, |
943 | 952 | "nbformat": 4, |
|
0 commit comments