Python Functions Cheat Sheet
Python function fundamentals covering default arguments, *args/**kwargs, lambdas, type hints, scope rules, and higher-order functions.
1 PageBeginnerApr 2, 2026
Defining Functions
Default arguments and variable-length argument lists.
python
def greet(name, greeting="Hello"): return f"{greeting}, {name}!"def add_all(*args): # args is a tuple return sum(args)def make_profile(**kwargs): # kwargs is a dict return kwargsgreet("Alice") # 'Hello, Alice!'add_all(1, 2, 3) # 6make_profile(name="Bob", age=30) # {'name': 'Bob', 'age': 30}
Lambda Functions
Anonymous single-expression functions.
python
square = lambda x: x ** 2square(5) # 25data = [(1, 'c'), (2, 'a'), (3, 'b')]sorted(data, key=lambda x: x[1]) # sort by second element
Type Hints
Optional static typing annotations for parameters and return values.
python
def add(a: int, b: int) -> int: return a + bdef process(items: list[str], count: int = 0) -> None: ...from typing import Optionaldef find(name: str) -> Optional[int]: return None
Scope: global & nonlocal
Modifying variables from an enclosing scope.
python
def make_counter(): count = 0 def increment(): nonlocal count # refers to enclosing scope's count count += 1 return count return incrementcounter = make_counter()counter() # 1counter() # 2
Built-in Higher-Order Functions
Functions that take or return other functions.
- map(fn, iterable)- applies fn to every item, returns a map object (lazy)
- filter(fn, iterable)- keeps items where fn(item) is truthy
- functools.reduce(fn, iterable)- cumulatively applies fn to reduce to one value
- sorted(iterable, key=fn)- returns a new sorted list using fn as the sort key
- any(iterable)- True if at least one element is truthy
- all(iterable)- True if every element is truthy
- zip(a, b)- pairs up elements from two or more iterables
Pro Tip
Never use a mutable object like a list or dict as a default argument value — defaults are evaluated once when the function is defined, so all calls share the same object. Use `None` as the default and create the mutable object inside the function body instead.
Was this cheat sheet helpful?
Explore Topics
#PythonFunctions#PythonFunctionsCheatSheet#Programming#Beginner#DefiningFunctions#LambdaFunctions#TypeHints#ScopeGlobalNonlocal#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance