~ Tips for beginners to Python ③ ~

Introduction

This article is the 25th day article of Python Advent Calendar 2016. A lot of fear is the last day !!

Introduction ②

I usually write for the server side as an engineer while representing a company called Liaro. This is a continuation of Tips② that I wrote a long time ago, but please understand that it is not as much as before due to various chasers. I think that there are many parts written with ambiguous knowledge, so thank you for your cooperation!

Forces of map, filter, reduce, lambda

Although it is another language, Python also has map / filter / reduce, which is provided in the lambda of an anonymous function (lambda expression).

python:Python3.5.2


>>> f = lambda x:x**2
>>> f(3)
9
>>> 
>>> a = [i for i in range(10)]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = map(lambda x:x*x, a)
>>> b = map(lambda x:x*x, a)
>>> for i in b:
...     print(i)
... 
0
1
4
9
16
25
36
49
64
81
>>> c = filter(lambda x:x%2==0, a)
>>> for i in c:
...     print(i)
... 
0
2
4
6
8
>>> from functools import reduce #import reduce
>>> d = reduce(lambda x,y:x+y, a)
>>> d
45
>>> 

Decorator

Python has a function called a decorator, and you can extend the method like @ hoge above the method name (here is detailed. ). The example of the link destination is good, and other than that, it will be similar without thinking much, Suppose you write the following when extending a function called foo.

python:Python3.5.2


>>> def foo(x, y):
...     return x ** y
... 
>>>
>>> def hoge(func): 
...     def inner(x, y):
...         if x == y == 0:
...             return "Indefinite"
...         return func(x, y)
...     return inner
... 
>>>
>>> foo = hoge(foo)

In Python, it can be rewritten as follows using the @ symbol.

python:Python3.5.2


>>> @hoge
... def foo(x, y):
...     return x ** y
... 
>>>

For example, a method in a class is basically an instance method, but writing @ classmethod makes it a class method, and writing @staticmethod makes it a static method.

About the default value of the function

In the previous Tips, I wrote about the handling of arguments in basic functions, but I didn't write some notes, so I'd like to write them here.

python:Python3.5.2


>>> def foo(x, y):
...     return x ** y
... 
>>> foo(y=5, x=2) #Name matching takes precedence over order for arguments
32
>>> 
>>> def hoge(a=[]):
...     a.append(1)
...     return a
...
>>> b = hoge()
>>> print(b)
[1]
>>> b = hoge()
>>> print(b) #The default value is not generated every time you call
[1, 1]

A scripture called pycodestyle

This is a Python coding convention. Python has recommended coding conventions for the language and I think many Python users follow them. Formerly called PEP8, it has changed. However, there is basically no problem because people suddenly became spicy in English and came out with PEP8 (PEP8). To enumerate some ・ Indent is 4 spaces ・ The length of one line is basically 79 characters -Write variables and functions in lowercase (snake) and class names in camel ・ Import separates lines And so on. I think you should read it once!

Finally

The content is very thin, but I want to write a sequel again ... Then Merry Xmas !! & Have a nice year ~~! !! !! !! !! !! !!

Click here for Tips ① Click here for Tips②

Recommended Posts

~ Tips for beginners to Python ③ ~
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
python textbook for beginners
Tips for Python beginners to use the Scikit-image example for themselves
OpenCV for Python beginners
Memo # 4 for Python beginners to read "Detailed Python Grammar"
The fastest way for beginners to master Python
Python for super beginners Python for super beginners # Easy to get angry
Memo # 1 for Python beginners to read "Detailed Python Grammar"
~ Tips for Python beginners from Pythonista with love ① ~
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Introduction to Programming (Python) TA Tendency for beginners
Memo # 6 for Python beginners to read "Detailed Python Grammar"
How to make Python faster for beginners [numpy]
~ Tips for Python beginners from Pythonista with love ② ~
Memo # 5 for Python beginners to read "Detailed Python Grammar"
Learning flow for Python beginners
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
[Python + Selenium] Tips for scraping
Introduction to Python For, While
Tips for Python beginners to use the Scikit-image example for themselves 9 Use from C
Tips for Python beginners to use the Scikit-image example for themselves 6 Improve Python code
python tips
How to convert Python # type for Python super beginners: str
[For beginners] How to study Python3 data analysis exam
python tips
Python # How to check type and type for super beginners
Python Tips
Python tips
Tips for Python beginners to use Scikit-image examples for themselves 5 Incorporate into network apps
Tips for Python beginners to use the Scikit-image example for themselves 2 Handle multiple files
Python Exercise for Beginners # 2 [for Statement / While Statement]
Python for super beginners Python # dictionary type 1 for super beginners
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
Tips for calling Python from C
Python #len function for super beginners
Beginners use Python for web scraping (1)
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
An introduction to Python for non-engineers
python beginners tried to find out
How to learn TensorFlow for liberal arts and Python beginners
Tips for coding short and easy to read in Python
How to convert Python # type for Python super beginners: int, float
[For beginners] Script within 10 lines (4. Connection from python to sqlite3)
[Python] Introduction to graph creation using coronavirus data [For beginners]
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
2016-10-30 else for Python3> for:
Python Conda Tips
python [for myself]
Updated to Python 2.7.9
INSERT into MySQL with Python [For beginners]
Tips to make Python here-documents easier to read
[Python] Minutes of study meeting for beginners (7/15)
Answer to AtCoder Beginners Selection by Python3
Memo to ask for KPI with python