Python Notes #4 – Functions

local – Variables defined inside a function or a block are local to that function or block.
global – Variables defined at the top level of a module or file are global. global keyword is used to declare a variable that is defined outside of a function (…


This content originally appeared on DEV Community and was authored by Elvin Seyidov

local - Variables defined inside a function or a block are local to that function or block.
global - Variables defined at the top level of a module or file are global. global keyword is used to declare a variable that is defined outside of a function (i.e., in the global scope) and make it accessible within a function
nonlocal - Used inside nested functions to refer to variables in the nearest enclosing function (but not global).

1️⃣ Defining & Calling Functions
def keyword

Positional and keyword arguments

Return values (including None)

2️⃣ Function Parameters & Arguments
Positional arguments

Keyword arguments

Default values

*args and **kwargs

Argument unpacking

3️⃣ Scope & Lifetime
Local vs global scope

global and nonlocal keywords

Variable shadowing

4️⃣ First-Class Functions
Assigning functions to variables

Passing functions as arguments

Returning functions from functions

5️⃣ Lambda Expressions
Syntax & limitations

Use cases (sorting, filtering, mapping)

Comparison to def

6️⃣ Higher-Order Functions
map(), filter(), reduce(), sorted() with key

When to use comprehensions instead

7️⃣ Decorators
Basic decorator pattern

@decorator syntax

Stacking decorators

functools.wraps

Decorators with arguments

8️⃣ Closures
Functions that remember enclosing scope

Use cases for delayed execution, encapsulation

9️⃣ Recursion
Recursive function structure

Base case and stack overflow risk

Tail recursion (conceptual, not optimized in Python)

🔟 Type Hints in Functions
Annotating parameters and return types

Using Optional, Union, Callable, etc. from typing

1️⃣1️⃣ Anonymous vs Named Functions
When to use lambdas

Why avoid using lambdas in complex logic

1️⃣2️⃣ Introspection
name, doc, annotations

inspect module for advanced inspection

recursive functions - calculating factorial, fibonacci


This content originally appeared on DEV Community and was authored by Elvin Seyidov


Print Share Comment Cite Upload Translate Updates
APA

Elvin Seyidov | Sciencx (2025-03-25T18:19:08+00:00) Python Notes #4 – Functions. Retrieved from https://www.scien.cx/2025/03/25/python-notes-4-functions/

MLA
" » Python Notes #4 – Functions." Elvin Seyidov | Sciencx - Tuesday March 25, 2025, https://www.scien.cx/2025/03/25/python-notes-4-functions/
HARVARD
Elvin Seyidov | Sciencx Tuesday March 25, 2025 » Python Notes #4 – Functions., viewed ,<https://www.scien.cx/2025/03/25/python-notes-4-functions/>
VANCOUVER
Elvin Seyidov | Sciencx - » Python Notes #4 – Functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/25/python-notes-4-functions/
CHICAGO
" » Python Notes #4 – Functions." Elvin Seyidov | Sciencx - Accessed . https://www.scien.cx/2025/03/25/python-notes-4-functions/
IEEE
" » Python Notes #4 – Functions." Elvin Seyidov | Sciencx [Online]. Available: https://www.scien.cx/2025/03/25/python-notes-4-functions/. [Accessed: ]
rf:citation
» Python Notes #4 – Functions | Elvin Seyidov | Sciencx | https://www.scien.cx/2025/03/25/python-notes-4-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.