Home >Backend Development >Python Tutorial >What are the most frequently asked questions in Python interviews?
Integer (int)
Floating point (float)
String (str)
Boolean (bool)
List ( list)
Tuple (tuple)
Dictionary (dict)
Set )
List: list is a variable type, and the data can change dynamically
Tuple: is an immutable type, with a fixed size
Iterator
Function: Simplify the loop code and save memory
It is an object that can remember the traversed position. The iterator object starts accessing from the first element of the collection until all elements have been accessed. Iterators can only go forward and not backward
Iterators have two basic methods: iter() and next().
Generator
Function: save a lot of memory
The function that uses yield is called a generator. The generator is an iterator that returns Functions can only be used for iterative operations. It is easier to understand that a generator is an iterator
Principle: During the process of calling the generator, the function will pause and save all current values every time it encounters yield. Run information, return the value of yield, and continue running from the current position the next time the next() method is executed
Closure refers to the object obtained by packaging the languages that make up functions and the execution environments of these languages in Python
The decorator is a kind of added function or class functions, which can quickly insert the same functionality into different functions or classes. Syntax: "@decorator name" is added before the function. Example:
Anonymous function: The function created using lambda is so-called anonymous, which means that a function is no longer defined in a standard form such as a def statement.
Benefits:
1. When using Python to write some execution scripts, using lambda can save the process of defining functions and make the code more streamlined.
2. For some abstract functions that will not be reused elsewhere, sometimes it is difficult to name the function. There is no need to consider naming issues when using lambda.
3. Use lambda to make the code easier to understand at certain times.
Application scenarios: Often used in combination with some built-in functions, such as map(), filter(), sorted(), reduce()
, etc.
Expression format:lambda parameter list: lambda body
##Case:
frame.applymap(lambda x: '%.2f' % x) frame.apply(lambda x: x.max() - x.min())6. How Can you improve the operating efficiency of Python?Use generators to optimize memoryOptimization of loops: multiple if elif condition judgments, you can write the condition most likely to occur first, so that you can Reduce the number of program judgments and improve efficiencyOptimize algorithm time: The time complexity of the algorithm has the greatest impact on the execution efficiency of the program. In Python, the time complexity can be optimized by selecting appropriate data structures, such as list and The time complexity of searching for a certain element in set is O(n) and O(1)7. Have you ever used classes? Do you know about inheritance? Please write an example using inheritance. Inheritance: refers to becoming a child object of an object or a subclass of a class by obtaining the properties and capabilities of the parent object, plus custom properties and capabilities. Rewriting: The method name is the same, the method is rewritten 8, deep copy and shallow copy
os module: Provides many functions related to the operating system.
sys module: Common tool scripts often call command line parameters .
re module: Provides regular expression tools for advanced string processing. For complex matching and processing, regular expressions provide a concise and optimized solution:
random module: Provides tools for generating random numbers.
json module
: Provides methods for Python to parse json data and convert to and from python format
time module
: A module used to process time in python
logging module
: module for log processing in python
xml module
: module for locating html tags in python crawler
Python adopts a reference counting mechanism as the main strategy, supplemented by two mechanisms: mark-clear and generational collection (intergenerational recycling, generational recycling)
Counting mechanism: Python's GC module mainly uses reference counting to track and recycle garbage. On the basis of reference counting, "mark-clear" can also be used to solve the problem of circular references that may occur in container objects. Further improve the efficiency of garbage collection by exchanging space for time through generational collection.
Mark-Clear:: The emergence of mark-clear breaks the circular reference, that is, it only focuses on those objects that may produce circular references. Disadvantages: The additional operations caused by this mechanism Proportional to the memory blocks that need to be reclaimed.
Generation-separated recycling Principle: All memory blocks in the system are divided into different collections according to their survival time. Each collection becomes a "generation", and the frequency of garbage collection increases with the The survival time of "generation" decreases with the increase. In other words, the longer an object lives, the less likely it is to be garbage, and the frequency of garbage collection for it should be reduced. So how to measure this survival time: It is usually measured by several garbage collection actions. If an object has gone through more garbage collections, it can be concluded that the object has a longer survival time.
The above is the detailed content of What are the most frequently asked questions in Python interviews?. For more information, please follow other related articles on the PHP Chinese website!