Unit: Functions in Python
1. The def Statement and Returning Values
Definition: A function is a reusable block of organized, related code that performs a specific task.
Syntax:
def function_name(parameters):
"""Docstring: Describe what the function does"""
# function body
return value # optionaldefkeyword starts the function definition.- Function name follows standard identifier rules.
parametersare optional inputs.- The body is indented.
returnpasses a value back to the caller (optional).
Example:
def greet():
"""Print a greeting message."""
print("Hello, World!")
greet()2. Parameters and Arguments
Parameters: Variables listed in the function definition. Arguments: Values passed to the function during the call.
Example:
def add(a, b):
return a + b
result = add(3, 5) # Arguments 3, 5 passed to parameters a, b
print(result) # Output: 83. Local Variables and Other Notes
Local variables: Variables defined inside a function, accessible only within that function.
Example:
def func():
x = 10 # Local variable
print(x)
func()
# print(x) # Error! x is not accessible here4. Global Variables and the global Statement
Global variables: Defined outside functions, accessible throughout.
Use global keyword to modify global variables inside functions.
x = 5 # Global variable
def modify():
global x
x = 10
modify()
print(x) # Output: 105. Docstrings for Functions and Decorators
Docstrings:
Multi-line string used to document functions, accessible via function_name.__doc__.
def sum_numbers(a, b):
"""Return the sum of two numbers."""
return a + b
print(sum_numbers.__doc__)Decorators: Functions that modify the behavior of other functions.
def decorator(func):
def wrapper():
print("Before function call")
func()
print("After function call")
return wrapper
@decorator
def greet():
print("Hello!")
greet()6. Lambda, Iterators, and Generators
Lambda expressions: Anonymous functions with one expression.
square = lambda x: x * x
print(square(5)) # 25Iterators:
Objects that implement __iter__() and __next__() methods.
Generators:
Functions that yield values using yield.
def countdown(n):
while n > 0:
yield n
n -= 1
for val in countdown(3):
print(val) # 3, 2, 17. Modules and Docstrings for Modules
Modules:
Python files (.py) that contain functions, classes, and variables to be reused.
Importing modules:
import math
print(math.sqrt(16)) # 4.0Docstrings for modules: Top of the Python file to document module purpose.
8. Packages
Definition:
A collection of Python modules organized in directories with an __init__.py file.
Usage: Use packages to organize related modules into a hierarchy.
These notes cover function creation, usage, scope management, documentation, decorators, lambda, iterators/generators, and module/package basics with syntax and examples for thorough understanding.
Say “next” to continue with the “Object-Oriented Programming” unit.
Footnotes
-
https://www.freecodecamp.org/news/python-functions-define-and-call-a-function/ ↩
-
https://www.tutorialspoint.com/python/python_functions.htm ↩
-
https://www.simplilearn.com/tutorials/python-tutorial/python-functions ↩
-
https://codeskiller.codingblocks.com/library/articles/functions-in-python-syntax-with-and-without-arguments ↩
-
https://cs.stanford.edu/people/nick/py/python-function.html ↩