First python [O'REILLY]

Overview

A memorandum for myself. I read O'REILLY's ** first python ** and listed what I thought was "usable".

Precautions when changing the list (Chapter 6 6.2.1 Overwriting shared references and objects)

For example, be careful when using the following list.

[No good]list_use.py


L = [1, 2, 3]     #List generation
M = L             #List copy
M[0] = 5          #Change the 0th element of M
print M
>>> [5, 2, 3]     #It ’s okay, it ’s changed.
print L
>>> [5, 2, 3]     #Hmm! ?? I didn't want to change the list of L ...

In python, when assigning a value to a variable (for example, x = 1), two objects, a box called a variable and an object called a value, are created. Variables store reference information to objects, and assignments between variables (such as y = x) copy the reference information.

So, if the variable L has the reference information of the object [1, 2, 3] and this is M = L, the variable M will store the reference information of the object [1, 2, 3]. Become. Therefore, changing M [0] = 5 is an act of making changes to the object that L also refers to, so even if you intend to change only M, you will also change L. This is a caveat only for objects with variability. It is the list dictionary class that has variability, so be careful of these as well. If you do, create a new object as follows.

[OK]list_use.py


L = [1, 2, 3]     #List generation
M = L[:]          #Copy all elements of the list
M[0] = 5          #Change the 0th element of M
print M
>>> [5, 2, 3]
print L
>>> [1, 2, 3]     #L is not affected by the change!

This and that of the dictionary (Chapter 8 8.4 Use of the dictionary)

Get a list of keys ~ D.keys () ~

dict_test.py


D1 = {'spam':2, 'ham':1, 'eggs':3}
print D1.keys()
>>> ['eggs', 'ham', 'spam']   
# D1.keys()Get a list of dictionary keys

#Useful when processing dictionaries in key order
L = D1.keys()
L.sort()
for x in L:
    print x, D1[x]

>>> eggs 3
>>> ham 1
>>> spam 2

Dictionary merge ~ D1.update (D2) ~

dict_marge.py


D1 = {'spam':2, 'ham':1, 'eggs':3}
D2 = {'milk':4, 'ham': 4}
D1.update(D2)
print D1
>>> {'eggs': 3, 'milk': 4, 'ham': 4, 'spam': 2} 
#New elements only in D2 are added, elements in both D1 and D2 are updated

Create a dictionary with two lists ~ dict (zip (L1, L2)) ~

create_dict.py


keys = ['spam', 'eggs', 'toast']
vals = [1, 3, 5]
D3 = dict(zip(keys, vals))
print D3
>>> {'toast': 5, 'eggs': 3, 'spam': 1}

Save / read an object with pickle (Chapter 9 9.2.3 Example of using a file object)

use_pickle.py


#Save with pickle
D = {'a':1, 'b':2}
import pickle
F = open('dump.pkl', 'w')
pickle.dump(D, F)          #Serialize with pickle
F.close()

#Load from pickle
F = open('dump.pkl', 'r')
E = pickle.load(F)         #Deserialize with pickle
print E
>>> {'a':1, 'b':2}

if liberal arts (Chapter 12 if statement)

How to write like if statement execution in dictionary. For example, the following checks for the presence of elements in the branch dictionary.

dict_if.py


branch = {'spam': 2, 'ham': 3, 'eggs': 1}
print branch.get('spam', 'nothing!')
>>> 2
print branch.get('bacon', 'nothing!')
>>> nothing!

while and for else statements (Chapter 13, 13.2 break, continue, pass statement and else block)

Use of else

You can use else in while and for statements. else will not be executed if it breaks out during the loop, but it will be executed if the loop is completed to the end. For example, the following usage example for primality test

while_else.py


y = 13
x = y /2
while x > 1:
    if y % x == 0:
        print y, 'has factor', x
        break
    x = x -1
else:
    print y, 'is prime'
#Break if there is a divisible number greater than 1. Otherwise, prime numbers and outputs in else.

Use \ * and \ * \ * as arguments (Chapter 16 16.5 Argument Variations)

If you define a function argument with \ * or \ * \ *, the argument can be received as a tuple or dictionary type. Useful when you are unsure how many variables are available when calling a function.

args_test.py


#The value of the argument becomes a tuple.
def f1(*args): 
    print args

f1()
>>> ()   
#An empty tuple is output.
f1(1)
>>> (1, ) 
#Only 1 tuple is output.
f1(1, 2, 3, 4)
>>> (1, 2, 3, 4) 
# 1, 2, 3,4 tuples are output


def f2(**args): 
    print args

f2()
>>> {}
#An empty dictionary is output
f2(a = 1, b = 2)
>>> {'a': 1, 'b': 2}
# {'a': 1, 'b': 2}Dictionary is output

Function system (Chapter 17 Advanced techniques related to functions)

lambda expression

The name is a word used in LISP and seems to be derived from λ. Like def, it is used to define a function.

lambda_test.py


L = [(lambda x : x ** 2), (lambda x : x ** 3), (lambda x : x ** 4)]

for f in L:
    print f(2)
>>> 4
>>> 8
>>> 16

If you use def, it becomes the following and is redundant. Moreover, the operation cannot be grasped without checking the contents by jumping to each function.

def_use.py


def f1(x): retrun x ** 2
def f2(x): retrun x ** 3
def f3(x): retrun x ** 4

L = [f1(2), f2(2), f3(2)]

for f in L:
    print f
>>> 4
>>> 8
>>> 16

lambda is effective when used when using a simple function. Moreover, since the formula is in the immediate vicinity, visibility is good. It's often used in tensorflow sources, so it's worth remembering. But I can't understand it. .. Let's review this many times.

map function

Used when you want to apply the same function to a sequence such as a list [character string (character by character), dictionary, etc.].

map_use.py


def inc(x): return x + 10

L = [11, 12, 13, 14, 15]
print map(inc, L)
>>> [21, 22, 23, 24, 25]

Can also be used in combination with lambda.

map_lambda.py


L = [1, 2, 3, 4]
print map((lambda x: x + 3), L)
>>> [4, 5, 6, 7]

List comprehension

Like map, you can adapt a function to certain values and receive the result as a list.

list_loop1.py


#Simple loop
print [x ** 2 for x in range(5)]
>>> [0, 1, 4, 9, 16]

The same thing can be done with a map, but the basic list comprehension is simpler to write.

list_loop2.py


#You can also express double loops
print [x + y for x in 'spam' for y in 'SPAM']
>>> ['sS', 'sP', 'sA', 'sM', 'pS', 'pP', 'pA', 'pM', .. , 'mA', 'mM']

list_loop3.py


#You can also combine it with if
print [(x, y) for x in range(5) if x % 2 == 0 for y in range(5) if y % 2 == 1]
>>> [(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]

list_loop4.py


#Also good at handling matrix
M = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

N = [[2, 2, 2],
     [3, 3, 3],
     [4, 4, 4]]

#Take out the second line of M
print [row[1] for row in M]
>>> [2, 5, 8]

#Multiply each element
print [[M[row][col] * N[row][col] for col in range(3)] for row in range(3)]
>>> [[2, 4, 6], [12, 15, 18], [28, 32, 36]]

generator

Although it is in a function, the result can be executed at any time without returning the result in one shot. There is no return and yield is described.

genesquare.py


#Creating a generator
def gensquares(N): 
    for i in range(N): 
        yield i ** 2

#Execution using an iterator
x = gensquares(5)  #Assign generator to x
print x.next()
>>> 0
print x.next()
>>> 1
print x.next()
>>> 4
print x.next()
>>> 9
print x.next()
>>> 16
print x.next()
>>> error ...

Import processing flow (Chapter 18 18.3 Import processing)

Import refers to the following flow.

    1. Search for the module to be imported.
  1. Compile the found file and convert it to bytecode (.pyc).
    1. Execute the bytecode and create the object of the module to be imported.

1. 1. Find the module to be imported.

Search in the directory of sys.path (import sys; print sys.path). sys.path consists of the following four.

① Working directory of the execution program (2) Directory set in the environment variable PYTHONPATH ③ Directory of standard library modules (library group [sys, math, etc.] packed from the beginning) ④ The directory described in the .pth file (it seems to be myconfig.pth, but I couldn't find it in my environment ...)

For example, in the case of math, ③ is in the path obtained by the following math.file. This will be one of the standard library paths.

>>> import math; dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> math.__file__
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/math.so'

On the contrary, if this math.so is mv from the directory of the standard library, math cannot be used.

MacBook-Pro:hoge$ sudo mv /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/math.so ./
cd ..

MacBook-Pro:work fuga$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named math

2. Compile the found file into bytecode (.pyc).

If you already have .pyc, don't bother to bytecode it. However, if the time stamp of the .py file to be imported is larger than the .pyc, bytecode it. (Because .pyc seems to be old) However, when it comes to creating .pyc as a file, that is not the case. Just bytecode it for use in the current execution module. It does not output .pyc.

3. 3. Execute bytecode and create an object of the module to be imported.

Object generation from top to bottom.

About import (Chapter 19 Module import and reload)

Difference between import and from

The import target is module_test.py, and the import side is module_use.py.

module_test.py


def printer(x): 
    print x

module_use.py


#import reads the entire module
import module_test
module_test.pirinter('spam')
>>> spam

#from xxx import yyy reads the yyy variable of xxx
from module_test import printer
printer('spam')
>>> spam

Recommended Posts

First python [O'REILLY]
First time python
First Python 3 ~ First comparison ~
First time python
First Python ~ Coding 2 ~
First Python 3rd Edition
PyQ ~ Python First Steps ~
First Python image processing
[Python] Python / Scikit-learn's first SVM
First Python miscellaneous notes
[Python] Chapter 01-01 About Python (First Python)
First Fabric (Python deployment tool)
O'Reilly python3 Primer Learning Notes
First neuron simulation with NEURON + Python
First simple regression analysis in Python
Web scraping with Python First step
First Python 3 ~ Extra: Numerical Random Fantasy ~
[GUI with Python] PyQt5-The first step-
Prepare your first Python development environment
Python (from first time to execution)
C / C ++ programmer challenges Python (first step)
See python for the first time
The first step in Python Matplotlib
kafka python
Generate a first class collection in Python
Python basics ⑤
python + lottery 6
Python Summary
Python comprehension
Python technique
Studying python
Python memorandum
Python FlowFishMaster
Python service
python tips
First Flask
python function ①
Python basics
Python memo
ufo-> python (3)
Continuously play the first Python Sukusta MV
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
First draft
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables