25 Common Python FAQs Interview Questions

1. What Python and what are the benefits of using it?

Python is a programming language with its own packages, modules, thread exception handling and automatic memory management.

It's simple, convenient and open source. There are many built-in data structures.

2. What is PEP8,?

PEP8 is a programming specification with recommendations to make your programming more readable.

3. How is Python interpreted?

Python is an interpreted language. Its source code can be run directly. The Python interpreter will interpret the source code into an intermediate language, and then translate it into machine code for compilation.

4. How does Python manage memory?

All memory management in Python is managed by a private heap space. All Python objects and data structures will be in this private heap space, and programmers do not have permission to access it. Allocating memory for heap allocation is performed by the memory management module. Its core API will allocate some calls to programmers. Python has its own memory management module that reclaims and releases unused memory so that it can be used by other programs.

From three aspects, 1. Reference counting system, 2. Garbage collection mechanism, 3. Memory pool mechanism

1. Python uses a reference counting mechanism internally. All objects have a reference count. The reference count tracks the changes of the object. The count increases as follows:

1. An object is assigned a new name,

2. Put it into a container, (collection, array or tuple, etc.)

The case where the reference count is reduced

1. Out of scope or reassigned

2. Use the del statement

garbage collection

When an object's reference count reaches 0, the garbage collection mechanism collects them

memory pool mechanism

Python provides a garbage collection mechanism for memory, which puts unused memory into a memory pool instead of returning it to the system.

5. What data structures does Python have?

Python's own data structures are divided into mutable and immutable, and mutable includes sets, arrays, and dictionaries. Immutable are tuples, strings, numbers.

6. How to convert a number into a string?

You can use Python's own str() to convert a number to a string.

7. What is the difference between xrange and range?

xrange returns an xrange object, and range returns an array.

8. How to realize mutual conversion between tuple and list?

You can directly use the functions of tuple() and list(), and type() can determine the type of the object.

9. Please write a piece of code to implement repeated elements in the list collection

1.set(list)

2.a=[1,2,3,4,5,6,7,8,9,0]

   b={}

   b=b.fromkeys(a)

   c=list(b.keys())

10. How to generate random numbers in Python?

random module

Random integer: the returned random integer random.randint(a,b), a<=x<=b

random.randrange(start,stop,[step]), the returned random integer does not include the step size, and the range is between start-stop

Random real number: random.random(), which returns a floating point number between 0-1.

random.uniform(a,b), returns a floating point number between ab.

11. When using Python to match HTML tags <.*> and <.*? > What's the difference?

<.*> is the most greedy match, <.*? is a non-greedy match

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

match() checks if the value of pattern matches at the beginning of the string, and search() searches the string for the first value that matches pattern.

12. How to use Python to replace text and query a string?

sub(replace, string, [count=0]), replace is the text to be replaced, string is the text to be replaced, and count is the maximum count.
























Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603703&siteId=291194637