Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 16 high-frequency interview questions for Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge about "Python's 16 high-frequency interview questions." In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

How does Python manage memory?

A: From three aspects, one object reference counting mechanism, two garbage collection mechanism, three memory pool mechanism

1. Reference counting mechanism of objects

Python uses reference counting internally to keep track of objects in memory, and all objects have reference counts. Cases where the reference count increases:

Assign a new name to an object

Put it in a container (such as a list, tuple, or dictionary) Reference count decrements:

Destroy object alias display using del statement

Reference out of scope or reassigned

sys.getrefcount( ) function gets the current reference count of an object

In most cases, the reference count is much larger than you might guess. For immutable data, such as numbers and strings, the interpreter shares memory among different parts of the program to conserve memory.

Waste Recycling

When an object's reference count reaches zero, it is disposed of by garbage collection.

When two objects a and b refer to each other, the del statement reduces the reference counts of a and b and destroys the names used to refer to the underlying object. However, since each object contains an application to other objects,

So the reference count doesn't go to zero and the object doesn't get destroyed. (This leads to a memory leak). To solve this problem, the interpreter periodically executes a loop detector that searches for loops of inaccessible objects and deletes them.

3. Memory pool mechanism

Python provides a garbage collection mechanism for memory, but it puts unused memory into the memory pool rather than returning it to the operating system.

Pymalloc mechanism. To speed up Python execution, Python introduces a memory pool mechanism to manage requests and releases of small blocks of memory.

All Python objects smaller than 256 bytes use the pymalloc implementation allocator, while large objects use the system's malloc.

Python objects, such as integers, floating-point numbers, and Lists, have their own private memory pools, and objects do not share their memory pools. This means that if you allocate and release a large number of integers, the memory used to cache those integers cannot be allocated to floating point numbers.

What is lambda function? What's good about it?

lambda expression is usually used when you need a function but don't want to bother naming it, i.e. anonymous functions

lambda function: primary use is to point to short callback functions lambda [arguments]:expression

>>> a=lambdax,y:x+y

>>> a(3,11)

How to convert tuple and list in Python?

A: Just use tuple and list functions directly. Type() can determine the type of object.

(4) Please write a Python code to delete duplicate elements in a list

a

Using the set function, set(list) Using the dictionary function,

>>>a=[1,2,4,2,4,5,6,5,7,8,9,0]>>> b={}>>>b=b.fromkeys(a)>>>c=list (b.keys())>>> c (v) Programming sorts, and then determines from the last element a=[1,2,4,4,5,7,10,5,7,8,9,0,3]a.sort() last=a[-1]for i inrange(len(a)-2,-1): if last==a:del a else:last=a print(a)(vi) How do you copy an object in Python? (Difference between assignment, shallow copy and deep copy)

A: Assignment (=) creates a new reference to the object, and changing any of the variables affects the other. Shallow copy: Create a new object, but it contains references to items contained in the original object (if one of the objects is modified by reference, the other will change){1, full slice method;2, factory function, such as list();

3, copy module copy() function}

Deep copy: creates a new object and recursively copies all the objects it contains (modifying one of them leaves the other unchanged){deep.deepcopy() function of copy module}

(7) Introduce the use and function of except?

A: Try…except…except…[else…][finally…]

Executes the statement under try, and if an exception is thrown, execution jumps to the except statement. Try execution for each except branch in sequence, and if the exception thrown matches the exception group in except, execute the corresponding statement.

If none of the except matches, the exception is passed to the next top-level try code that calls this code.

The statement under try executes normally, and the else block code is executed. If an exception occurs, it is not executed. If there is a finally statement, it is always executed.

What is the function of the pass statement in Python?

A: The pass statement does not perform any operation, generally as a placeholder or to create a placeholder program, whileFalse:pass

(9) Introduce the use of the range() function under Python?

A: List a set of data, often used in the for in range() loop

How do I query and replace a text string in Python?

A: You can use sub() or subn() functions in the re module to query and replace,

Format: sub(replacement, string[,count=0])(replacement is the text to be replaced, string is the text to be replaced, count is an optional parameter indicating the maximum number of replacements)

>>> import re>>>p=re.compile('blue|white|red')>>>print(p.sub('colour','blue socks and red shoes')) colour socks and colourshoes>>>print(p.sub('colour','blue socks and red shoes',count=1)) colour socks and redshoes

The subn() method performs the same effect as sub(), except that it returns a two-dimensional array containing the new string after substitution and the total number of substitutions

What is the difference between match() and search() in Python?

A: match(pattern,string[,flags]) in the re module, check whether the beginning of string matches pattern.

In the re module re.search (pattern,string[,flags]), search string for the first match of pattern.

>>>print(re.match ('super ', ' superstition').span()) (0, 5)>>print(re.match ('super ', ' superable')) None>>print(re.search ('super ', ' superstition').span()) (0, 5)>>print(re.search ('super ',' superable').span()) (2, 7)(XII) What's the difference between matching HTML tags with Python and?

A: The terms are greedy matching ( ) and non-greedy matching ( )

For example:

test :test : (13) How do you generate random numbers in Python?

A: Random module

Random integer: random.randint(a,b): returns random integer x,a

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report