SoFunction
Updated on 2024-12-11

6 Must Have Classic Interview Questions for Python Engineers

Question 1: How to implement tuple and list conversion inside Python?

The tuple(seq) function converts all iterable sequences into a tuple, the elements remain the same, and the ordering remains the same.

list to tuple:

temp_list = [1,2,3,4,5]

Forced conversion of temp_list: tuple(temp_list)

Determine if the conversion was successful: print(type(temp_list))

The function list(seq) converts all sequences and iterable objects into a list, with unchanged elements and ordering

tuple to list:

temp_tuple = (1,2,3,4,5)

The method is similar, just do a forced conversion: list(temp_tuple)

Determine if the conversion was successful: print(type(temp_tuple))

Question 2: Difference between search() and match() inside Python?

They are both in the re module

·The match() function matches at the start of the string, and returns None if it doesn't match;

·search() scans the entire string for matches;

match()

>>> import re
>>> print(('hello','helloworld').span())  # Match the beginning to
(0, 5)
>>> print(('hello','nicehelloworld').span()) # It didn't match the beginning of
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print(('hello','nicehelloworld').span())
AttributeError: 'NoneType' object has no attribute 'span'
>>>

search()

>>> print(('a','abc'))
<_sre.SRE_Match object; span=(0, 1), match='a'>
>>> print(('a','bac').span())
(1, 2)
>>>

Conclusion: match() is more limited in its usage

Question 3: How to delete a file using Python?

Use of the os module

(path)

Deletes the file path, throwing an OSError error if path is a directory. To delete a directory, use rmdir().

remove() is the same as unlink().

('')
(path)

Delete a directory recursively. Similar to rmdir(), removedirs() will remove the parent directory if the subdirectory was successfully removed; however, if the subdirectory was not successfully removed, an error will be thrown.

For example, ("a/b/c") will delete the c directory first, then b and a. If they are empty, the subdirectories cannot be deleted successfully and an OSError exception will be thrown.

(path)

Delete the directory path, path must be an empty directory, otherwise an OSError error is thrown.

Question 4: What is the difference between is and ==?

The three basic elements of an object in hon are: id (identity), type (data type) and value (value).

id Identity, which is the address in memory.

Full example

>>> a = 'hello'
>>> b = 'hello'
>>> print(a is b)
True
>>> print(a==b)
True
>>> a = 'hello world'
>>> b = 'hello world'
>>> print(a is b)
False
>>> print(a == b)
True
>>> a = [1,2,3]
>>> b = [1,2,3]
>>> print(a is b)
False
>>> print(a == b)
True
>>> a = [1,2,3]
>>> b = a
>>> print(a is b)
True
>>> print(a == b)
True
>>>

== is a comparison operator in python's standard set of operators that is used to compare two objects to determine if their values are equal.

is is also called the identity operator (object identifier), which compares objects that have the same unique identifier, i.e., id (address in memory).

Checking that a is b is actually equivalent to checking that id(a) == id(b). And checking that a == b is actually a call to the __eq()__ method of object a. a == b is equivalent to a.__eq__(b).

Here's another question: why does a is b return True when a and b are both "hello", but False when a and b are both "hello world"?

This is because Python's string-resident mechanism works in the former case. For smaller strings, in order to improve system performance Python will keep a copy of the value and just point to it when creating a new string.

So "hello" has only one copy in memory, and a and b have the same id value, whereas "hello world" is a long string that doesn't reside in memory, and Python creates separate objects to represent a and b, so their values are the Python creates separate objects for a and b, so they have the same value but different id values.

Try to see if their ids will still be equal when a=247,b=247. In fact Python uses a pool of small integer objects to optimize speed and avoid frequent memory requests and destruction for integers. Python's definition of a small integer is [-5, 257), which means that only numbers between -5 and 256 will have equal ids, and nothing beyond that.

>>> a = 247
>>> b = 247
>>> print(a is b)
True
>>> a = 258
>>> b = 258
>>> print(a is b)
False
>>>

is is a check to see if two objects are pointing to the same memory space, while == is a check to see if their values are equal. is is stricter than ==.

Question 5: a=1, b=2, Swap values of a and b without using intermediate variables?

Method 1

>>> a = 5
>>> b = 6
>>> a = a+b
>>> b = a-b
>>> a = a-b

Method II

>>> a = a^b
>>> b = b^a
>>> a = a^b

Method III

a,b = b,a

Question 6: Tell me about your understanding of zen of python and what are your ways of seeing it?

The Zen of Python

import this

Test Question Extension:

language property

1. Talk about the differences between Python and other languages.
2. briefly describe interpreted and compiled programming languages
types of interpreters and the associated features?
4. Tell us what you know about the differences between Python3 and Python2?
5. What is the difference between int and long in Python3 and Python2?
What is the difference between a range and a range?

编码规范

7.What is it? PEP8?
8. Know the Zen of Python?
9. What do you know about docstring?
10. Understand type annotations?
11. Give examples of Python objects that you know have naming conventions, such as methods or classes.
How many kinds of annotations are there in
13. How to elegantly comment a function?
14. How do I add comments to variables?
Whether Tab and Space are mixed in code indentation.
16. Is it possible to import multiple libraries in a single import?
17. What do I need to keep in mind when naming Py files?
18. Examples of tools that standardize Python code style

To this article on the Python engineer must test the 6 classic interview questions on the article is introduced to this, more related to the 6 Python engineer must test the content of the interview questions, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!