An Introduction in Code
# for x, y will be returned (f(x)=x^2)
def f_x(x):
return x * x
print(f_x(2)) // 4
print(f_x(3)) // 9
def higherOrder(secondFunc):
return secondFunc
def f():
return "f-function"
higherOrder(f) # <function f at ...>
higherOrder(f)() # f-function
f1 = lambda x, y : x * y
def f2(x, y):
return x * y
numbers = [1, 2, 3]
doubledNumbers = list(map(lambda x: x * 2, numbers))
print(doubledNumbers)
# [2, 4, 6]
Xs = [1, 2, 3, 4, 5, 6]
Ys = [2, 4, 6, 8, 10, 12]
results = list(map(lambda x, y: x + y, Xs, Ys))
print(results)
# [3, 6, 9, 12, 15, 18]
numbers = [1, 2, 3, 4]
even = list(filter(lambda x: x % 2 == 0, numbers))
print(even)
# [2, 4]
from functools import reduce
numbers = [1, 2, 3, 4]
sum = reduce(lambda x, y: x+y, numbers)
print(sum) # 10
num = 10
num = 5
# num is 5 now, even though the int is immutable
from typing import Final
num: Final = 10
num = 5
nums = [1, 2, 3]
print(id(nums)) # 140703355859008
nums[0] = 10
print(id(nums)) # 140703355859008
print(nums) # 10, 2, 3