Article \ | tide

Source: Python Technology “ID: pythonall”

The end of the year is approaching, for the preparation of job-hopping after the preparation of an interview guide, I hope you in the way of salary and growth more guidance!

What’s the difference between python2 and python3?

  • Python3 uses print to wrap the print in parentheses, such as print(‘hi’)
  • Python2 can use either curly braces or a space to separate the print, such as print ‘hi’
  • Python2 range(1,10) returns lists. Python3 returns iterators to save memory
  • ASCII is used in Python2 and UTF-8 is used in Python
  • In Python2, Unicode represents a sequence of strings and STR represents a sequence of bytes
  • In Python3, STR represents a sequence of strings and byte represents a sequence of bytes
  • Python2 introduces a coding statement for normal display of Chinese, which is not required in python3
  • Python2 is raw_input(), python3 is input()

Python code _args, *_kwargs meaning and usage?

Args: arguments

Kwargs: short for keyword arguments

List the five Python standard libraries?

  • OS: provides a number of functions associated with the operating system
  • Sys: usually used for command line arguments
  • Re: indicates a regular match
  • -Leonard: Math
  • Datetime: processing datetime

What are Python mutable and immutable data types?

Mutable data types: lists, dictionaries, collections

Immutable data types: numbers, strings, tuples

Magic methods in Python and their purpose?

__init__ : Object initialization method

__new__ : Method used when creating objects, used in single-column mode

__str__: When print is used to output an object, as long as the __str__(self) method is defined, the return data from this method will be printed

__del__ : Deletes a method executed by an object

What are the functions of OS and SYS modules in Python?

OS module: responsible for the program and operating system interaction, provides access to the underlying interface of the operating system. The SYS module is responsible for the program’s interaction with the Python interpreter and provides a set of functions and variables used to manipulate python’s runtime environment.

Describe the Python reference counting mechanism?

Python garbage collection is dominated by reference counting, supplemented by mark-sweep and generational sweep, which deal with the problem of circular references.

What is the difference between Python assignment, shallow copy, and deep copy?

Python has three types of assignment: direct assignment, shallow copy, and deep copy.

Direct assignment: is a reference to an object. (equivalent to giving the original object individual name), for example, there is a person named Zhang SAN, nicknamed Xiao Zhang, the object reference is similar, although change the name, but the two names refer to the same person.

Shallow copy, which copies the parent object, not the internal child object. (only from the shallow word can be seen that the copy of something is not deep, can be understood as a copy of only one layer) {1, completely slice method; Factory functions, such as list(); Copy () {copy()}

Deep copy, which contains a copy of the object inside the object. So a change in the original object does not change any child elements in the deepcopy.

What are the split(), sub(), and subn() functions in Python?

Split () : “splits” a given string into a list using the regular expression pattern.

Sub () : Finds all the substrings that the regular expression pattern matches and replaces them with different strings

Subn () : This is similar to sub() and also returns a new string.

What is the difference between sort and sorted methods?

Sorting a list using sort() changes the list itself and does not return a new list, and sort() cannot sort dict dictionaries;

The sorted method sorts iterable sequences and generates new sequences. By default, dict sorts are sorted according to dict keys, and the final result is a list of sorted keys.

Sorted still works on tuples, dict, but sort doesn’t;

Explain mutable and immutable types in Python?

1. Mutable types in Python include list,dict; Immutable types include string, number, and tuple.

2. Mutable types pass the address in memory when performing modification operations, that is, they change the value in memory directly without creating new memory.

3. When an immutable type is changed, it does not change the value in the original memory address. Instead, it creates a new memory and copies the value in the original address to operate on the value in the new memory.

What is the difference between class methods, class instance methods, and static methods in Python?

Classmethod: is a method of a class object, which is defined with @classmethod at the top. The parameter CLS represents a class object. Both class objects and instance objects can be called

Class instance method: a method used by a class to instantiate an object, which can only be called by the instance object. The parameter self refers to the object itself.

Staticmethod: is an arbitrary function decorated with @staticmethod. Instance objects and class objects can be called. However, no properties or methods of the class or instance can be used in the method body.

How do you write functions in Python?

There are four principles for writing functions in Python:

  • Function design should be as short as possible, the nesting level should not be too deep. Avoid excessively long functions, preferably within 3 layers of nesting
  • Function declarations should be reasonable, simple, and easy to use. In addition to the function name can accurately reflect its general function, the design of parameters should also be concise, the number of parameters should not be too many
  • Function parameter design should consider backward compatibility. Degradation can be avoided by adding default parameters
  • When a function does one thing, try to keep the level of abstraction as consistent as possible, with all statements at the same granularity. Doing more than one thing in a function is bad for code reuse;

Explain the concepts of synchronous, asynchronous, blocking, and non-blocking?

  • Synchronization: Multiple tasks are executed in sequence. After one task is executed, the next task can be executed.
  • Asynchrony: Multiple tasks can be executed at the same time in no order. Sometimes one task may need to retrieve the results of another task that is being executed at the same time. This is called a callback!
  • Block: If the caller is stuck, the caller cannot proceed, that is, the caller is blocked.
  • Non-blocking: Execution can continue if it is not stuck, that is, non-blocking.

Synchronous asynchrony versus multitasking and blocking versus non-blocking versus code execution.

Merge two lists and remove duplicate elements?

list1 = ['b'.'c'.'c'.'a'.'f'.'r'.'y'.'e'.'e']
list2 = ['t'.'y'.'x'.'y'.'z'.'e'.'f']
def merge_list(*args):
    s = set()
    for i in args:
        s = s.union(i)
    print(s)
    return s

merge_list(list1,list2)
Copy the code

How do I query and replace a string in text?

tempstr = "hello python,you,me,world"
print(tempstr.replace("hello"."python"Sub () tempstr ="hello python,you,me,world"
import re
rex = r'(hello|Use)'
print(re.sub(rex,"HELLO",tempstr))
Copy the code

conclusion

There are many more Python interview questions, and I hope they will be helpful.

reference

Github.com/DasyDong/in…

PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan!

Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!

[Code access ****]

Identify the qr code at the end of the article, reply: 1024