Continued from last night. 【Caution】 ["Data Scientist Training Course at the University of Tokyo"](https://www.amazon.co.jp/%E6%9D%B1%E4%BA%AC%E5%A4%A7%E5%AD%A6%E3 % 81% AE% E3% 83% 87% E3% 83% BC% E3% 82% BF% E3% 82% B5% E3% 82% A4% E3% 82% A8% E3% 83% B3% E3% 83 % 86% E3% 82% A3% E3% 82% B9% E3% 83% 88% E8% 82% B2% E6% 88% 90% E8% AC% 9B% E5% BA% A7-Python% E3% 81 % A7% E6% 89% 8B% E3% 82% 92% E5% 8B% 95% E3% 81% 8B% E3% 81% 97% E3% 81% A6% E5% AD% A6% E3% 81% B6 % E3% 83% 87% E2% 80% 95% E3% 82% BF% E5% 88% 86% E6% 9E% 90-% E5% A1% 9A% E6% 9C% AC% E9% 82% A6% I will read E5% B0% 8A / dp / 4839965250 / ref = tmm_pap_swatch_0? _ Encoding = UTF8 & qid = & sr =) and summarize the parts that I have some doubts or find useful. Therefore, I think the synopsis will be straightforward, but please read it, thinking that the content has nothing to do with this book.
A function has at least one of the following properties ⓪ Follow the instructions and perform the prescribed processing ① Collect and standardize a series of processes (2) Pass an argument and get the return value
③ The basic format is as follows
def do_nothing():
pass
do_nothing()
result do nothing
def hello_world():
print('hello world')
hello_world()
result
hello world
def hello_world(arg='hello_world'):
print(arg)
hello_world('hello_world')
hello_world('good morning')
result
hello_world
good morning
def hello_world(*args):
print(args)
hello_world('hello_world','good morning')
result In-function and output will be tuple
('hello_world', 'good morning')
def hello_world(**kwargs):
print(kwargs)
hello_world(day='hello_world',morning='good morning', evening='good evening')
result The inside and output of the function will be a dictionary, the argument name will be the key of the dictionary, and the value of the argument will be the value of the dictionary.
{'day': 'hello_world', 'morning': 'good morning', 'evening': 'good evening'}
def calc_multi(a, b):
return a * b
print(calc_multi(3,5))
result
15
fibonacci
def calc_fib(n):
if n ==1 or n== 2:
return 1
else:
return calc_fib(n-1) + calc_fib(n-2)
for n in range(1,21):
print(calc_fib(n),end=' ')
result
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
As shown below, a function without a function name is called an anonymous function lambda.
print((lambda a, b:a*b)(3, 5))
result
15
def calc_double(x):
return x * 2
for num in [1, 2, 3, 4]:
print(calc_double(num))
result
2
4
6
8
Calculate [1, 2, 3, 4] at once with map function and output with list
print(list(map(calc_double, [1, 2, 3, 4])))
result
[2, 4, 6, 8]
You can replace a normal function with lambda and output it in one line
print(list(map(lambda x : x*2, [1, 2, 3, 4])))
result
[2, 4, 6, 8]
print((lambda x : sum(x))(i for i in range(1,51)))
print((lambda x : sum(x))(range(1,51)))
print( sum(i for i in range(1,51)))
print( sum(range(1,51)))
print(reduce(lambda x, y: x + y, range(1,51)))
result
1275
a = [-1, 3, -5, 7, -9]
print(list(filter(lambda x: abs(x) > 5, a)))
result
[7, -9]
The reduce function is the result of sequential addition of elements
from functools import reduce
print(reduce(lambda x, y: x + y, a))
result
-5
【reference】 Basic Python functions (map () filter () reduce ())
・ Function basics
・ How to use * args
and ** kwargs
・ I tried to arrange how to use anonymous functions lambda, map function, filter function, and reduce function.
It is easy to understand if you go back to the basics and arrange them side by side.
Recommended Posts