Home  >  Article  >  Backend Development  >  What are the common questions in Python interviews?

What are the common questions in Python interviews?

coldplay.xixi
coldplay.xixiOriginal
2020-10-27 09:58:144109browse

Frequently asked questions in Python interviews include: 1. How is Python interpreted? 2. What is PEP8? 3. How does Python manage memory? 4. What is a Python decorator? 5. What built-in Python provides Type etc.

What are the common questions in Python interviews?

## Lots of free learning recommendations, please visit python tutorial(Video)

Frequently asked questions in python interviews are:

1. What is Python? Briefly describe and explain the advantages and disadvantages?

Python is an object-oriented interpreted computer programming language that combines interpretation, compilation, interactivity and object-oriented scripting language.

Advantages: open source, free, simple, easy to learn, portable, scalable, easy to maintain, and high development efficiency.

Disadvantages: slow running speed, code cannot be encrypted.

2. How is Python interpreted?

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

3. What is PEP8?

PEP8 is a programming specification that provides suggestions for making programs more readable.

4. How does Python manage memory?

1) Reference counting mechanism: Python uses reference counting internally to keep track of objects in memory.

2) Garbage recycling mechanism: When the reference count of an object is zero, it will be processed by the garbage collection mechanism;

cyclic garbage recovery to ensure the release of the loop reference object.

3). Memory pool mechanism:

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

Pymalloc mechanism: In order to speed up the execution efficiency of Python, Python introduces a memory pool mechanism to manage the application and release of small blocks of memory.

For Python objects, such as integers, floating point numbers and Lists, they have their own independent private memory pools, and their memory pools are not shared between objects. This means that if you allocate and free a large number of integers, the memory used to cache these integers can no longer be allocated to floating point numbers.

Python decorator is a function, a unique change in Python that makes it easier to modify the function. It allows a function to dynamically generate additional functions without modifying its own function definition.

5. What is a Python decorator?

Python decorator is a function, a unique change in Python that makes it easier to modify the function. It allows a function to dynamically generate additional functions without modifying its own function definition.

Iterators, generators

Iterable objects: objects that can be traversed using for-in are all iterable objects

In Python, if an object has __iter__ () method or __getitem__() method, the object is said to be iterable (Iterable); the __iter__() method is used to allow the object to be traversed using a for...in loop, and the __getitem__() method is used to make the object Elements in the instance can be accessed through "instance name [index]". In other words, as long as one of the two conditions is met, the object can be said to be iterable. Obviously, data types such as List, Tuple, Dictionary, and String are all iterable.

Iterator: Iterator is a way to access the elements of a collection. An object that can be traversed using for-in and iterated using the next function

In Python, if an object has an __iter__() method and a __next__() method, it is called an iterator ( Iterator); the __iter__() method allows the object to be traversed using a for...in loop, and the __next__() method allows the object to access the next element through next (instance name). Note: These two methods must be available at the same time to be called an iterator. Although data types such as List, Tuple, Dictionary, and String are iterable, they are not iterators because they do not have a next() method.

6. Function closure

A function that references a free variable is a closure. The referenced free variable exists with the function, even if it has left The environment in which it was created is no exception.

7. What is the difference between an array and a tuple?

The difference between arrays and tuples: array contents can be modified, while tuple contents are read-only. Additionally, tuples can be hashed, e.g. as dictionary keys.

8. Class methods and static methods

method: Called through an instance, you can reference any properties and methods inside the class

classmethod: No instance required , you can call class attributes and class methods, but you cannot get ordinary member attributes and methods

staticmethod: No need to instantiate, you cannot get any properties and methods inside the class, a completely independent method

9. What built-in types does Python provide?

Integer (int), string (str), tuple (tuple), Boolean (bool), set (set), list (list), dictionary (dict)

10 , what are dictionary comprehensions and list comprehensions?

They are syntax structures that make it easy to create dictionaries and lists.

How are parameters passed by value and reference passed?

Everything in Python is a class, and all variables are references to an object. The value of the reference is determined by the function and therefore cannot be changed. But if an object can be modified, you can change the object.

11. What is lambda in Python?

This is an anonymous function that is often used for a single expression in code.

12. What is pass in Python?

pass is a placeholder used to represent blank and will not be executed.

13. What is the difference between shallow copy and deep copy in Python?

copy.copy() Shallow copy: The object is copied, but the value of the copied object still points to the value of the original object (equivalent to a reference). If the elements of the copied object are modified, the copied object's The value will also be modified.

copy.deepcopy() deep copy: not only copies the object, but also copies the elements in the object, obtaining a brand new object, which is completely independent of the copied object; but this requires sacrificing a certain amount of time and space .

14. What are modules and packages in Python?

In Python, modules are a way to build programs. Every Python code file is a module and can reference other modules, such as objects and properties.

A folder containing a lot of Python code is a package. A package can contain modules and subfolders.

The above is the detailed content of What are the common questions in Python interviews?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn