name: number can’t be the first symbol, but almost any unicode symbol can be used
return: if none, then returns None
Syntax 3
Functions are first-class objects in Python.
What does it mean?
1️⃣ What is First-Class object
Created at runtime
Assigned to a variable or element in a data structure
Passed as an argument to a function
Returned as the result of a function
Integers, strings, dictionaries, and functions in Python
🚀 Function as an object
🚲 Let’s create a function
Let’s create a function 1
Let’s create a function 2
Let’s create a function 3
Let’s upgrade the function!
📦 Packing and unpacking
We did it!
The solution
🧐 How to apply the function to a collection? (list, tuple, set)
The answer: unpacking
Let’s get back…
👀 What’s wrong?
Hint: when do default values initialize?
How to solve it?
The solution
🤔 How-to: require key argument
🚀 Assignment (unpacking)
Let’s look inside one time only
Functions wrap up
basic syntax
minimal example
packing, unpacking
👁 Scopes
LEGB Rule: Local → Enclosing → Global → Built-in
The search is done during execution, not definition.
🤔 Is Python a functional programming language?
Guido van Rossum
I have never considered Python to be heavily influenced by functional languages, no matter what people say or think. I was much more familiar with imperative languages such as C and Algol 68 and although I had made functions first-class objects, I didn’t view Python as a functional programming language.
We have anonymous functions!
map applies the given function to every iterable (sequence element)
filter removes any element, which is not satisfying the given predicat
zip builds tuple sequence from elements of multiple sequences