?print-pdf' Created for
def greet(name):
print("Hello, {}".format(name))
#a function can be assigned to variable:
foo = greet
greet("Maria")
foo("Pesho")
#OUTPUT
# Hello, Maria
# Hello, Pesho
def greet(name):
print("Hello, {}".format(name))
# a function can be passed as argument to another function
def wrapper(f, n):
f(n)
wrapper(greet, "Alex")
#OUTPUT
# Hello, Alex
#a function can be returned as value from another function
def greet_wrapper(name):
def wrapper():
print("Hello, {}".format(name))
return wrapper
greet = greet_wrapper("Viktor")
greet()
decorated = decorator(undecorated)
def stars_decorator(f):
def wrapper():
print("*" * 50)
f()
print("*" * 50)
return wrapper
def greet():
print("Howdy World!")
# let's decorate greet:
greet = stars_decorator(greet)
# and use it:
greet()
@decorator_name) for calling higher-order functions.
#define the decorator:
def decorator():
pass
# define a function to be decorated:
@decorator
def decorated():
pass
# just call the decorated function:
decorated()
#define the decorator:
def stars_decorator(f):
def wrapper():
print("*" * 50)
f()
print("*" * 50)
return wrapper
# let's decorate greet:
@stars_decorator
def greet():
print("Howdy World!")
# and use it:
greet()
What about if we want the decorated function to take some arguments?
def stars_decorator(f):
def wrapper(n):
print("*" * 50)
f(n)
print("*" * 50)
return wrapper
def greet(name):
print("Howdy {}!".format(name))
# let's decorate greet:
greet = stars_decorator(greet)
# and use it:
greet("Pesho")
def log_decorator(f):
def wrapper(a,b):
print("LOG: The function was executed!")
return f(a,b)
return wrapper
@log_decorator
def sum(x,y):
return (x+y)
print( sum(2,3) )
def and return.
lambda parameter_list: expression
def some_func(parameter_list):
return expression
l = lambda x,y:x+y
print( l(2,3) )
maxnum = lambda x,y: x if x>y else y
print( maxnum(30,40) )
calc = {
'add': lambda x,y:x+y,
'del': lambda x,y:y!=0 and x/y,
}
print( calc['add'](2,3) )
print( calc['del'](2,3) )
filter() Functionfilter() Function
filter() is a built-in function that allows to process an iterable and extract those items that satisfy a given condition
filter(function, iterable)
def is_even(x):
return True if x!=0 and x%2==0 else False
even_numbers = filter(is_even, range(10))
print( list(even_numbers) )
# [2, 4, 6, 8]
even_numbers = filter(lambda x: x%2==0 if x!=0 else False, range(10))
print( list(even_numbers) )
names = ["Ivan", "", "Alex", "", "Maria", "Angel", ""]
not_empty_names = filter(lambda s: s, names)
print( list(not_empty_names) )
names = ["Ivan", "Alex", "Maria", "Angel", ""]
filtered_names = filter(lambda s: s and s[0]=="A", names)
print( list(filtered_names) )
names = ["Ivan", "", "Alex", "", "Maria", "Angel", ""]
not_empty_names = [ name for name in names if name]
print( list(not_empty_names) )
map() Functionmap() Function
map() return an iterator that applies function to every item of iterable, yielding the results
map(function, iterable, ...)
numbers = [1,2,3,4,5]
sqr_numbers = map(lambda x:x**2, numbers)
print( list(sqr_numbers) )
#[1, 4, 9, 16, 25]
letters = map(chr, range(1040, 1072))
print( list(letters) )
#['А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я']
letters = [chr(code) for code in range(1040, 1072)]
print( list(letter
#['А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я']
l1 = [1,2,3]
l2 = [1,2,3]
l_sum = map(lambda a,b: a+b, l1, l2)
print( list(l_sum) )
# [2, 4, 6]
reduce() Functionreduce() Function
reduce() allows us to apply a function to an iterable and reduce it to a single cumulative value
from functools import reduce
reduce(function, iterable[, initializer])
functools module!reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5)reduce(lambda x, y: x+y, [1, 2, 3, 4, 5], 100) calculates (((((100+1)+2)+3)+4)+5)
from functools import reduce
res = reduce(lambda a,b: a+b, range(11))
print(res)
#55
products = [
{'name':'apples', 'price': 2},
{'name':'oranges', 'price': 5},
{'name':'bananas', 'price': 3},
]
#note that here we must provide initializer
total_price = reduce( lambda curr_price, product:curr_price+product['price'], products, 0)
print(total_price)
l1 = [1,2,3]
l2 = [1,2,3]
l3 = [1,2,3]
l = map(lambda *t: reduce(lambda a,b: a+b, t) , l1, l2,l3)
print( list(l) )
# [3, 6, 9]
users = [
{'name':'Maria', 'balance': 2000},
{'name':'Petar', 'balance': -189},
{'name':'Ivan', 'balance': 3200},
{'name':'Asen', 'balance': -2890},
]
# YOUR CODE here
# EXPECTED OUTPUT:
['Maria', 'Ivan']
quotes = [
'Nothing travels faster than the speed of light, with the possible exception of bad news, which obeys its own special laws',
'A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.'
]
TRAVELS
THAN
THAT
TRYING
These slides are based on
customised version of
framework