[Effective Python study memo] Chapter 1: Python-style thinking (Pythonic Thinking)

Introduction

This is a memo of the items that I personally learned by reading Effective Python. The "Foreword" of the book says "You can freely browse between items", but I will read them in order from the front. In addition, it seems that the second edition will be released on 2020/07/26, but please note that we will handle books released in 2016 here. image.png Effective Python-59 items to improve Python programs (Japanese) Large book – 2016/1/23

Chapter 1: Pythonic Thinking

Python programmers prefer to be explicit, choose simplicity over complexity, and maximize readability. Programmers familiar with other languages ​​try to write Python as if it were C ++, Java, or the language they are most familiar with.

I read Chapter 1 later and then read back this preface, but I thought I couldn't grasp the "Python style" because some specifications and expressions I didn't know came out.


Item 2: Follow the PEP8 style guide

  • Expressions and sentences (excerpts from some items)

Well, I feel like I've done this quite a bit. .. I'm sorry I won't do it anymore. ..


Item 5: Know how to slice a sequence

Slices also properly handle the start and end subscripts that cross list boundaries. Therefore, it is easy to write a code that sets the maximum length in consideration of the input sequence.

The following is feasible. It means that you can easily realize the code to extract up to 20 elements regardless of the sequence length by slicing.

a = ['a', 'b', 'c', 'd', 'e', 'f']
a[:20]

Execution result ['a', 'b', 'c', 'd', 'e', 'f']

I was ashamed to say that I didn't know the following. ..

The slice lengths to be assigned do not have to be the same. The slice values ​​are preserved before and after the assignment.

I will try it.

a = ['a', 'b', 'c', 'd', 'e', 'f']
a[2:4] = [1, 2, 3, 4, 5]
print(a)

Execution result ['a', 'b', 1, 2, 3, 4, 5, 'e', 'f']

Really, it's done.


Item 6: Do not use start, end, stride in one slice

# somelist[start:end:stride]
a[2::2]
a[-2::-2]
a[-2:2:-2]
a[2:2:-2]

The point is that the stride part of the slice syntax is extremely confusing. (...) To avoid this problem, don't use stride with start and end subscripts.


Item 8: Avoid more than one expression in list comprehensions

In item 7, it is recommended to use list comprehension instead of map and filter, but list comprehension is not appropriate when there are many expressions. For example, in the following cases.

my_lists = [[[1, 2, 3], [4, 5, 6]]]
flat = [x for sublist1 in my_lists for sublist2 in sublist1 for x in sublist2]

The number of lines saved doesn't convince you of the hassle later.

This is really it.


Item 12: Avoid using else blocks after for and while loops

When a break statement is executed in a loop, the else block is actually skipped.

I did not know. .. (I never tried to write it)

for i in range(3):
    print('loop: %d' % i)
    #Do not break inside the loop
else:
    print('Else block!')

Execution result loop: 0 loop: 1 loop: 2 Else block!

for i in range(3):
    print('loop: %d' % i)
    if i == 1:
        #Break in a loop
        break
else:
    print('Else block!')

Execution result loop: 0 loop: 1

It's true…! It says that the else block immediately after the loop should not be used because its behavior is not intuitive.

Summary

I personally wrote down the points that made me feel good.

in conclusion

I learned a lot from Chapter 1 because there were many sections that I could think of. I would like to summarize after this. Not all items are summarized, so if you are interested, please consider purchasing a book (not a turner)

Recommended Posts

[Effective Python study memo] Chapter 1: Python-style thinking (Pythonic Thinking)
"Effective Python 2nd Edition" Chapter 1 <Pythonic Thinking>
Effective Python Memo Item 3
"Effective Python 2nd Edition" Chapter 3 <Functions>
Python & Machine Learning Study Memo: Environment Preparation
Python memo
python memo
Python memo
Effective Python memo Item 10 Enumerate from range
python memo
[Online] Everyone's Python study session # 55 Rough memo
Python memo
Python & Machine Learning Study Memo ③: Neural Network
Study memo 1_eclipse (python) download, python execution, OpenPyXL download
Pythonic thinking
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Python & Machine Learning Study Memo ⑥: Number Recognition
Python memo
Python memo
Python & Machine Learning Study Memo ②: Introduction of Library
Python & Machine Learning Study Memo ⑦: Stock Price Forecast
[Python] Memo dictionary
Python study note_002
Python study notes _000
LPIC101 study memo
python beginner memo (9.1)
Python study notes_006
Python study note_004
★ Memo ★ Python Iroha
[Python] EDA memo
Python 3 operator memo
Python study note_003
Python study notes _005
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
Python study notes_001
[Python] Numpy memo
Python study day 1
Python learning memo for machine learning by Chainer from Chapter 2
Introduction to Mathematics Starting with Python Study Memo Vol.1