Module 6: Python Functions
Intermediate 16 chapters
71. Introduction to Functions
Meet functions: named, reusable blocks of code. Learn why they exist, how to define and call a simple one, and how they make programs shorter, clearer and easier to fix.
72. Defining Functions
Learn how to define functions with def: naming, parameters, the body, return values, docstrings and local variables, with clear examples and common mistakes.
73. Calling Functions
Learn all the ways to call a function: with arguments, using the returned value, inside expressions, from other functions and in loops, plus what happens when the call is wrong.
74. Function Parameters
Learn what parameters are, how they differ from arguments, and how Python matches values to them, including the different kinds of parameters a function can have.
75. Positional Arguments
Understand how Python matches arguments to parameters by position, why order matters, and how to unpack lists and tuples into positional arguments.
76. Keyword Arguments
Learn to pass arguments by name, mix them with positional ones, unpack dictionaries with **, and use keyword-only parameters for clearer function calls.
77. Default Arguments
Learn how default parameter values make arguments optional, the rule about ordering, and the famous trap of using a mutable object such as a list as a default.
78. Variable-Length Arguments
Learn how a function can accept any number of positional or keyword arguments by using *args and **kwargs in its parameter list.
79. *args and **kwargs
Learn how `*args` and `**kwargs` collect extra positional and keyword arguments, and how the same stars unpack lists and dictionaries when you call a function.
80. Return Statement
Learn how the `return` statement sends a result back to the caller, ends the function early, returns several values as a tuple, and why a missing return gives `None`.
81. Scope of Variables
Understand where names live in Python: local, enclosing, global and built-in scopes, the LEGB lookup order, and why variables made inside a function disappear afterwards.
82. Local and Global Variables
Learn where Python looks for names, the difference between local and global variables, and how to use the global and nonlocal keywords safely.
83. Lambda Functions
Learn to write small anonymous functions with lambda, and use them with sorted, map, filter and max for short, clear code.
84. Recursion
Understand recursion: functions that call themselves, with a base case and a recursive case. Learn factorial, Fibonacci, sums of nested lists, memoization and the recursion limit.
85. Higher-Order Functions
Learn higher-order functions: functions that take functions as arguments or return them. Covers map, filter, sorted, functools.reduce and partial, and writing your own.
86. map(), filter() and reduce()
Learn map(), filter() and functools.reduce(), three tools that apply a function to every item, keep only some items, or combine items into one value.