Recording intelligenceAI-generated brief · check the source for context

Function Arguments in Python | Python Tutorial - Day #21

13:49 recording · EN · 1 speaker

Watch the original

Executive Summary AI
  • In this episode of 100 days of code the instructor takes up function arguments and the different ways they can be passed, including as a tuple and as a dictionary.
  • After building a simple average function that prints (a + b) / 2 and gives 5.0, he names the four argument types: default, keyword, variable length and required.
  • Giving a = 9 and b = 1 as defaults makes average() print 5, while calling it with 1 and 5 ignores the defaults and prints 3, and keyword arguments such as b = 9, a = 21 let the order be changed freely.
  • Rewriting the function as def average(*numbers) collects the arguments into a tuple, so average(5, 6) prints 5.5 and any count of numbers works because the sum is divided by len(numbers), while **name collects key = value pairs into a dictionary.
  • Finally he replaces the print with return, stores the result in c, and prints 4.75 for 5, 6, 7, 1, explaining that without a return the caller gets None and that only the first return executed is used.

Brief overview

A 100 Days of Code lesson on Python's four argument types and the return statement.

  1. Four argument types in Python functionsDefault, keyword, variable length and required arguments are the four kinds named in the lesson.
  2. Defaults fill in, keywords free the ordera = 9, b = 1 are used when nothing is passed, and b = 9, a = 21 works regardless of position.
  3. Star collects a tuple, double star a dictionary*numbers arrives as a tuple and **name as a dictionary, shown with type() printing class tuple and class dict.

Questions this recording answers

10 questions, each answered where it is said
How many types of arguments can a Python function take?

Four types are named: default arguments, keyword arguments, variable length arguments and required arguments. In the average function written with a and b, both a and b are required arguments, because no default value was given for either of them in the function definition.

What are default arguments and how are they written?

A default argument is given its value in the definition itself, such as a as 9 and b as 1. Calling average with nothing then still works and gives 5, because the function assumes the default value whenever a value is not provided for that argument in the call.

What happens if I pass values for parameters that already have defaults?

The passed values win. With defaults a as 9 and b as 1, calling with 1 and 5 ignores 9 and 1, takes a as 1 and b as 5, and the answer is 3. Passing only a as 5 leaves b at 1, so 5 plus 1 by 2 prints 3.

How do keyword arguments let me ignore the order of arguments?

You provide arguments as key equals value, so the interpreter recognises each one by the parameter name and the order does not matter. Writing b equals 9 and a equals 21 makes a 21 and b 9. With plain arguments you must give a first and then b.

Which arguments must always be passed when calling a function?

Required arguments, the ones with no default. If a equals 9 is not written in the definition, a becomes required and you cannot escape giving its value, while b still falls back to its default. Without key equals value syntax, arguments must be in the correct positional order and match the definition.

How can one function accept any number of numbers?

Define it with a star before the parameter: def average star numbers. Inside, initialize sum from 0, write for i in numbers with sum equal to sum plus i, then print sum divided by length of numbers. Passing 5, 6, 7, 15 or 25 numbers causes no problem.

What does the star parameter actually contain inside the function?

It takes the numbers as a tuple. Calling average with 5 and 6 gives 5.5, and printing the type of numbers shows class tuple, so the values passed in arrive collected as a tuple. The difference between a tuple and a list is left for a later lesson.

How do I receive key equals value arguments as a dictionary?

Write double star before the parameter, as in star star name. The key equals value pairs passed to that function then arrive inside it as a dictionary called name, and printing the type of name prints class dict. The example prints hello with first name, middle name and last name.

What does return do, and how does it differ from printing?

Return sends the computed value back to the calling code instead of printing it. Writing return in average, then c equal to the call and print c, gives 4.75 for the numbers 5, 6, 7 and 1. Without a return you get none, because the function returns nothing.

What happens if a function has two return statements?

Only the first return is considered; the second, and every return placed after it in the function, is ignored. Whichever return is reached first finalises the value and the function goes back. Return means go back with that value, so returning 7 makes the call give 7.

Key Quote

“Default arguments, keyword arguments, variable length arguments”

— Function Arguments in Python | Python Tutorial - Day #21, at 1:56▶ watch at 1:56
Key Quote

“We can provide a default value while creating a function.”

— Function Arguments in Python | Python Tutorial - Day #21, at 2:41▶ watch at 2:41
Key Quote

“What you can do is change the order of the arguments.”

— Function Arguments in Python | Python Tutorial - Day #21, at 4:31▶ watch at 4:31
Key Quote

“Required arguments are those arguments that you have to give.”

— Function Arguments in Python | Python Tutorial - Day #21, at 5:17▶ watch at 5:17
Key Quote

“So what happened is that it took numbers as a tuple.”

— Function Arguments in Python | Python Tutorial - Day #21, at 7:56▶ watch at 7:56
Key Quote

“Return means go back with this value.”

— Function Arguments in Python | Python Tutorial - Day #21, at 11:59▶ watch at 11:59

Cite this

“Function Arguments in Python | Python Tutorial - Day #21.” https://www.youtube.com/watch?v=0d6b6fFuCkE. Transcript summary by WhipScribe, https://whipscribe.com/transcript-pages/function-arguments-in-python-python-tutorial-day-21.html.

Report this page
What is wrong with this page?