Python final review resources

Time:2022-1-5

Tip: blue is the required part, yellow is the prompt and unnecessary part.

1. Source code file extension * py

2. Python uses indentation as the syntax boundary, and generally uses four grid indentation.

3. Python variables and functions are defined without specifying the type.  
Python variables do not need to be declared in advance and do not need to specify types, but they must be assigned to use.

4. Python identifier naming rules – 1 Number (cannot start) – 2 Letters (case sensitive)
     -3. Underline – 4 Keywords cannot be used.

5. Legal: continuous assignment (a = b = C = 10), multivariable assignment (a, B = 3,5), compound assignment (a + = 3)
Illegal: expression assignment (M = (n = k))

6. Sequence element subscripts: s [0] (first), s [- 1] (last), s [2:5] (S0, S1, S2, S3, S4, S5,
Slice from the third (s [2]) to the fifth (s [4]), closed left and open right)

7. Python built-in functions:
Len() returns the number of elements in lists, tuples, dictionaries, and collection objects.
Sum() returns the sum of elements when the elements in the list, tuple and collection are all numeric types. Otherwise, an error is reported.
Max() returns the maximum value when the values of list, tuple, set and dictionary are both numbers or strings.
Min() returns the minimum value when the values of list, tuple, set and dictionary are both numbers or strings.

8. Several ways to import modules in Python
(1) Basic mode: import module1 (module name)
(2) Call the function format in the module: (first import the module: import random) and then call the function random randint(0,1)
(3) Some functions in the import module: from module name, import function name 1, function name 2,…
(4) Import all functions in the module: from module name import*

9. What are the differences and similarities between exceptions and errors in Python.
(this question introduces the concepts of exception and error respectively)
A: errors are divided into syntax errors and logic errors. Syntax errors refer to the code that does not conform to the syntax and logic errors of the interpreter or compiler
It refers to incomplete or illegal input or calculation problems. An exception is caused by an error. It refers to an error caused when the program runs. Memory
Insufficient or IO errors can also cause exceptions.

10. Function test questions:
Function definition: def function name (parameter list) \ function body \ return expression.
Function call: it can be completed by “function name ()”.
Function parameters: ((pass) parameter refers to the variable added in “()” when defining the function, which is called (pass) parameter)
Default parameter: set the default value for the function parameter when defining the function. This parameter is called the default parameter.
Variable length parameter: variables with “*” are used as tuples to store unnamed variable parameters.
Variables with “* *” are used as dictionaries to store named parameters.
Keyword parameter: refers to calling the program by using the key value pair key = value.
Positional parameters: positional parameters refer to the same order of parameters when calling a function.
(if the keyword parameter is used as the transfer method, the function cannot use the position parameter later)
Constructor__ init__: It has the function of initialization, that is, when the class is instantiated, the function will be executed automatically.
Then you can usually put the attributes to be initialized in this.
Destructor__ del__: Is a method that works when an object is garbage collected. Its execution generally means
Object cannot continue to be referenced. Reclaim memory.

11. File related questions:
Open file: F = open (‘text. TXT ‘) # if the access mode is not specified, an error will be reported if the file does not exist.
Close file: f.close ()
Text file: the stored content consists of several lines. Each line ends with a newline character “\ n”, mainly including English letters, Chinese characters, numbers and strings. The content can be displayed and edited normally through document editing such as Notepad, and people can read and understand it directly.
Binary file: the information is stored in the form of byte string, and cannot be read and written directly by Notepad or other text editing software,
You can’t use Python’s file object to directly read the contents of binary files.
File open mode:
R: open a file. It is read-only. If the file does not exist, an error will be reported.
RB: open a file in binary format. It is read-only. If the file does not exist, an error will be reported.
R +: open a file for reading and writing, and the file pointer will be placed at the beginning of the file.
Rb +: open a file in binary format for reading and writing, and the file pointer will be placed at the beginning of the file.
W: open a file for writing only. If the file already exists, overwrite it; Create if not present.
WB: open a file in binary format for writing only. If the file already exists, overwrite it; Create if not present.
W +: open a file for reading and writing. If the file already exists, overwrite it; Create if not present.
WB +: open a file in binary format for reading and writing. If the file already exists, overwrite it; Create if not present.
A: open a file for appending. If it exists, write the new content after the existing content; If the file does not exist, create a file for} writing.
AB: open a file in binary format for appending. If it exists, write the new content after the existing content; If the file does not exist, create a file for writing.
A +: open a file for read-write append. The file already exists, and the file pointer will be placed at the end of the file. If the file does not exist, a new file is created for reading and writing.
AB +: open a file in binary format for read / write append. The file already exists and the file pointer will be placed at the end of the file. If} the file does not exist, create a new file for reading and writing.

12. Program questions:

(1)
def sum(a,b=3,c=5)
      return sum(a,b,c)
Print (sum (5)) # assigns 5 to a
#The return value is 13
Print (sum (5,6)) # assigns 5 and 6 to a and B in order
#The return value is 16
Print (sum (a = 2, C = 3)) # designedly assigns 2 to a and 3 to C
#The return value is 18
(2)
(import random
x = [random.randint(0,10) for i in range(100) ]
print(x)
y = x[0:50]
y.sort()
x[0,50] = y
y = x[50:100]
x.sort(reverse = True)
x[50:100] = y
print(x))
def demo(*para):
    avg = sum(para)/len(para)
    g = [i for i in para if i>avg]
    return tuple(g)
print(demo(10,20,30,40,50))
def demo(lst,k)
    if k<len(lst)
        return lst[k:]+lst[:k]
lst = [2,4,6,8,10]
Demo (LST, 3) #return LST [K:] + LST [: k] from left to right, the result is 8 10 2 4 6

(3)
#Open the text file data in the root directory of drive C by reading txt
with open(“C:\\data.txt”,”r”) as fp:
#Read the text data line by line and store it in the list data
data = fp.readlines()
#Traverse all the elements in the list, convert them into integers and store them in data again
data = [int(item) for item in data] 
#Sort in descending order, that is, flip after sorting in ascending order
data.sort(reverse = True)
#Restore the list elements to character type. Note that one character is spliced after each character, so that each data occupies one line.
data = [str(item)+”\n” for item in data]
#Open the text file data in the root directory of drive C by writing txt
with open(“C:\\data.txt”,”w”) as fp:
    fp.writelines(data)