[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.

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.

Chapter1-2 Python Basics

1-2-5 Function

1-2-5-1 Function basics

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

Simple function

③ The basic format is as follows

Function that does nothing
def do_nothing():
    pass
do_nothing()

result do nothing

Output hello world
def hello_world():
    print('hello world')
hello_world()

result

hello world
Output changes depending on the argument
def hello_world(arg='hello_world'):
    print(arg)
    
hello_world('hello_world')
hello_world('good morning')

result

hello_world
good morning
* args Output argument as tuple
def hello_world(*args):
    print(args)
    
hello_world('hello_world','good morning')

result In-function and output will be tuple

('hello_world', 'good morning')
** Output the kwargs argument as a dictionary
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'}
Function with a return value
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
1-2-5-2 Anonymous functions lambda and map functions

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
map function
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]
map function and lambda

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]
Exercise 1-2
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
filter and reduce functions
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 ())

Summary

・ 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

[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Introduction to Data Scientists] Basics of Python ♬
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Probability and Statistics ♬ Probability / Random Variables and Probability Distribution
# 4 [python] Basics of functions
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Scipy
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Pandas
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use graph drawing library ♬ Basics of Matplotlib
List of Python libraries for data scientists and data engineers
[Introduction to Data Scientists] Basics of scientific calculation, data processing, and how to use the graph drawing library ♬ Environment construction
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
[Introduction to Data Scientists] Descriptive Statistics and Simple Regression Analysis ♬
[Python] Summary of how to use split and join functions
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Introduction to Python3 Day 1] Programming and Python
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Introduction to Python Basics of Machine Learning (Unsupervised Learning / Principal Component Analysis)
[Introduction to Python] I compared the naming conventions of C # and Python.
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Introduction to TensorFlow-Summary of four arithmetic operations and basic mathematical functions
[Introduction to cx_Oracle] (Part 2) Basics of connecting and disconnecting to Oracle Database
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
Statistical basics and Python, graphing, etc. (memo)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Compress python data and write to sqlite
Introduction of Python
Basics of Python ①
Basics of python ①
#Python basics (functions)
Python basics: functions
Introduction of Python
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Introduction to cx_Oracle] (Part 3) Basics of Table Reference
[Python] How to read data from CIFAR-10 and CIFAR-100
[Introduction to Python] How to handle JSON format data
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
[Introduction to cx_Oracle] (5th) Handling of Japanese data
cv2 functions and data types (OpenCV python bindings)
[Introduction to Python] Basic usage of lambda expressions
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
[Introduction to cx_Oracle] (Part 9) DB and Python data type mapping (version 8 or later)
[Introduction to Python] How to get the index of data with a for statement
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
What to use for Python stacks and queues (speed comparison of each data structure)
Summary of tools needed to analyze data in Python
Full-width and half-width processing of CSV data in Python
Basics of Python scraping basics
[Chapter 5] Introduction to Python with 100 knocks of language processing
Reading Note: An Introduction to Data Analysis with Python
Introduction to Python language
How to use Python Kivy ① ~ Basics of Kv Language ~
[Chapter 3] Introduction to Python with 100 knocks of language processing