Focusing on "Python Tutorial 3rd Edition", [Python Tutorial] on the Web (https://docs.python.org/ja/3.5) I also looked at /tutorial/index.html) and actually executed it. It took me a long time to take this note. The execution environment is mainly commands in CentOS7. Finally, I tried it on Jupyer Notebook, but it was easier for me personally.
Python is an interpreted language
(In shell) Start python3.5 interpreter * 3.5 points vary depending on the environment \ >>> quit () Exit the interpreter
python -c command [arguments] ...
Accessable by running "import sys" sys.argv
Primary prompt (>>>) Secondary prompt (...)
By default, Python source code is treated as UTF-8 encoded.
Cannot be changed | Can be changed | Repeatable | sequence | mapping | data | |
---|---|---|---|---|---|---|
bool | ◎ | True,False | ||||
int | ◎ | integer | ||||
float | ◎ | Floating point number | ||||
complex | ◎ | Complex number | ||||
str | ◎ | ◎ | ◎ | String | ||
list | ◎ | ◎ | ◎ | |||
tuple | ◎ | ◎ | ◎ | |||
range | ◎ | ◎ | ◎ | |||
dict | ◎ | ◎ | ◎ | |||
set | ◎ | ◎ | ||||
bytes | ◎ | ◎ | ◎ | binary | ||
bytearray | ◎ | ◎ | ◎ | |||
file object | ◎ | ◎ |
The operator +-* /% () is as the image of other languages
operator.
>>> 17 / 3; #division(In python2"//"Same result as
>>> 17 // 3; # ***Devaluation division***
>>> 5 *** 2; #5 squared
>>> 0%3 #0/Remainder of 3 → 0
3*3.72/1.5
In interactive mode, the last displayed expression is "_」(underscore)Is assigned to.
>>> price = 100.5
>>> tax = 10.5
>>> price * tax
>>> price + _ #In interactive mode, the last displayed expression is the variable "_Substitute in
>>> _ = 10000 #Substitution is possible, but it should be treated as read-only
Error when used without assignment to variable
>>> 'doesn\'t' #escape
>>> "doesn't" #Enclose in double quotes
>>> s = 'First\nSecond'
>>> print(s)
>>> print(r'C:\some\name') #raw string
Other(Not listed).
>>> a = 'Py'
>>> b = 'thon'
>>> c = 'Jython'
>>> a = b = c #Multiple assignment
>>> a #The last variable applies to everything
'Jython'
>>> b
'Jython'
>>> c
'Jython'
Triple quotes (double quotes or single quotes) can handle line breaks as they are.
>>> '''aaa #Example
... bbb
... ccc
... '''
'aaa\nbbb\nccc\n'
>>> print("""\ #Multiple lines 1
... Usage: ls
... -r
... -h
... """)
>>> print('''\ #Multiple lines 2
... Usage: ls
... -r
... -h
... ''')
Basically, + is used for string concatenation. (Spaces are possible, but errors other than variables and strings)
String concatenation.
>>> word = 'python'
>>> word2 = 'ium'
>>> 3 * 'un' + 'ium' #repetition
'unununium'
>>> 'Py' + 'thon' #The character is +,Any space can be combined
'Python'
>>> 'Py' 'thon'
'Python'
>>> word 'ium' #Variables and characters cannot be combined with spaces
File "<stdin>", line 1
word 'ium'
^
SyntaxError: invalid syntax
>>> word word2 #Variables cannot be combined with spaces
File "<stdin>", line 1
word word2
^
SyntaxError: invalid syntax
>>> word + word2 #Use + to combine variables and variables and characters
'pythonium'
Variable string manipulation(Index specification).
>>> word = 'python'
>>> word[2] #***0 is the first character***
't'
>>> word[-2] #***-1 is the last character***
'o'
Slicing slicing > String [Start index: End index: Number of steps]
Index is between letters(between)The left end of the first character is 0.
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
Variable string manipulation(Slicing(Cut out)Designation).
>>> word = 'python'
>>> word[0:2] #Includes first character but does not include last character
'py'
>>> word[2:5] #Includes first character but does not include last character
'tho'
>>> word[:2] #start:0 when omitted
'py'
>>> word[-2:] #End:If omitted, the size of the character string(Until the last character)
'on'
>>> word[:-2] #start:0 when omitted
'pyth'
>>> word[0:10:2] #Specify the number of steps * Although not mentioned in the book, questions will be asked in the mock exam
'pto'
>>> word[:2] + word[2:] #Equivalent to a string
'python'
Variable string manipulation(Out of range designation).
>>> word[42] #Index specification is NG
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[4:42] #Slicing specification is OK
'on'
Variable string manipulation(Change string).
>>> word[0] = 'J' #Python strings cannot be modified = immutable body(immutable)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 'J' + word[1:] #Need to generate a new string
'Jython'
Variable string manipulation(String length).
>>> word = 'dive\ninto\ncode\t'
>>> print(len(word))
15 #Control characters are counted as one character
>>> d = 'xxxxDIVExxxxDIVExxxxDIVE'
>>> print(d.replace('DIVE', 'CODE', 1))
xxxxCODExxxxDIVExxxxDIVE #Replace only the first value
>>> d
'xxxxDIVExxxxDIVExxxxDIVE' #No change to the original string
list.
>>> squares = [1,4,9,16,25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0]
1
>>> squares[2]
9
>>> squares[-1]
25
>>> squares[-3:]
[9, 16, 25]
>>> squares[:]
[1, 4, 9, 16, 25]
>>> squares[::]
[1, 4, 9, 16, 25]
>>> squares[:::]
File "<stdin>", line 1
squares[:::]
^
SyntaxError: invalid syntax
List concatenation.
>>> squares + [ 100, 1001 ]
[1, 4, 9, 16, 25, 100, 1001]
>>> squares2 = [ 8001, 8002 ]
>>> squares + squares2
[1, 4, 9, 16, 25, 8001, 8002] #List values do not change
List can be changed(mutable).
>>> squares[1] = 40000
>>> squares
[1, 40000, 9, 16, 25]
>>> squares.append(216) # append()Method
>>> squares.append(7**3)
>>> squares
[1, 40000, 9, 16, 25, 216, 343] #Add to the end of the list
>>> letters = ['a','b','c','d','e','f','g'] #Substitution to slicing is also possible
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:5] = ['C','D','E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> letters[:] = []
>>> letters
[]
for i in range(s): print(i)
range (10) # 1 and 3 arguments can be omitted if step by step from 0 range (5, 10) # 3rd argument can be omitted if step by step range (0, 10, 3) # 0 to 10 in 3 steps
>>> print(range(10)) # (Caution)The serial number of the value is not displayed
range(0, 10)
-As with C language, the break statement and continue statement each exit the loop and skip the rest of the loop to go to the next iteration. -The else clause in the loop is more like that of a try statement than that of an if statement. The tyr statement is executed when no exception occurs, and the loop is executed when *** break does not occur ***.
·do nothing. It is used when a syntactic error occurs.
When making the smallest class.
>>> class MyEmptyClass:
... pass
When you do not write specific code in the function or conditional statement when writing new code.
>>> def initlog(*args):
... pass
4.7.1. Default argument value The most convenient format is to specify a default value for one or more arguments.
def ask_default (a, b, c = 100): # 100 is set to c when the 3rd argument is omitted
Accumulate the arguments passed to the function in subsequent function calls.
>>> def f(a, L=[]):
... L.append(a)
... return L
...
>>> print(f(1))
[1]
>>> print(f(2))
[1, 2]
>>> print(f(3))
[1, 2, 3]
4.7.2. Keyword arguments The function can also be in the form of "keyword = value". When calling a function, the positional argument must come first and the keyword argument must come after.
>>> def fugafuga(title,content = 'default_content', number = 4):
... print(title, end=' ')
... print(content, end=' ')
... print(number)
...
...
>>> fugafuga(title = 'title_default', content = 'None', number = 5)
title_default None 5
>>> fugafuga(title = 'title_default', number = 5) #Keyword arguments can be omitted (arguments in the middle can be omitted)
title_default default_content 5
>>> fugafuga(1000) #Positional arguments are also possible (arguments in the middle cannot be omitted)
1000 default_content 4
4.7.3. Optional argument list The least used option is to specify that the function can be called with any number of arguments.
>>> def write_multiple_items(file, separator, *args):
... file.write(separator.join(args))
>>> concat("earth", "mars", "venus")
'earth/mars/venus'
>>> concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'
>>> def dive_into_code(teacher, *mentor):
... print(teacher)
... print(mentor)
...
>>> dive_into_code('Noro', 'Nakao', 'Miyaoka')
Noro #1st argument
('Nakao', 'Miyaoka') #After the second argument
4.7.4. Unpacking the argument list
4.7.5. Lambda expression
4.7.6. Unpacking the argument list Documentation string
Python has PEP 8 as a style guide that most projects follow.
The following points -Use 4 spaces for indentation and do not use tabs. The four whitespaces are just halfway between the small (deeply nested) indents and the large (easy to read) indents. Tabs are a source of confusion and should be eliminated. -Wrap lines so that the width of the source code does not exceed 79 characters. This will help users with smaller displays and even line up codes on larger displays. -Use blank lines to separate large code blocks within a function, class, or function. ・ If possible, write comments independently on the line. -Use docstring. • Put spaces before and after the operator and after the comma, not just inside the parentheses: a = f (1, 2) + g (3, 4). · Give classes and functions consistent names. By convention, CamelCase is used for class names and lower_case_with_underscores is used for function and method names. Always use self as the name of the first argument of the method. -Do not use eccentric encodings in code that you intend to use in an international environment. In any case, Python's default UTF-8 or plain ASCII works best. -Similarly, non-ASCII characters should not be used in identifiers if even the slightest amount of other language-speaking person may read or maintain the code.
List method append(x) extend(L) insert(i,x) remove(x) pop ([i]) Removes the element at the specified position in the list from the list and returns that element. If omitted, the element at the end of the list is the target. Equivalent to clear () del a [:] index(x) countx(x) list.sort(key=None,reverse=False) list.reverse() list.copy () Returns a shallow copy of the list. Equivalent to a [:]. list.count (x) Number of x in the list
>>> key = ['001','002','003','002','005','006','003','009']
>>> print(key.count('001'),key.count('002'),key.count('003'))
1 2 2
>>> key.reverse()
>>> key
['009', '003', '006', '005', '002', '003', '002', '001']
>>> key.sort()
>>> key
['001', '002', '002', '003', '003', '005', '006', '009']
>>> key.append('010')
>>> key
['001', '002', '002', '003', '003', '005', '006', '009', '010']
>>> key.insert(2,'020')
>>> key
['001', '002', '020', '002', '003', '003', '005', '006', '009', '010']
>>> key.extend(key)
>>> key
['001', '002', '020', '002', '003', '003', '005', '006', '009', '010', '001', '002', '020', '002', '003', '003', '005', '006', '009', '010']:
List type method(pop and append)Can use the list as a stack.
>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack.pop()
6
>>> stack
[3, 4, 5]
You can use the list as a queue, but the list methods are not efficient. It's faster to append or pop to the end of the list, Inserting or popping to the top of the list is slow. (Because it is necessary to shift other elements one by one). Use collections.deque.
>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")
>>> queue.append("Graham")
>>> queue.popleft()
'Eric'
>>> queue.popleft()
'John'
>>> queue
deque(['Michael', 'Terry', 'Graham'])
>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> squares = list(map(lambda x: x**2, range(10))) #Same as above
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> squares = [x**2 for x in range(10)] #Same as above:List comprehension
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
>>> #Equivalent to the above
>>> combs = []
>>> for x in [1,2,3]:
... for y in [3,1,4]:
... if x != y:
... combs.append((x, y)) #If the expression is a tuple, the tuple must have parentheses:Need
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
>>> [[x, y] for x in [1,2,3] for y in [3,1,4] if x != y]
[[1, 3], [1, 4], [2, 3], [2, 1], [2, 4], [3, 1], [3, 4]]
>>> vec = [-4, -2, 0, 2, 4]
>>> [x*2 for x in vec]
[-8, -4, 0, 4, 8]
>>> [x for x in vec if x >= 0]
[0, 2, 4]
>>> [abs(x) for x in vec]
[4, 2, 0, 2, 4]
>>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> [(x, x**2) for x in range(6)] #Generate a list of binary tuples
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
>>> [x, x**2 for x in range(6)] #Error if tuples are not enclosed in parentheses
File "<stdin>", line 1
[x, x**2 for x in range(6)]
^
SyntaxError: invalid syntax
>>> vec = [[1,2,3], [4,5,6], [7,8,9]] #Smooth the list (one-dimensional): Use two for
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> from math import pi
>>> [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
Swap rows and columns in matrix.
>>> matrix = [ # matrix = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12],]
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... ]
>>> [[row[i] for row in matrix] for i in range(4)] #Comprehension notation
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
#Equivalent to
>>> transposed = []
>>> for i in range(4):
... transposed.append([row[i] for row in matrix])
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
#Equivalent to the following
>>> transposed = []
>>> for i in range(4):
... transposed_row = []
... for row in matrix:
... transposed_row.append(row[i])
... transposed.append(transposed_row)
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
In practice, it's better to use the built-in function zip () than the complicated flow of expressions. zip () function: A function that combines elements of multiple iterable objects (lists, tuples, etc.)
>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
Reference: Additional confirmation.
>>> [row[i] for row in matrix3] #
[4, 8, 12]
>>> [i for row in matrix3] #
[3, 3, 3]
>>> matrix3
[[1, 2, 3, 4, 99], [5, 6, 7, 8, 91], [9, 10, 11, 12, 92, 93]]
>>> [[row[i] for row in matrix3] for i in range(5)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12], [99, 91, 92]]
>>> [[row[i] for row in matrix3] for i in range(6)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "<stdin>", line 1, in <listcomp>
IndexError: list index out of range
for a, b, c in zip (A, B, C) # Iterate multiple sequences
Reference: zip().
>>> names = ['Alice', 'Bob', 'Charlie']
>>> ages = [24, 50, 18]
>>> for name, age in zip(names, ages):
... print(name, age)
...
Alice 24
Bob 50
Charlie 18
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:] #Delete all values
>>> a
[]
>>> del a #Delete the entire variable
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
• Sequence data type: — list, tuple, range, etc. ・ Tuple When writing tuples, you don't necessarily have to enclose them in parentheses You may need parentheses (if the tuple was part of a larger expression) Tuple elements cannot be assigned. Immutable. Tuples can contain mutable types such as lists. Elements are often accessed by unpacking operations or indexes (or attributes in the case of namedtuples)
Tuple.
>>> t = 12345, 54332, 'hello!'
>>> t
(12345, 54332, 'hello!')
>>> t[0]
12345
>>> u = t, (1, 2, 3, 4, 5)
>>> u #Tuples can be nested
((12345, 54332, 'hello!'), (1, 2, 3, 4, 5))
>>> t[0] = 88888 #Tuples cannot be changed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Pack / Unpack.
>>> t = 12345, 54321, 'hello!' #Tuple pack(tuple packing)
>>> x, y, z = t #Sequence unpacking(sequence unpacking)
>>> x
12345
>>> y
54321
>>> z
'hello!'
Tuple method.
>>> key0 = ('001','002','003','002','005','006','003','009')
>>> key0.count('002')
2
>>> key0.append('099') #Method to change is not possible
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
A collection of unordered elements with no overlapping elements
Basic use is to determine existence and eliminate duplicate entries Supports mathematical operations such as sum (union), product (intersection), difference (difference), and symmetric difference
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)
{'banana', 'apple', 'orange', 'pear'}
>>> 'orange' in basket
True
>>> 'crabgrass' in basket
False
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a
{'c', 'd', 'a', 'b', 'r'} # 'abracadabra'Duplicate elements removed from'abrcd'
(Reference 2.7)set(['a', 'r', 'b', 'c', 'd'])
>>> a - b #Difference set(Same for difference method)
{'d', 'r', 'b'}
>>> a | b #Union(Same for union method)
{'c', 'z', 'm', 'l', 'd', 'a', 'b', 'r'}
>>> a & b #Intersection(Same for intersection method): Characters that exist again in a and b
{'c', 'a'}
>>> a ^ b #Exclusive OR set(symmetric_Same for difference method): Uncommon characters in a or b
{'d', 'z', 'm', 'r', 'b', 'l'}
Supports set inclusions.
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}
(Out of range) Add element to set myset.add (x) Delete elements from set myset.remove (x) Create set type from list type
>>> mylist = [2, 1, 3, 1]
>>> myset = set(mylist)
>>> print(myset)
{1, 2, 3}
An unordered set of keys (unique) and values
Key: value Only some immutable type can be keyed Tuples can be keys if they contain only strings, numbers, or other tuples. You can't list. )
>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape'] #Delete(Error if not present)
>>> tel
{'jack': 4098, 'guido': 4127} # 'sape'Has been deleted
>>> tel['irv'] = 4127 # 'irv'Add
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 4127} # 'irv'Position: 3 series(3.6.8)Result in.
(reference) {'jack': 4098, 'irv': 4127, 'guido': 4127} # 'irv'Position: Book,Click here for the Web. 2 system(2.7.5)If you do it in, you will get this result.
>>> tel['irv'] = 9999 #Edit value
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 9999}
>>> list(tel.keys())
['jack', 'guido', 'irv']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel #Existence check
True
>>> 'jack' not in tel
False
dict()The constructor generates a dictionary from a list containing tuples of key / value pairs.
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'guido': 4127, 'jack': 4098} #3 series(3.6.8)Execution result in
(reference){'sape': 4139, 'jack': 4098, 'guido': 4127} #2 system(2.7.5)Execution result in
If the key is a simple string, define it using keyword arguments and generate a dictionary.
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'guido': 4127, 'jack': 4098} #3 series(3.6.8)Execution result in
(reference){'sape': 4139, 'jack': 4098, 'guido': 4127} #2 system(2.7.5)Execution result in
Generate a dictionary from any key / value pair using a dictionary comprehension.
>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}
>>>dic = {'Noro': 1, 'Nakao': 2, 'Miyaoka': 3}
>>>dic['Miyaoka'] += 1
>>>print(dic)
{'Noro': 1, 'Nakao': 2, 'Miyaoka': 4} #1 addition! !!
Dictionary method update() copy() items() values() del clear()
When looping through a dictionary, items()You can use the method to retrieve the key and its corresponding value at the same time..
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
...
gallahad the pure
robin the brave
Enumerate when looping across sequences()Functions can be used to retrieve element indexes and elements at the same time.
>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
...
(0, 'tic') #Element index, value
(1, 'tac')
(2, 'toe')
Function zip to loop two or more sequence types at the same time()Use to combine each element.
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
Specify the range of the sequence in the forward direction, then the function reversed()Call.
>>> for i in reversed(range(1, 10, 2)):
... print(i)
...
9
7
5
3
1
Loop the sequence in sorted order: sorted()Use a function. (Returns a new sorted array without changing the original array).
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)
...
apple
banana
orange
pear
In some cases it is easier and safer to create a new list than to change the list.
>>> import math
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
>>> filtered_data = []
>>> for value in raw_data:
... if not math.isnan(value):
... filtered_data.append(value)
...
>>> filtered_data
[56.2, 51.7, 55.3, 52.5, 47.8]
There are other operators as well as value comparisons for conditions used in while and if statements. -Comparison operators in and not in: Whether a certain value exists (or does not exist) in a certain sequence * Already mentioned -Operators is and is not: Whether the two objects are actually the same object Only meaningful objects that can be modified, such as lists
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'
-Note that in Python, unlike C language, assignment cannot be performed inside an expression. C: if(a=1)==1
-Sequence objects can be compared with objects that have the same sequence type. -In the module, the module name (of the character string) can be obtained with the global variable name (Comparison method) ★★ ・ Compare in lexicographical order -If the comparison results of all the elements of the two sequences are equal, the sequences are considered to be equal. -If one sequence is a partial sequence corresponding to the beginning of the other, the shorter sequence is regarded as the smaller sequence.
>>> (1, 2, 3) < (1, 2, 4)
True
>>> [1, 2, 3] < [1, 2, 4]
True
>>> (1, 2, 3, 4) < (1, 2, 4)
True
>>> (1, 2) < (1, 2, -1)
True
>>> 'ABC' < 'C' < 'Pascal' < 'Python' #Dictionary order
True
>>> (1, 2, 3) == (1.0, 2.0, 3.0) #0 and 0.0 is equivalent
True
>>> (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)
True
(Memo) Character comparison: I did a little research because the standard was unknown in the dictionary order, but I couldn't understand it immediately, so I checked only the movement. At least it seems to be in ascii code order.
>>> 'C' < 'c' # 'C':0x43、'c':0x63
True
>>> 'c' < 'C'
False
>>> 'c' < 'c'
False
>>> 'C' < 'C'
False
>>> '(' < ')' # '(':0x28、')':0x29
True
>>> ')' < '('
False
-Comparison of objects of different types is possible as long as those objects provide appropriate comparison methods. The interpreter raises a TypeError exception if it does not provide a proper comparison method (Reference: Python2 series) Even if there is relatively no error, it will always be False and no error will occur.
TypeError for comparing strings and numbers, lists and tuples.
>>> 'ab' < 3 #Strings and numbers.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'
>>> (1,3,5) < [1,3,6] #Lists and tuples.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'tuple' and 'list'
-In Python, there is a way to write the definition in a file and use it in a script or on an interactive instance of the interpreter, and this file is called a module. -Definition in a module can be imported into other modules or main module.
>>> import fibo
>>> fibo.fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
-A module is a file that contains Python definitions and statements. The file name is "module name + .py" ・ If you want to use the function frequently, you can assign it to a local variable.
>>> import fibo
>>> fib = fibo.fib #Can be assigned to a local variable(Module definition omitted)
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
-A module can contain an executable statement in addition to a function definition. (For initializing the module) These executable statements are executed only when the module name is first found in the import statement. -Each module has its own private symbol table, and the functions defined in the module use this table as a global symbol table. -Global variables can be used in modules. -A module can also import other modules. -All import statements are placed at the beginning of the module, but it is customary and not required. -The imported module name is placed in the global symbol table of the module being imported. -The import statement also has a variant that imports the name in a module directly into the symbol table of the module executing the import.
*** import
>>> from fibo import fib, fib2
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
-There is also a variant that imports all the names defined in the module.
>>> from fibo import *
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
-Use of importlib.reload () function (imp.reload () from 3.4 to 3.3) For efficiency reasons, each module is only imported once per session of the interpreter. Reload if you want to fix the module and test it immediately.
-When you execute the script "python fibo.py
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
Order when importing a module named spam
Cache each compiled module as a "module.version.pyc" file in the "pycache" directory to speed up module loading
# ll -R
drwxr-xr-x 2 root root 33 January 1 08:31 __pycache__
-rw-r--r--1 root root 376 January 1 08:48 fibo.py
./__pycache__:
-rw-r--r--1 root root 503 January 1 08:31 fibo.cpython-36.pyc
Python automatically compares the source modification date and time with the compiled version to see if the compiled version is out of date and needs to be recompiled. -Compiled modules are platform-independent, so the same library can be shared between systems with different architectures. ・ Two cases where the cache is not checked
Always recompile when the module is loaded directly from the command line Without source module
winreg module provided only on Windows systems Variables sys.ps1 and sys.ps2 Define the character strings to be displayed at the primary and secondary prompts
sys.path is set to the default path obtained from the environment variable PYTHONPATH, or the built-in default value if PYTHONPATH is not set..
>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')
You can see the name defined by the module. Returns a sorted list of strings.
>>> import fibo, sys
>>> dir(fibo)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'fib', 'fib2']
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__',(abridgement)
With no arguments, dir()Enumerates the names currently defined.
>>> a = [1, 2, 3, 4, 5]
>>> fib = fibo.fib
>>> dir() #No arguments
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'fib', 'fibo', 'sys']
Use the standard module builtins for a list of built-in function and variable names * Not listed in dir ()
>>> import builtins
>>> dir(builtins)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError',(abridgement)
How to build a Python module using "dot delimited module name"
-Module name A.B → Represents submodule B of the package A · Authors of multi-module packages don't have to worry about each other's module names -The file init.py is required to make Python treat a directory as a directory containing packages. · In the simplest case init.py can be just an empty file, but init.py can run the initialization code for the package or set the all variable.
Import individual modules from package(case 1).
>>> import sound.effects.echo
>>> sound.effects.echo.echofilter(input, output, delay=0.7, atten=4) #Must be referenced by full name
Import individual modules from package(Case 2).
>>> from sound.effects import echo
>>> echo.echofilter(input, output, delay=0.7, atten=4) #Available without package name
Import individual modules from package(Case 3).
from sound.effects.echo import echofilter
echofilter(input, output, delay=0.7, atten=4) #Method available directly
-When using the syntax of "from package import item" item can be a submodule (or subpackage) of the package package Other names defined in the package, such as functions, classes, variables, etc. The import statement first checks to see if the item is defined in the package. If not defined, it will try to load the module, assuming item is the module name. If the module is not found, an ImportError is thrown
-When using the syntax of "import item.subitem.subsubitem" Each element except the last subsubitem must be a package
Have the package author explicitly provide the package index
「from sound.effects import * 」
Support path as a special attribute The path attribute is before the code in the package's init.py is executed. Initialized to be a list of directory names containing init.py
-How to output the value:
Expression statement print () function File object write () method
-Files representing standard output can be found in sys.stdout -Output format method
Use string slicing and concatenation operations to do all the string processing Use str.format method Use the Template class of the string module How to convert a value to a string: Pass the value to the repr () or str () function.
The str () function is for returning a human-readable representation of the value. The repr () function is intended to return an interpreter-readable (or SyntaxError if there is no equivalent syntax) representation. For objects that do not have a specific representation suitable for human reading, str () returns the same value as repr ().
>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1/7)
'0.14285714285714285'
>>> repr(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print(s)
The value of x is 32.5, and y is 40000...
>>> str(s)
'The value of x is 32.5, and y is 40000...'
>>> repr(s)
"'The value of x is 32.5, and y is 40000...'"
>>> hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, world\n'
>>> str((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"
>>> repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"
Write square and cubic tables in two ways.
>>> for x in range(1, 11):
... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
... print(repr(x*x*x).rjust(4))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
>>> for x in range(1, 11):
... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
str.rjust (width [, fillchar]) method ・ ・ ・ Returns a right-justified character string with the length of width. When fillchar is specified, fill with the specified characters. str.ljust (width [, fillchar]) method ・ ・ ・ Returns a left-justified character string with the length of width. When fillchar is specified, fill with the specified characters. If you want to truncate characters, use slices and use x.ljust (n) [: n]. Round () for rounding. str.center (width [, fillchar]) method ・ ・ ・ Returns a centered character string with the length of width. When fillchar is specified, fill with the specified characters. str.zfill () method ・ ・ ・ Zero-fills the left side of the numeric character string
>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'
str.format () method
Parentheses and the characters in them(This is called a format field).
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"
The numbers in parentheses are str.format()Can be used to represent the position of the object passed to the method.
>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam
str.format()If a keyword argument is passed to the method, its value is referenced by the name of the keyword argument..
>>> print('This {food} is {adjective}.'.format(
... food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.
You can also combine ordinal and keyword arguments.
>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',other='Georg'))
The story of Bill, Manfred, and Georg.
'!a'(ascii()Adapt)Or'!s'(str()Adapt)Or'!r'(repr()Adapt)Can be converted before formatting the value using.
>>> contents = 'eels'
>>> print('My hovercraft is full of {}.'.format(contents))
My hovercraft is full of eels.
>>> print('My hovercraft is full of {!r}.'.format(contents))
My hovercraft is full of 'eels'.
You can control how the value is formatted by the format specifier.
>>> import math
>>> print('The value of PI is approximately {0:.3f}.'.format(math.pi))
The value of PI is approximately 3.142.
'Prefix with an integer to specify the minimum character width for that field.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
... print('{0:10} ==> {1:10d}'.format(name, phone))
...
Sjoerd ==> 4127
Jack ==> 4098
Dcab ==> 7678
Pass the dictionary as an argument and brackets'[]'You can refer to the dictionary key using.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
... 'Dcab: {0[Dcab]:d}'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
table**] How to pass as a keyword argument using notation.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
String formatting using the% operator
>>> import math
>>> print('The value of PI is approximately %5.3f.' % math.pi)
The value of PI is approximately 3.142.
Second argument of open: * Same as fopen in C language
mode | motion | If there is a file | If the file does not exist |
---|---|---|---|
r (By default) | Read-only | No change | error |
w | Write-only | Set size to 0 (overwrite) | Create New |
a | Only for additional writing | Add to end of file | Create New |
r+ | Read and write | No change | error |
w+ | Write and read | Set size to 0 (overwrite) | Create New |
a+ | Read and additional write | Add to end of file | Create New |
Text mode and binary mode ・ Text mode (default) Convert line feed code Treat line feed code as EOF (end of file) ・ Binary mode (b) ・ ・ ・ Specify here for binary data such as JPG and EXE. Do not convert line feed code Do not treat line feed code as EOF (end of file) Specification method: Combine with the above mode and set to'rb' and'r + b'. (B can be either front or back, but b is behind)
File open.
f = open('workfile','wb')
read and readline
f.read(size)Read in.
f = open('workfile','w')
>>> f.read()
'123\n456\n'
f.read(size)Read by size.
>>> f.read(5)
'123\n4' #Returns a string of specified bytes
>>> f.read(5)
'56\n' #If the specified byte is not reached, only the existing character string is returned.
>>> f.read(5)
'' #If you reach the end''return it
Read with readline.
>>> f.readline()
'123\n' #Returns line by line
>>> f.readline()
'456\n'
>>> f.readline()
'' #If you reach the end''return it
To read multiple lines from a file, write a loop on the file object.
>>> f = open('workfile','r+')
>>> for line in f:
... print(line, end='')
...
123
456
You can also use list (f) or f.readlines () if you want to read every line of the file in list format
File writing.
>>> f = open('workfile','r+')
>>> f.write('This is a test\n') #File writing
15 #The number of characters written is returned
>>> f.close()
>>> f = open('workfile','r+') #Check below
>>> f.read()
'This is a test\n'
>>> f.close()
Other types of objects need to be converted before writing Either a string (text mode) or a bytes object (binary mode)
>>> f = open('workfile','r+')
>>> value = ('the answer', 42)
>>> s = str(value) #String
>>> value
('the answer', 42)
>>> s
"('the answer', 42)"
>>> f.write(s)
18
>>> f.close()
f.tell () ・ ・ ・ Returns an integer indicating the current position in the file f.seek (offset, starting point) ・ ・ ・ When you want to change the current position Starting point: "0" file start (default), "1" current position, "2" file end
Read error without opening the file * Web is described in reading and writing files.
>>> f.close()
>>> f.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
Use the keyword with when dealing with file objects. The file is closed properly when exiting a series of investigations, and any exceptions that occur in the middle are handled. Much shorter than the party's try-finally block of action.
File open / read * Web is described in reading / writing files.
>>> with open('workfile') as f:
... read_data = f.read()
...
>>> f.closed
True
File objects also have methods such as isatty () and truncate (), but they are rarely used
Strings can be freely read and written to files, but numbers require a little effort. Since the read () method returns only a string, you need to go through a function such as int (). When it comes to complex data, it's confusing to do it manually.
Python makes it easy to work with JSON
>>> import json
>>> json.dumps([1, 'simple', 'list'])
'[1, "simple", "list"]'
>>> import json
>>> x = {'name':'yamada','data':[2,3,4]}
>>> print(json.dumps(x))
{"name": "yamada", "data": [2, 3, 4]}
dump ・ ・ ・ Simply serialize the object to a text file load ・ ・ ・ Deserialize
(See) pickle module A protocol that allows serialization of any complex Python object, as opposed to JSON. However, it is unique to Python and cannot be used to communicate with applications written in other languages. Also, it is not secure by default.
The display content of the error message when an exception occurs in the function is important
Occurs because it does not follow the rules set by Python. Syntax interpretation (parse) error Display: SyntaxError
Even if the field expression is syntactically correct, I get an error when I run it. Display: ZeroDivisionError, NameError, TypeError, etc. There is a list of built-in exceptions in Built-in Exceptions (https://docs.python.org/ja/3.5/library/exceptions.html#bltin-exceptions)
♯## Exception handling Behavior of try statement
Specify handlers for various exceptions by providing multiple except clauses in one try statement
... except (RuntimeError, TypeError, NameError):
Variables can be specified after the exception name in the except clause The variable is tied to the exception instance, and instance.args contains the arguments when the exception instance was created. Str () is defined in the exception instance, and the arguments can be printed directly without referring to .args.
>>> try:
... raise Exception('spam', 'eggs')
... except Exception as inst:
... print(type(inst))
... print(inst.args)
... print(inst.args)
... x, y = inst.args
... print('x =', x)
... print('y =', y)
...
<class 'Exception'>
('spam', 'eggs')
('spam', 'eggs')
x = spam
y = eggs
The exception handler also handles exceptions that occur inside a function called from a try clause (even indirectly). (Not just exceptions that occur directly inside the try clause)
else clause (else clause) ・ ・ ・ The else clause is executed when no exception is thrown in the try clause. The try… except statement can optionally have an else clause. If there is an else clause, it must be placed after all except clauses
for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except IOError:
print('cannot open', arg)
else:
print(arg, 'has', len(f.readlines()), 'lines')
f.close()
Certain exceptions can be raised using the raise statement
You can include your own exceptions in your program by creating a new exception class. You can define anything that a normal class can do, but usually only allow the handler to retrieve information about the error when an exception occurs.
class Error(Exception):
pass
class InputError(Error):
def __init__(self, expression, message): #Overriding
self.expression = expression
self.message = message
The finally clause is always executed before exiting the try statement, regardless of whether an exception has occurred.
The problem below is that the file is left open
for line in open("myfile.txt"):
print(line, end="")
The with statement ensures that objects like files are always cleaned up immediately and correctly.
with open("myfile.txt") as f:
for line in f:
print(line, end="")
Objects have individuality, and the same object can be assigned multiple names (from multiple scopes). (In other languages, it is called aliasing)
A namespace is a mapping from a name to an object. A scope is a range of program text that can be accessed directly from a namespace.
(The simplest form)
class ClassName:
<Sentence 1>
.
.
.
Class objects support two types of operations: attribute reference and instantiation.
Attribute reference Instantiation
>>> class Complex:
... def __init__(self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)
Instance objects support attribute reference-only operations. Two types of attribute names, data attributes and methods, are valid
Data attributes correspond to Smalltalk "instance variables" and C ++ "data members" A method is a function that "belongs" to an object.
· Instance variables are for unique data for each instance · Class variables are for attributes and methods shared by all instances of that class
-Data attributes overwrite method attributes with the same name → Use rules that minimize the possibility of collision Start the method name with an upper letter, prefix the data attribute name with a short unique string (or just underscore), use a verb for the method, use a noun for the data attribute, etc. The first argument of the method is called self, but this naming is just a convention.
class DerivedClassName(BaseClassName):
<Sentence 1>
.
.
.
class DerivedClassName(Base1,Base2,Base3):
If the attribute in DerivedClassName does not exist, it will be searched from Base1 first, then recursively from Base1's base class, and if it is still not found, it will be searched from Base2.
But in reality, it's a little more complicated than this The method resolution order is dynamically changed due to the cooperative call to super (). Known as call-next-method in other languages with multiple inheritance, it is more powerful than the super call in languages with only single inheritance.
There are no "private" instance variables in Python. However, it is customary to treat names that start with an underscore (eg _spam) as private.
-Data types that group named data elements, such as Pascal's "record" and C's "struct", are implemented using the shell class definition.
You can use the for statement to loop across most container objects.
>>> for element in [1, 2, 3]:
... print(element)
...
1
2
3
>>> for element in (1, 2, 3):
... print(element)
...
1
2
3
>>> for key in {'one':1, 'two':2}:
... print(key)
...
one
two
>>> for char in "123":
... print(char)
...
1
2
3
>>> for line in open("workfile"):
... print(line, end='')
...
0123456789abcdef2)>>>
Behind the scenes, the for statement calls the iter () function on the container object.
-The function returns an iterator object that defines the next () method that accesses the elements in the container one by one. -If there are no more elements, the next () method throws a StopIteration exception, and the for loop ends upon receiving the notification.
You can also call the next () method directly using the built-in next () function.
>>> s = 'abc'
>>> it = iter(s)
>>> it
<str_iterator object at 0x7fc6cf183eb8>
>>> next(it)
'a'
>>> next(it)
'b'
>>> next(it)
'c'
>>> next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration #Throw a StopIteration exception
It's easy to add iterator behavior to your class if you're observing the mechanics behind the iterator protocol.
Define a iter () method that returns an object with a next () method. If the class defines the next () method, the iter () method can also simply return self.
>>> class Reverse:
... """Iterator for looping over a sequence backwards."""
... def __init__(self, data):
... self.data = data
... self.index = len(data)
... def __iter__(self):
... return self
... def __next__(self):
... if self.index == 0:
... raise StopIteration
... self.index = self.index - 1
... return self.data[self.index]
...
>>> rev = Reverse('123')
>>> iter(rev)
<__main__.Reverse object at 0x7fc6cf18ec88>
>>> for char in rev:
... print(char)
...
3
2
1
-Generator is a concise and powerful tool for creating iterators. -Use yield statement when returning some data · Each time next () is called on the generator, the generator resumes previously interrupted processing (recording state).
>>> def reverse(data):
... for index in range(len(data)-1, -1, -1):
... yield data[index]
...
>>> for char in reverse('golf'):
... print(char)
...
f
l
o
g
For a simple generator, there is a simple way to code it using an expression. An expression with a syntax similar to list comprehension, but with parentheses instead of square brackets.
>>> sum(i*i for i in range(10))
285
os module ・ ・ ・ Operating system related
>>> import os
>>> os.getcwd()
'/root/sample/test'
>>> os.chdir('/root')
>>> os.system('mkdir today')
0
Use the import os format instead of from os import *
>>> import os
>>> dir(os)
>>> help(os)
shutil module: A high-level interface that is easier to use for daily management of files and directories.
>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
'archive.db'
>>> shutil.move('/build/executables', 'installdir')
'installdir'
glob module ・ ・ ・ Get the file path name that matches the pattern specified in the argument
>>> import glob
>>> glob.glob('*.py')
>>> glob.glob('*') #Output all file names in the directory
>>> import sys
>>> print(sys.argv)
['demo.py', 'one', 'two', 'three']
The sys module also has attributes for stdin, stdout, and stderr.
The re module provides regular expressions for more advanced string processing
The math module provides a way to access C library functions for floating point arithmetic
>>> import math
>>> math.cos(math.pi / 4)
0.7071067811865476
>>> math.log(1024, 2) # log(x[, base]):x(With e as the bottom)Natural logarithm
10.0
>>> math.sqrt(2) #Get the square root
1.4142135623730951
The random module provides a tool for element selection based on random numbers
>>> import random
>>> random.choice(['apple','pear','banana'])
'pear'
>>> random.sample(range(100), 10)
[6, 49, 57, 72, 31, 25, 35, 90, 30, 76]
>>> random.random()
0.7892340475238878
>>> random.randrange(6)
5
The statistics module calculates the basic statistical characteristics of numerical data (mean, median, variance, etc.)
SciPy Project provides many modules for numerical processing
The two simplest modules are urllib.request to get the data from the URL and smtplib to send the email.
The datetime module provides classes for manipulating dates and times in both simple and complex ways.
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2020, 1, 3)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'01-03-20. 03 Jan 2020 is a Friday on the 03 day of January.'
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
20244
Modules: zlib, gzip, bz2, lzma, zipfile, tarfile
>>> import zlib
>>> s = b'witch which has which witches wrist watch'
>>> len(s)
41
>>> t = zlib.compress(s)
>>> len(t)
37
>>> zlib.decompress(t)
b'witch which has which witches wrist watch'
>>> zlib.crc32(s)
226805979
timeit module
>>> from timeit import Timer
>>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.025199586991220713
>>> Timer('a,b = b,a', 'a=1; b=2').timeit()
0.022948098019696772
The doctest module provides tools for finding modules and evaluating tests embedded in your program's docstring.
battery included: A library and integrated environment that programmers can use immediately are prepared in advance. xmlrpc.client and xmlrpc.server modules email package json package sqlite3 module gettext, locale, codecs package
-Reprlib module: Limits the output length, can be changed, but the default is 6 elements
reprlib.repr and repr.
>>> reprlib.repr('1111122222333334444455555666667777788888')
"'111112222233...6667777788888'" #Omitted on the way
>>> repr('1111122222333334444455555666667777788888')
"'1111122222333334444455555666667777788888'" #(Comparison)result of repr
reprlib.repr and repr:Combined with set set.
>>> reprlib.repr(set('1111122222333334444455555666667777788888'))
"{'1', '2', '3', '4', '5', '6', ...}" #Make it unique by using the set set and omit the back
>>> repr(set('1111122222333334444455555666667777788888'))
"{'5', '3', '8', '2', '6', '1', '4', '7'}" #(Comparison)result of repr
>>> reprlib.repr({'d', 'e', 't', 'c', 'n', 'o', 'v', 'i'})
"{'c', 'd', 'e', 'i', 'n', 'o', ...}"
-Pprint module -Textwrap module: Adjusts the text composed of paragraphs so that it fits exactly in the specified screen width. ・ Locale module
The string module contains a Template class that is flexible and has a simple syntax that end users can easily edit.
>>> from string import Template
>>> t = Template('${village}folk send $$10 to $cause.')
>>> t.substitute(village='Nottingham', cause='the ditch fund')
'Nottinghamfolk send $10 to the ditch fund.'
The substitute () method throws a KeyError if there is no placeholder equivalent in the dictionary or keyword argument.
struct module ・ ・ ・ Provides functions such as pack () and unpack () that operate binary record formats of various lengths.
>>> import struct
>>> kind, value = struct.unpack("BxH", data)
>>> data
b'd\x00\xb0\x04'
Threading is a technique for splitting multiple tasks that are not in orderly dependencies. The most difficult problem in creating a multithreaded application is coordination between threads that share data and resources.
The logging module provides a flexible logging system with many functions.
>>> import logging
>>> logging.debug('Debugging information')
>>> logging.info('Informational message')
>>> logging.warning('Warning:config file %s not found', 'server.conf')
WARNING:root:Warning:config file server.conf not found
>>> logging.error('Error occurred')
ERROR:root:Error occurred
>>> logging.critical('Critical error -- shutting down')
CRITICAL:root:Critical error -- shutting down
By default, info () and debug () output is suppressed and output is sent to standard errors
Python manages memory automatically (most objects are managed by reference counting, and garbage collection removes circular references). Memory is freed shortly after the last reference to the object is gone.
The decimal module provides Decimal data types that support decimal floating point arithmetic.
The Decimal class can represent exact values, enabling modulo calculations and equality tests that cannot be calculated as expected with binary floating point numbers. decimal module ・ ・ ・ Arithmetic operation with the required accuracy
pyvenv tutorial-env Create the tutorial-env directory if it doesn't exist, and create a subdirectory inside it that contains the Python interpreter, standard library, and other related files.
pip search astronomy pip install novas pip install requests==2.6.0 pip install --upgrade requests pip show requests pip list pip freeze > requirements.txt pip install -r requirements.txt pip uninstall request
Learning after learning the tutorial and various links
-Variable and module name completion is automatically enabled when the interpreter is started, and the completion function can be called with the [Tab] key. · By default, save history in a file named .python_history in your user directory
IPython bpython
Floating-point numbers are represented in computer hardware as (binary) fractions with a radix of 2.
The representation error is related to the fact that some (actually most) decimal decimals cannot be represented as a binary (radix 2) fraction.
>>> from decimal import Decimal
>>> from fractions import Fraction
>>> Fraction.from_float(0.1)
Fraction(3602879701896397, 36028797018963968)
>>> (0.1).as_integer_ratio()
(3602879701896397, 36028797018963968)
>>> Decimal.from_float(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> format(Decimal.from_float(0.1), '.17')
'0.10000000000000001'
Recommended Posts