[interview record] summary of 200 common interview questions and answers

Posted by philip@hux.co.za on Fri, 04 Mar 2022 21:29:48 +0100

Summary of 200 common interview questions and answers in Python

/To be improved/

1. List five common python standard libraries?

os: It provides many functions associated with the operating system, and provides a portable method to use the functions of the operating system. use os The interface provided in the module can realize cross platform access. But not all os The interfaces in the module are common in the whole platform, and the implementation of some interfaces depends on a specific platform, such as linux Related file rights management and process management.
os Main functions of the module: system related, directory and file operation, command execution and management process
>>> os.getcwd() # Returns the current working directory
>>> os.chdir() # Modify the current working directory
>>> os.system('mkdir today') # Execute the system command mkdir

sys: Commonly used for command line arguments

re: Regular matching

math: Mathematical operation

datetime: Processing date and time

2. What are Python's built-in data types?

Numeric (integer) int,Floating point number float,complex complex)

Boolean bool


character string str

list list

Yuanzu tuple

aggregate set

Dictionaries dict

3. Describe the with method. Open the processing file to help me. What have we done?

# General open file writing
f = open("./1.txt")
# with statement open file writing method
with open('1.txt') as f:
Some exceptions may occur when opening a file for reading and writing. If you follow the normal f.open Writing, need try,except,finally,Make exception judgment, and execute the file no matter what happens in the end finally f.close()Close the file, with Method helped us achieve finally in f.close. 

be careful: with There is no exception capture function in itself. If a runtime exception occurs, it can still close the file and release resources.


with context [as var]:
 Of which: context Is an expression that returns an object, var Used to save context The object returned by the expression can be a single or multiple return values.
with open('1.txt') as f:
# The expression open('1.txt ') returns a_ io.TextIOWrapper type variable object, saved with f.
# In the with statement block, you can use this variable to manipulate the file.
# After executing the with structure. f will close automatically. It's equivalent to bringing a finally.

print # 1. Contents of TXT file
True # After executing the with statement block, f automatically closes

[with statement is essentially context management]

1,Context management protocol. Inclusion method__enter__()and__exit__(),Support the protocol object to implement these two methods.
2,Context manager. Define execution with Statement, which is responsible for execution with Enter and exit operations in the context of the statement.
3,Execute when entering the context__enter__(),If set as var sentence, var Variable acceptance__enter__()Method returns a value.
4,If an exception occurs at runtime, exit the context manager. Call manager__exit__()method.

[application scenario]

1,File operation
2,Mutually exclusive objects between processes and threads
3,Support context and other objects

4. List variable data types and immutable data types in Python. Why?

Detailed reference link

Variable data type: in id(Memory address)Without change, value(Value) can be changed, it is called variable type. That is, when a variable is operated, its value is variable. The change of value will not cause the creation of a new object, and its address will not change, but the content in the address has changed or the address has been expanded.
  • List list
  • Dictionary dict
# Take list as an example
>>> a = [1, 2, 3]
>>> id(a)
>>> a = [1, 2, 3]
>>> id(a)
# Perform two operations of a = [1, 2, 3], and the address values referenced by a are different, which means that two different objects are actually created.
# That is, multiple objects with the same value are saved in memory, and the address values are different.
>>> a.append(4)
>>> id(a)
>>> a += [2]
>>> id(a)
>>> a
[1, 2, 3, 4, 2]
# When operating on list a, the object value referenced by a changes, but the address value of a does not change, but a new address is expanded after the address.
Immutable data type: value(value)Once it changes, id(Memory address) also changes, it is called immutable type( id Change, which means that a new memory space is created).
Advantage: no matter how many references there are in memory, the same object only occupies one piece of memory.
Disadvantages: when the variable needs to be operated to change the value of the object referenced by the variable, because it is an immutable data type, new objects must be created, which will create new objects one after another, but the memory that is no longer used will be recycled by the garbage collector.
  • Number (int, float, complex)
  • Boolean bool
  • String str
  • Yuanzu tuple
  • Set set
# int as an example
>>> x = 1
>>> id(x)
>>> y = 1
>>> id(y)
# x. All y reference objects are 1, and the id remains unchanged
>>> x = 2
>>> id(x)
>>> y = 2
>>> id(y)
>>> z = y
>>> id(z)
# At this time, the x, y and z reference objects become 2, and the id changes

5.Python get current date?

import time
localtime = time.localtime(time.time())

# time.time() is used to get the current timestamp.
# Each timestamp is represented by how long it has elapsed since midnight (calendar year) on January 1, 1970. Floating point decimal in seconds.
# Use the localtime() function to convert the floating-point number into the primitive struct_time structure


time.struct_time(tm_year=2021, tm_mon=3, tm_mday=18, tm_hour=16, tm_min=18, tm_sec=48, tm_wday=3, tm_yday=77, tm_isdst=0)

6. Count the number of occurrences of each word in the string

# The collection module implements a specific target container to provide alternatives to Python standard built-in containers dict, list, set, and tuple.
from collections import Counter

str1 = 'I can because i think i can'
# Counter -- subclass of dictionary, which provides the counting function of hashable objects
counts = Counter(str1.split())


Counter({'can': 2, 'i': 2, 'I': 1, 'because': 1, 'think': 1})

7. Delete files with linux and python commands

# python
import os
# linux
rm 111.py

8. Write a custom exception code

# Base class of this module
class CalcErr(Exception):

# A non integer type threw an exception
class NumErorr(CalcErr):
    """The non integer data type entered will throw this exception"""

    def __init__(self, numA, numB): # Initialization properties of exception class object
        """User entered data"""
        self.numA = numA
        self.numB = numB

    def __str__(self): # Returns the description of the exception class object
        """Return exception description"""
        return f"This computer only accepts integers!"

# The try clause threw an exception when evaluating a function
def calculator(a, b):
        if type(a) != int or type(b) != int:
            raise NumErorr(a, b)  # Throw an exception class object and pass in initialization data
    except Exception as e:  # Catch exception and return description information
    else: # When the input is normal, it is calculated directly without triggering a field
        c = a + b

9. Give an example to illustrate the meaning of try except else finally in the exception module

try...except...else No exception caught, execute else sentence

try...except...finally Execute whether an exception is caught or not finally sentence

10. How to deal with bug s

1,Errors in details, through print()Print, can be executed to print()Note: generally, there is no problem with the above code. Is there a problem with the segment detection program? If so js Yes, if you like alert or console.log

2,If some third-party frameworks are involved, they will check the official documents or some technical blogs.

3,about bug The management and classification summary of the general test will be tested bug use teambin etc. bug The management tool will record, and then we will modify one by one. The modification process is also a way to understand the business logic and improve the meticulousness of our programming logic. I will also collect and take some notes.

4,Guide Package problem, display error caused by multi tone words of urban positioning

Language characteristics

1. Talk about the differences between Python and other languages

1,Language features: concise, omitting all kinds of braces and semicolons, as well as some keywords and type descriptions; Compact expression logic
2,Language type: interpretive language, which is interpreted and run line by line, so it is very convenient to debug code and high development efficiency; But the running speed is slow.
3,Third party Library: python Yes

2. Briefly describe the interpretive and compiled programming languages

  • Interpretive type
Use a special interpreter to interpret the source program line by line into the machine code of a specific platform and execute it immediately.

Interpretive languages do not need to be compiled in advance. They directly interpret the source code into machine code and execute it immediately. Therefore, as long as a platform provides a corresponding interpreter, the program can be run.

Interpretive language needs to interpret the source code as machine code and execute it every time, which is inefficient;

As long as the platform provides the corresponding interpreter, you can run the source code, so it is convenient for source program transplantation;

Python They belong to interpretative language.
  • Compiled type
Use a special compiler to compile the high-level language source code into machine code that can be executed by the hardware of the platform at one time, and package it into the format of executable program that can be recognized by the platform.

Before the program written in compiled language is executed, a special compilation process is needed to compile the source code into machine language files, such as exe When you want to run a file in format in the future, you can directly use the compilation results, such as running directly exe Documents. Because it only needs to be compiled once and does not need to be compiled at run time in the future, the execution efficiency of compiled language is high.

It can be compiled into machine language files related to the platform at one time, which is separated from the development environment and has high operation efficiency;

It is related to a specific platform and generally cannot be transplanted to other platforms;

Existing C,C++,Objective And so on are compiled languages.
  • difference
The main difference is that the compiled source program can be run on the platform after compilation, and the translation is compiled during operation. Therefore, the compiled language runs fast and the translated language has good cross platform performance.

  • What language is java?
To be exact java It is an interpretative language, and its so-called compilation process is just.java Compile the file into platform independent bytecode.class File, not to C Compile into executable machine language, please note Java The difference between the so-called "compilation" and the traditional "compilation"). As a compiled language, JAVA The program should be uniformly compiled into bytecode file - file suffix.class. Such documents are java Also known as class file. java Class file cannot be executed directly on the computer, it needs to be java Virtual machine( JVM)It can only be executed after being translated into local machine code, and java The translation process of virtual machine is interpretive. java Bytecode files are first loaded into the computer memory, and then read an instruction, translate an instruction, and execute an instruction. This process is called java The interpretation and execution of language is by java Completed by the virtual machine. And in reality, java development tool JDK Two very important commands are provided to complete the above compilation and interpretation (translation) process. The two commands are java.exe and javac.exe,Former loading java Class file, and gradually compile the bytecode file, while another command corresponds to java Interpretation of language(javac.exe)Process. In order, java Language is the process of compiling first, and then interpreting and executing.

3.Python interpreter types and related features?

When we finish writing Python When the code, we will get a containing Python Code with.py For the file with extension name, at this time, we need to run this code Python Interpreter to execute.py Documents.
  • CPython
use C Language implemented Python The interpreter is also the official and most widely used Python Interpreter; CPython It is an interpreter that uses bytecode. Any program source code must be compiled into bytecode before execution. It also has external function interfaces that interact with several other languages. It is characterized by the most widely used interpreter
  • IPython
be based on CPython An interactive interpreter above, that is, IPython It is only enhanced in the way of interaction, but the execution Python Function and of code CPython It is exactly the same. For example, although the appearance of domestic browsers is different, the kernel is actually called IDE,It is characterized by strong interaction.
  • PyPy
The goal is execution speed, using JIT Technique( Just-In-Time,Just in time compiler), right Python The code is compiled dynamically, so it can be significantly improved Python Code execution speed, so its biggest feature is that it can improve execution efficiency.
  • JPython
Run in Java On platform Python Interpreter, you can directly Python Code compiled into Java Bytecode execution.
  • IronPython
and JPython Similar, but IronPython Is running at Microsoft.net On platform Python Interpreter, you can directly Python Code compiled into.net The advantages of bytecode are also obvious.

4. Tell me the difference between Python 3 and python 2 you know?

  • print
stay Python 2 In, print Is a statement; stay py2 In, print The statement is followed by a tuple object.

and Python3 Exists as a function in. and py3 In, print Function can receive multiple unknown parameters.
# py2
>>> print("hello", "world")
('hello', 'world')

# py3
>>> print("hello", "world")
hello word
  • code
Python2 The default code for is ASCII Code;

Python3 Default use UTF-8 Encoding, so you don't need to write at the top of the file# coding=utf-8.
  • character string
strbytebyteencodeStorage and transmission
  • True and False
stay Python2 In, True and False Are two global variables (names), numerically corresponding to 1 and 0 respectively, because they are variables and can point to other objects.
# py2
>>> True = False
>>> print(True)
>>> True is False
Python3 This defect has been corrected in, True and False Become two keywords, always point to two fixed objects, and are not allowed to be re assigned.
# py3
>>> True = 1
	File "<stdin>", line 1
SyntaxError: cant't assign to keyword
  • iterator
stay python2 Many built-in functions and methods that return list objects in python3 Changed to something similar to「iterator 」Because of the lazy loading characteristics of iterators, it is more efficient to operate big data. Python2 Medium range and xragne Functions are merged into range Function.
In addition, the dictionary object dict.keys(),dict.values()Methods do not return a list, but in an iterator like way“ view"Object returns. Higher order function map,filter,zip Nor is the list object returned. Python2 The iterator must be implemented next Method, and python3 Changed to__next__
  • nonlocal
Python2 You can use keywords in functions global Declare a variable as a global variable, but in nested functions, it is impossible to declare a variable as a nonlocal variable python3 Keywords are added in nonlocal,Make nonlocal variables possible.
def func():
    c = 1

    def foo():
        c = 12



def func():
    c = 1

    def foo():
        nonlocal c
        c = 12




5. What is the difference between int and long in Python 3 and python 2?


long(Long integer)There is a at the end of the number L

64 Bit machine, range-2^63~2^63-1

Beyond the above range, python Automatically convert to long (Long integer)


All integers are int,No, long (Long integer)

6. What is the difference between xrange and range?


xrange: It is not created immediately in memory, but is created while looping  

range: Create all values immediately in memory  


only range,amount to python2 Medium xrange  

range: It is not created immediately in memory, but is created while looping  

Coding specification

7. What is PEP8?

8. Do you know the Zen of Python?

9. Do you know docstring?

10. Do you know the type annotation?

11. You know the naming conventions of Python classes, for example

12. How many comments are there in Python?

13. How to annotate a function gracefully?

14. How to annotate variables?

15. Whether Python code indentation supports the mixing of Tab key and space.

16. Can I import multiple libraries in one import sentence?

17. What should we pay attention to when naming Py files?

18. Give examples of several tools to standardize Python code style

data type

character string

19. List the basic data types in Python?

20. How to distinguish between variable data types and immutable data types

21. Convert "Hello world" to initial capital "Hello world"

22. How to detect that a string contains only numbers?

23. Reverse the string "ilovechina"

24. What do you know about string formatting in Python?

25. There are spaces at the beginning and end of a string, such as "adabdw". It is required to write a function to remove the spaces before and after the string.

26. Get the last two characters of the string "123456".

s = "123456"
tmp = s[4:6:1]


27. How to convert a string S encoded as GBK into a string encoded as UTF-8?

demo_str = "demo".encode("gbk")
demo = demo_str.decode('gbk').encode('utf-8')

28. (1)s = "Info: xiaoZhang 33 shandong", output ['info ',' xiaoZhang ',' 33 ',' shandong '] a = "hello China" with regular segmentation string, remove redundant spaces and leave only one space.
29. How to convert a string to lowercase?

# Note that s.lower() does not change the case in the original string s
  1. What is the difference between single quotation marks, double quotation marks and three quotation marks?
1,stay Python In, both single quotation marks and double quotation marks can be used to represent a string, which is exactly the same without escaping characters; If there is an escape character, it can be divided.

2,When defining a string with single quotation marks or double quotation marks, you can only write the string together in one line. If you have to write multiple lines, you have to add one after each line\Represents a hyphen or an escape character\n;At this point, you can use three quotation marks, single and double; Three quotation marks also serve as comments.


30. Given list = [1,2,3,1,2], de duplicate the list elements of list and write the specific process.

tmp = set(AList)
AList = list(tmp)

[1, 2, 3]

31. How to turn "1,2,3" into ["1", "2", "3"]

s = "1,2,3"
lst = list(map(str, s.split(',')))

['1', '2', '3']

32. Given two list s, A and B, find out the same element and different elements

print('A,B Same element in:')
print('A,B Different elements in:')

33. [1,2], [3,4], [5,6]] expand the list to get [1,2,3,4,5,6]

34. Consolidated list [1,5,7,9] and [2,2,6,8]

35. How to disrupt the elements of a list?

from random import shuffle

lst = [1, 3, 4]

[4, 3, 1]


36. What is the difference between del and pop in dictionary operation

37. Sort by age in the dictionary

38. Please merge the following two dictionaries: A = {"A": 1, "B": 2},b = {"C": 3, "D": 4}

39. How to use generative method to generate a dictionary and write a section of function code.

40. How to turn tuples ("a", "b") and tuples (1,2) into dictionaries {"a": 1, "b": 2}


41. What are the types and characteristics of data structures commonly used in Python?

42. How to exchange the keys and values of dictionary {"A": 1, "B": 2}?

new_dic = {v: k for k, v in dic.items()}

43. How to convert tuple and list in Python?

lst = [1,2,3]
tup = tuple(lst)

(1, 3, 4)

44. We know that slicing can be used to select some elements for the list, so how to achieve the same function for generator type objects?

45. Please change [i for i in range(3)] to generator

stay Python In, the yield The function of is called a generator( generator). 
Unlike ordinary functions, a generator is a function that returns an iterator and can only be used for iterative operations. It is easier to understand that a generator is an iterator.
In the process of calling the generator to run, every time yield The function will pause and save all the current running information and return yield Value of, And execute it next time next() Method continues running from the current location.
Call a generator function and return an iterator object.
(i for i in range(3))

46. a="hello" and b = "hello" are encoded as bytes

a = b"hello"
b = bytes("Hello", 'utf-8')
c = "hello".encode(encoding='utf-8')
d = "Hello".encode(encoding='utf-8')
print(a, end='\n')
print(b, end='\n')
print(c, end='\n')
print(d, end='\n'


Operational topics

49.Python exchanges the values of two variables

50. read, readline or readlines will be used when reading files to briefly describe their respective functions

51. What data types can be processed when serializing JSON? How to customize and support datetime types?

52. When serializing JSON, Chinese will be converted to unicode by default. What if you want to keep Chinese?

53. There are two disk files A and B, each with A line of letters. It is required to combine the information in these two files (in alphabetical order) and output them to A new file C.

54. If the current date is 20190530, it is required to write a function to output the date after N days (for example, if N is 2, 20190601 will be output).

55. Write a function, receive the integer parameter n and return a function. The function of the function is to multiply the parameter of the function by N and return the result.

56. What problems will exist in the following code and how to improve it?

57. One line of code outputs all even numbers between 1-100.

  1. The function of the with statement is to write a piece of code?

  2. Method for converting python dictionary and json string to each other

  3. Please write a Python logic to calculate the number of uppercase letters in a file

  4. Please write a Python connection to the Mongo database, and then the query code.

  5. Talk about the basic types of Redis.

  6. Please write a code for Python to connect to Redis database.

  7. Please write a piece of code for Python to connect to MySQL database.

  8. Do you know about Redis affairs?

  9. Do you understand the three paradigms of database?

  10. Do you know about distributed locks?

  11. Use Python to realize the function of a distributed lock of IDS.

  12. Write a piece of code for Python to create an index using Mongo database.

Advanced features

70. What is the function decorator for? Please list the description?

71.Python garbage collection mechanism?

72. Magic function__ call__ How do you use it?

73. How to judge whether an object is a function or a method?

74. Usage and difference between @ classmethod and @ staticmethod

75. How to implement the interface in Python?

76. Do you understand reflection in Python?

77. What is the role of metaclass? And application scenarios?

Usage of settr. Attr()

79. Please list the magic methods and uses of Python you know.

80. How do I know the type of a Python object?

81. Does Python pass parameters by value or address?

82. Use examples of metaclasses in Python

83. Briefly describe the any() and all() methods

84.filter method finds all odd numbers in the list and constructs a new list, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

85. What is a monkey patch?

86. How is memory managed in Python?

87. Release all memory allocations when exiting Python?

regular expression

88. Use regular expressions to match

Baidu once, you will know

The address in a = "Zhang Ming 98 points", with re Sub, replace 98 with 100

89. Regular expression matching (. *) and (. *?) Match difference?

(.*)It is greedy matching, and it will match as many as possible that meet the regularity

(.*?)Yes no greedy matching will match as few as possible that meet the regularity
import re
s= str("<a>ha-ha</a><a>gossip</a>")
res1 = re.findall("<a>(.*)</a>",s)
res2 = re.findall("<a>(.*?)</a>",s)

90. Write a regular expression that matches the mailbox

Examples of legal mailboxes:
1234@qq.com((pure digital)
wang@126.com((plain letter)
wang123@126.com((mixed number and letter)
wang123@vip.163.com(Multi level domain name)
wang_email@outlook.com(Underlined _)
wang.email@gmail.com(Including English full stop .)
According to the observation of the above mailbox, the mailbox can be divided into two parts(“@"Left and right parts) to analyze:

1 The left part can have numbers, letters and underscores(_)And English full stop(.),Therefore, it can be expressed as:[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*. 
2 The right part is the domain name. According to the rules of the domain name, there can be numbers, letters and dashes(-)And English full stop(.),In addition, the top-level domain name is generally 2 ~ 6 English letters (e.g“ cn","com","site","group","online"),Therefore, it can be expressed as:([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}. 
Two points should be noted:
1 Considering that there can be no other characters at the beginning and end of the string when matching the mailbox, the meta character of the start flag should be added ^ And end flag metacharacters $. 
2 English full stop(.)Is a metacharacter of a regular expression, so it needs to be escaped(\.). 
email = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/

Other contents

91. Explain the function of pass statement in python?

92. Briefly describe your understanding of the input() function

93. is and in Python==

94. Namespace and scope in Python

Namespace(Namespace)It is a mapping from name to object. Most namespaces are through Python Dictionary.

Namespaces provide a way to avoid name conflicts in a project. Each namespace is independent and has no relationship, so a namespace cannot have duplicate names, but different namespaces can have duplicate names without any impact.

There are generally three namespaces:
1,Built in name( built-in names),Python Language built-in names, such as function names abs,char And exception name BaseException,Exception wait.
2,Global name( global names),The name defined in the module records the variables of the module, including functions, classes, other imported modules, module level variables and constants.
3,Local name( local names),The name defined in the function records the variables of the function, including the parameters of the function and locally defined variables. (also defined in the class)

Namespace lookup order:
Suppose we want to use variables runoob,be Python The search order is: local namespace to -> Global Namespace  -> Built in namespace.
If the variable cannot be found runoob,It discards the lookup and raises a NameError abnormal:
NameError: name 'runoob' is not defined. 

Lifecycle of namespace:
The lifecycle of a namespace depends on the scope of the object. If the execution of the object is completed, the lifecycle of the namespace ends.
Therefore, we cannot access objects with internal namespaces from external namespaces.
Scope is a Python The program can directly access the body area of the namespace.

In a python In the program, directly accessing a variable will access all scopes from inside to outside until they are found, otherwise an undefined error will be reported.

Python In, the variables of the program are not accessible anywhere, and the access authority depends on where the variable is assigned.

The scope of the variable determines which part of the program can access which specific variable name.

Python There are four scopes in total, namely:
L(Local): The innermost layer contains local variables, such as a function/Method internal.
E(Enclosing): Contains nonlocals(non-local)Nor is it global(non-global)Variable.
For example, two nested functions, one function (or class) A It contains another function B ,So for B For the name in A The scope in is nonlocal. 
G(Global): The outermost layer of the current script, such as the global variable of the current module.
B(Built-in):  Contains built-in variables/Keywords, etc., Finally searched
 Rule order: L –> E –> G –>gt; B. 
If it cannot be found locally, it will go to the local outside the local (such as closure). If it cannot be found again, it will go to the global, and then go to the built-in.

be careful: Python There are only modules in the( module),Class( class)And functions( def,lambda)Will introduce a new scope and other code blocks (such as if/elif/else/,try/except,for/while No new scope will be introduced, that is, the variables defined in these statements can also be accessed externally,

95. Ternary operation writing method and application scenario?

96. Do you know enumerate?

97. List five standard modules in Python

98. How to set a global variable in a function

99. Usage examples of pathlib

100. Exception handling in Python, write a simple application scenario

101. How to break through the maximum number of recursions in Python?

102. What is object-oriented mro

103.isinstance function and application scenario?

104. What is an assertion? Application scenario?

105.lambda expression format and application scenario?

106. Differences between new and old types

107. What is dir () for?

108. There are three modules in a package, demo1 py, demo2. py, demo3. Py, but how to ensure that only demo1 and demo3 are imported when using from tools import * to import modules.

109. List five exception types in Python and their meanings

110. What is the difference between copy and deep copy?

111. The meaning and usage of * args and * * kwargs often encountered in the code.

112. In Python, there will be functions or member variables with single underscore prefix and end, and double underscore prefix end. What is the difference?

113. Differences between W, a +, wb file writing modes

114. Give an example of the difference between sort and sorted

115. What is a negative index?

116. What does the pprint module do?

117. Explain the assignment operator in Python

118. Explain the logical operators in Python

119. Talk about bitwise operators in Python

120. How do I use hexadecimal numbers in Python?

121. How to declare multiple variables and assign values?

Algorithms and data structures

122. Known:

(1) Find 4 from AList and BSet. Which is the worst time complexity?

(2) Insert 4 from AList and BSet. What is the worst time complexity?

123. Implement a binary search function in Python

124. Implementation method of Python singleton mode

125. Implement a Fibonacci sequence using Python

126. Find duplicate numbers in the list

127. Find a single number in the list

128. Write a bubble sort

129. Write a quick sort

130. Write a topological sort

131.python implements a binary calculation

132. There is a group of "+" and "-" symbols. It is required to arrange "+" to the left and "-" to the right, and write out the specific implementation method.

133. Single linked list inversion

134. Cross linked list for intersection

135. Implement stack with queue

136. Find the median of the data flow

137. The K-th smallest element in the binary search tree

Reptile related

138. In the requests module, requests Content and requests Textwhat's the difference

139. Briefly write the usage framework of lxml module

140. Tell me about the workflow of scratch

141. De duplication principle of sweep

142. How many types of scratch middleware are there? Which middleware have you used

143. What did you encounter when you wrote about reptiles? Anti reptile measures, how did you solve it?

144. Why are agents used?

145. What if the agent fails?

146. List the content and information you know about the header

147. When you open the browser and visit Baidu, you will know the results and the whole process.

148. How to deal with the verification code when the climbing speed is too fast

149. What is the difference between scratch and scratch redis? Why choose redis database?

150. What problems does distributed crawler mainly solve

151. Is it good to use multiple processes to write crawlers? Or multithreading? Why?

152. What are the most used parsers for parsing web pages

153. For web pages that need to log in, how to solve the problem of restricting ip, cookies and sessions (some of which are dynamically generated) without using dynamic crawling?

154. Solution of verification code (simple: it can be obtained after image processing, difficult: the verification code is dynamically carried out by clicking and dragging?)

155. What are the most used databases (mysql, mongodb, redis, etc.)?

Network programming

156. What is the difference between TCP and UDP?

157. Briefly introduce three handshakes and four waves

158. What is a sticky bag? What causes packet sticking in socket? Under what circumstances will sticking occur?


159. Give an example Usage of future midline process pool

160. Talk about the difference between multithreading, multiprocessing and concurrency.

1,The process is the smallest unit allocated by the operating system, and the thread is CPU The smallest unit of scheduling. A coprocess is neither a process nor a thread. A coprocess is only a special function. A coprocess is not a dimension with a process or thread.

2,A process is composed of one or more threads. Threads are different execution routes of code in a process. A coroutine is an operation, and the user operates the switching of threads by himself(Switch in user mode),In this way, thread switching can be greatly reduced(Switch in kernel mode)The cost of.

3,Cost of switching process(Time, resources, etc)It is larger than the switching thread, and the switching thread also needs to cost. Like the process, the switching has the problem of context switching.

4,A process can contain multiple threads, and a thread can contain multiple coroutines.

5,Although multiple coprocesses in a thread can be switched, multiple coprocesses are executed serially and can only run in one thread, which can not be used CPU Multi core capability.

# Multi process multitasking
import os
import time
from multiprocessing import Pool

# The multiprocessing module provides a Pool process Pool to create sub processes in batch.

def long_time_task(name):
    print('Run task %s (%s)...' % (name, os.getpid()))
    start = time.time()
    time.sleep(1)  # The amount of time the thread is delayed from executing
    end = time.time()
    print('Task %s runs %0.2f seconds.' % (name, (end - start)))

if __name__ == '__main__':
    print('Parent process %s.' % os.getpid())
    p = Pool(5)  # Pool object. Parameter 5 indicates how many parallel processes are called to run the program. The default capacity of pool is the number of computing cores of CPU
    for i in range(10):
        p.apply_async(long_time_task, args=(i,))  # p.apply_async() function adds process tasks to the process pool. Its parameters include the program to be run and the incoming parameters of the program.
        print('Waiting for all subprocess done ...')
        p.close()  # Calling the join() method on the Pool object will wait for all child processes to complete execution. Before calling join(), you must call close(), and after calling close(), you cannot continue to add new processes.
        p.join()  # The join() method can wait for the child process to finish before continuing. It is usually used for synchronization between processes.
        print('All subprocesses done.')

# Multithreading multitasking
import time
import threading
# threading module

# Code executed by the new thread:

def loop():
    print('thread %s is running...' % threading.current_thread().name)
    # threading.current_thread() always returns an instance of the current thread
    n = 0

    while n < 5:
        n = n + 1

        print('thread %s >>> %s' % (threading.current_thread().name, n))


    print('thread %s ended.' % threading.current_thread().name)

print('thread %s is running...' % threading.current_thread().name)

t = threading.Thread(target=loop, name='LoopThread')  # Name the child thread with LoopThread



print('thread %s ended.' % threading.current_thread().name) 

161. Brief GIL

  • Preparatory knowledge
Understanding of parallelism and Concurrency:

Concurrency: the ability to handle multiple tasks alternately; 
Parallelism: the ability to handle multiple tasks at the same time; 

The key to concurrency is that you have the ability to handle multiple tasks, not necessarily at the same time.
The key to parallelism is your ability to handle multiple tasks at the same time,The emphasis is on the same time.

The biggest difference: Yes or no『meanwhile』Handle tasks.
  • Gil (global interpreter lock)
Through the code, it can be found that multiple processes can be fully used cpu While multithreading is not fully used cpu Two cores of 

problem:Through verification, we found that multithreading can not really make multi-core cpu Achieve parallelism. 

cpython There is one in the interpreter GIL(Global interpreter lock),Its function is to ensure that only one thread can execute code at the same time, 
This makes it impossible to achieve parallelism when using multithreading.

1.It needs to be continuously used in processing such as scientific computing cpu Single thread is faster than multithreading. You can use multi-process and multi-core CPU resources.
2.Processing image IO When operating such tasks that may cause blocking, multithreading is better than single thread, because IO The blockage will be released automatically GIL lock 

Solution method: 
1: Replace the interpreter, such as using jpython(java Realized python interpreter) 
2: Use multi process to complete multi task processing

162. How do processes communicate

Process There must be communication between processes. The operating system provides many mechanisms to realize the communication between processes. Python of multiprocessing The module wraps the underlying mechanism and provides Queue,Pipes And other ways to exchange data.
from multiprocessing import Process, Queue
import os, time, random
# Write the code executed by the data process:
def write(q):
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue...' % value)
# Code executed by data reading process:
def read(q):
    print('Process to read: %s' % os.getpid())
    while True:
        value = q.get(True)
        print('Get %s from queue.' % value)
if __name__=='__main__':
    # The parent process creates a Queue and passes it to each child process:
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # Start subprocess pw, write:
    # Start subprocess pr, read:
    # Wait pw end:
    # The pr process is an endless loop. You can't wait for it to end. You can only forcibly terminate it:

163. What is the role of IO multiplexing?

164. What are the differences among select, poll and epoll models?

165. What are concurrency and parallelism?

166. How does a thread 1 Let thread 2 call a function?

167. Explain what asynchronous non blocking is?

168. threading. What is the role of local?

The internal automatically maintains a space (Dictionary) for each thread to access its own value. Ensure data isolation between threads.

thread  ID: {...}
thread  ID: {...}
thread  ID: {...}
thread  ID: {...}

Git interview questions

169. Tell me about the git command you know

170.git how to view the content of a submitted modification