Python basic interview (1)

1. A line of code to achieve the sum of 1-100

Use sum() function to sum

sum(range(1,101))
#5050
2. How to modify global variables inside a function

Function internal global declaration to modify global variables

a = 5
def fn():
	global a
	a = 4
fn()
prinit(a)
#4
3. List 5 python standard libraries
  • os: Provides many functions associated with the operating system
  • sys: usually used for command line parameters
  • re: regular matching
  • math: mathematical operations
  • datetime: Processing date and time
4. How to delete keys and merge two dictionaries in a dictionary

del and update methods
Insert picture description here

5. Talk about python's GIL

GIL is the global interpreter lock of python. If there are multiple threads running in the same process, one thread will occupy the python interpreter when running a python program (adding a lock or GIL), making other threads in the process unable to Run, other threads can only run after the thread has finished running. If a time-consuming operation is encountered during the thread running, the interpreter lock is unlocked and other threads are allowed to run. Therefore, in multithreading, the running of threads is still sequential, not at the same time.

In multi-process, because each process can be allocated resources by the system, it is equivalent to having a python interpreter for each process, so multi-process can realize the simultaneous operation of multiple processes. The disadvantage is that the process system resources are expensive.

6. How to implement list de-duplication in python

First go through the collection to remove the duplication, and then turn the list
Insert picture description here

7. What does args and **kwargs in fun ( args, **kwargs) mean?

Insert picture description here
Insert picture description here

8. The difference between python2 and python3 range (100)

python2 returns a list, python3 returns an iterator, saving memory

9. Explain in one sentence what kind of language can use decorators?

Languages ​​where functions can be passed as parameters, decorators can be used

10. What are the built-in data types in python
  • Integer --int

  • Boolean --bool

  • String --str

  • List

  • Tuple --tuple

  • Dictionary--dict

11. Briefly describe the difference between __new__ and __init__ in object-oriented

__init__ is the initialization method. After the object is created, it is called by default and can receive parameters, as shown in the figure

Insert picture description here
1. __new__ must have at least one parameter cls, which represents the current class. This parameter is automatically recognized by the Python interpreter when instantiated

2. __new__ must have a return value to return the instantiated instance. This point must be paid special attention to when implementing __new__ by yourself. You can return the parent class (via super(current class name, cls)) __new__ instance , Or directly an instance of __new__ of object

3. __init__ has a parameter self, which is the instance returned by this __new__. __init__ can complete some other initialization actions on the basis of __new__, and __init__ does not require a return value

4. If __new__ creates an instance of the current class, it will automatically call the __init__ function. The first parameter of the __new__ function called in the return statement is cls to ensure that it is the current class instance, if it is another class The name of the class; then the actual creation and return are instances of other classes. In fact, the __init__ function of the current class will not be called, nor will the __init__ function of other classes be called.
Insert picture description here

12. Briefly describe what the with method has done for me to open the processing file?

Insert picture description here
There may be some abnormal situations when opening files for reading and writing. If you follow the normal f.open

To write, we need to try, except, finally, to make abnormal judgments, and no matter what happens to the file in the end, we must execute finally f.close() to close the file. The with method helps us realize the f.close in finally

(Of course there are other custom functions, if you are interested, you can study the source code of the with method)

13. List [1,2,3,4,5], please use map() function to output [1,4,9,16,25], and use list comprehension to extract numbers greater than 10, and finally output [16 ,25]

The first parameter of the map() function is fun, the second parameter is generally a list, and the third parameter can be written in list or not, according to requirements
Insert picture description here

14. Method of generating random integers, random decimals, and decimals between 0 and 1 in python

Random integer: random.randint(a,b), generates an integer in the interval

Random decimals: I am used to using the numpy library and use np.random.randn(5) to generate 5 random decimals

0-1 random decimal: random.random(), no parameters are passed in the brackets

Insert picture description here

15. Avoid escaping. Which letter is added to the string to represent the original string?

r, which means the original string is needed, special characters are not escaped

Guess you like

Origin blog.csdn.net/weixin_42464956/article/details/107558098