Blind spots of basic Python knowledge (learn from teacher Liao Xuefeng's tutorial website)

Previously mainly used Java and C++As my own programming language, I want to learn comprehensively recently python,So I started from the basic study, according to teacher Liao Xuefeng[python Website learning](https://www.liaoxuefeng.com/wiki/1016959663602400). I learned a little before when I used python to do projects, but it is not systematic. I take this blog to put the knowledge points I won't know here for later review.

1, python Foundation

python uses # to denote comments
If you want to use some special characters in python strings, you can use the escape character \
Null values are represented by None
Double division sign / / indicates the division of the floor. The division result is still an integer, such as

10//3
 The result is 3

Three codes:

ASCII Encoding: only English numbers and some special symbols, each character occupies one byte
Unicode Coding: it unifies all languages into one code, and its standard is still developing. Each character accounts for two bytes
UTF-8 Code: variable length code, Unicode Different characters in the encoding are compiled into different bytes

list: list

classmates = ['Michael', 'Bob', 'Tracy']
len(classmates) 			#, get the length of the list, and the result is 3
# Its access is similar to that of arrays. The following table starts from 0. If the subscript is negative, it indicates the penultimate. For example, classmates[-2] indicates the penultimate
classmates.append('Adam')# Append element
classmates.insert(1, 'Jack') #Insert element
classmates.pop() #Delete end element
classmates.pop(1) #Deletes the element at the specified location
classmates[1] = 'Sarah'	#Direct assignment
s = ['python', 'java', ['asp', 'php'], 'scheme']	#Element types are also different, and can even contain lists

Tuple: tuple

classmates = ('Michael', 'Bob', 'Tracy')	#Tuples, once initialized, cannot be changed for code security

Conditional judgment

The format is:

if <Condition judgment 1>:
    <Execution 1>
elif <Condition judgment 2>:
    <Execution 2>
elif <Condition judgment 3>:
    <Execution 3>
else:
    <Execution 4>

loop

There are two kinds of Python loops. One is the for... In loop, which iterates out each element in the list or tuple in turn

names = ['Michael', 'Bob', 'Tracy']
for name in names:
    print(name)

The second type of loop is the while loop, which keeps cycling as long as the conditions are met, and exits the loop when the conditions are not met

sum = 0
n = 99
while n > 0:
    sum = sum + n
    n = n - 2
print(sum)

Dictionary: dict

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}

Because the dictionary has an index table, it will look up quickly
Please note that the internal storage order of dict has nothing to do with the order in which key s are placed.
Compared with list, dict has the following characteristics:
The search and insertion speed is extremely fast and will not slow down with the increase of key s;
It takes up a lot of memory and wastes a lot of memory.
On the contrary:
The time of searching and inserting increases with the increase of elements;
It takes up little space and wastes little memory.

The key of dict must be an immutable object

2, Advanced features

section

Slicing is used to extract elements in a range in the list

L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
 L[0:3]	# Take out ['Michael', 'Sarah', 'Tracy']
 # L[0:3] indicates that it starts from index 0 to index 3, but does not include index 3. That is, the indexes 0, 1 and 2 are exactly 3 elements. If the first index is 0, it can also be omitted

Tuple is also a list. The only difference is that tuple is immutable. Therefore, tuple can also be sliced, but the result of the operation is still tuple
The string 'xxx' can also be regarded as a list, and each element is a character. Therefore, a string can also be sliced, but the result of the operation is still a string:

iteration

An iteration is a loop through a list or tuple

List generator

The list generator is used to generate lists

list(range(1, 11))	#Generate [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[x * x for x in range(1, 11)]	# Generate [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

[x * x for x in range(1, 11) if x % 2 == 0]	#Judgment and generation [4, 16, 36, 64, 100] are added
[m + n for m in 'ABC' for n in 'XYZ']	#Use two-layer cycle to generate full arrangement ['ax ',' ay ',' AZ ',' BX ',' by ',' BZ ',' CX ',' CY ',' CZ ']


# Next, it is used to output the files in the folder
import os # Import os module. The concept of module will be described later
[d for d in os.listdir('.')] # os.listdir can list files and directories

generator

The generator generator is generated in a loop relative to the list generator

3, Functional programming

map function

The map() function receives two parameters, one is a function and the other is iteratable. Map applies the passed function to each element of the sequence in turn, and returns the result as a new Iterator

def f(x):
...     return x * x
r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
list(r)
# Output [1, 4, 9, 16, 25, 36, 49, 64, 81]

reduce function

reduce applies a function to a sequence [x1, x2, x3,...]. This function must receive two parameters. reduce continues to accumulate the result with the next element of the sequence. The effect is:

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

filter

filter

sorted

Sorting function
The sorted() function is also a high-order function. It can also receive a key function to realize user-defined sorting, such as sorting by absolute value:

sorted([36, 5, -12, 9, -21], key=abs)

Anonymous function

For example, lambda expressions

list(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]))

Keywords: Python

Added by opels on Sun, 23 Jan 2022 06:10:59 +0200