
Advance your understanding of Python functions in this intermediate course by reviewing fundamentals and mastering positional and keyword arguments, required and optional arguments, *args and **kwargs, and generator functions.
Explore how Python functions work: define and call functions in the repl, distinguish function names from calls, and learn that print returns None.
Learn to pass functions as arguments to other functions, distinguish parameters from arguments, and call a function by name in a repeat five times example using print.
Explore how to define a function with a parameter named person, distinguish parameters from arguments, and see how the argument becomes the value inside the box in the greet example.
compare positional and keyword arguments by rewriting a greet function with host and guest, showing how host says hello to guest, and how order or naming affects calls.
Mix positional and keyword arguments in Python, and see when they work together. Learn that positional arguments come first, and keyword arguments must follow; starting with keywords is not allowed.
Explore how Python handles positional and keyword arguments, including using the sep parameter in print, swapping argument order, and the benefits of keyword arguments for clarity.
In Python, the contact list project uses a list of dictionaries to store contacts. Build an add contact function that appends new dictionaries, using positional and keyword arguments.
Explore how to use Python's pprint to print nested data structures legibly, and master positional and keyword arguments, including mixing rules and typical errors with a sample add_contact function.
Learn how to make Python function arguments optional by adding a default value, using a greeting parameter to customize output while preserving compatibility when omitted.
Explore how Python orders function parameters, mixing positional and keyword arguments, and why defaults must follow required parameters to avoid ambiguity.
Explore the contact list project by refactoring the add contact function to support optional last name and email via default values, positional and keyword arguments, and flexible data handling.
Explore how dictionaries differ from lists by focusing on key-value mappings rather than order, showing that two dictionaries are equal if their key-value pairs match, regardless of order.
Explore the mutable default value pitfall in Python, where a default list is shared across calls; fix by using None and creating a new list inside the function.
Explore how Python assigns default values to parameters, making some arguments optional, then see how add_contact builds lists and how print uses sep and end with defaults.
Explore unpacking in Python by printing lists with multiple arguments, unpacking lists into variables, iterating dict items, and handling errors when counts mismatch.
Explore advanced unpacking in Python by refining print usage with sep and end, and illustrate dictionary unpacking with * and **, laying the groundwork for args and kwargs.
Define a function with def and its name, then call it with parentheses, and distinguish parameters from arguments, including positional and keyword forms and default values.
Demonstrate handling a host and any number of guests with *args, comparing fixed versus optional positional parameters, and explaining how to use lists for scalable arguments.
Uncover how the *args pattern in Python collects multiple positional inputs into a tuple, as shown with host and guests, and how that tuple becomes iterable in for loops.
Explore how to use any number of optional keyword arguments with **kwargs in Python, map guest names to seat numbers using a dictionary, and loop through entries to print seating.
Master how to combine *args and **kwargs to build dynamic greetings for multiple hosts and guests, using tuple handling, unpacking, and string formatting.
Learn to collect and display contacts across multiple lists using a show all contacts function with *args, iterating through tuples of dictionaries and printing each entry.
Tidy up the contact list display by looping through dictionaries to print key-value pairs with f-strings, and add a display context function for clearer multi-list output.
Explore how to use **kwargs to extend a contact list project with arbitrary details, turning keyword arguments into a flexible dictionary of additional details such as subject, colleagues, and honors.
Explore how function signatures combine positional and keyword arguments, and how a rogue asterisk enforces keyword-only parameters after it. Compare this with the forward slash and common call errors.
Revisit how *args collects remaining positional arguments into a guests tuple, making them non-keyword; learn that args and the rogue asterisk serve the same purpose for keyword-only parameters.
Replace return with yield to turn a normal function into a generator, producing values one at a time via next calls, exposing generator objects and stop iteration.
Explore generator functions by building a numbers generator that yields values one at a time with yield inside a for loop, learning how this avoids storing all values in memory.
learn how a single generator function creates independent generator objects that yield values at their pace. explore multiple generators from the same function, iteration, and the possibility of infinite generators.
Explore how docstrings document Python functions, describe parameters and return values, and enable help utilities. Learn to write multi-line docstrings, import modules, and view signatures.
Learn how type hints annotate function parameters and return values in Python, why they are useful, and how tooling like PyCharm uses hints to improve autocomplete and error detection.
Return values appear as a list of strings, and type hints annotate the return value to declare it as a list.
Walk through end-of-course insights on Python functions, emphasizing naming with verbs to express actions, ensuring readability, and splitting complex tasks into single-purpose functions.
This course covers Python functions at intermediate and advanced levels. Once students have mastered the fundamentals of defining and calling functions, they're ready to start exploring more advanced topics. This course enables students to write more flexible and powerful functions and to be better placed to use functions in the standard library and third-party packages.
Functions allow you to package code so you can re-use it flexibly and efficiently whenever and wherever you need to. Understanding all the options available when creating parameters and passing arguments is key to be able to define and use functions effectively.
This course covers the following topics:
Positional and keyword arguments, and the requirements when mixing argument categories.
Optional arguments with default values
*args and **kwargs
Positional-only and keyword-only arguments using / and *
Generator functions using the `yield` keyword
---
About me, your instructor
I've been teaching Python for a decade. Before that, I worked as a physicist and used programming in my research work for over a decade.
My focus is on communicating clearly, in a friendly and relaxed manner. I'm the author of the The Python Coding Book (you can ask Google for a "python book" and you'll find this book as one of the first entries) and I have taught Python to individuals and corporations around the world.
And I'm approachable. You can ask me questions and I'll always reply, whether here on social media or anywhere else you can find me!