The biggest headache about learning Python by yourself is not that you can’t get into it, but that you don’t know what level you’ll be at when you finish! For reasons of space, only the first 15 exercises are shared in this article.

This was given to me by amway, a byte giant, and it’s easy to pass the interview.

Attach the original link, the inside is 30 exercises, you can rest assured to eat: (don’t forget to look at the end of the receive mode) mp.weixin.qq.com/s/qvcrduneu…

1: What are the features and advantages of Python?

A: As an introductory programming language, Python has the following features and advantages: Explicable dynamic object-oriented simplicity Open source has strong community support

2: What is the difference between a deep copy and a shallow copy?

A: Deep copy is copying an object into another object, which means that if you make changes to a copy of an object, it doesn’t affect the original object. In Python, we use the function deepcopy() to perform a deepcopy and import the module copy as follows:

import copy b=copy.deepcopy(a)

The picture

A shallow copy copies a reference from one object to another, so if we make changes in the copy, the original object will be affected. We use function() to perform a shallow copy, as follows:

b=copy.copy(a)

The picture

  1. What’s the difference between a list and a tuple?

A: The main difference is that lists are mutable, whereas tuples are immutable. Here’s an example:

Mylist filling mylist = [1] [1] = 2 mytuple = (1 filling) mytuple [1] = 2

Traceback (most recent call last): File “<pyshell#97>”, line 1, in mytuple[1]=2

TypeError: ‘tuple’ object does not support item Assignment

For more on lists and tuples, see here: Q4 through Q20 are basic Python interview questions for beginners, but those with experience can also take a look at the questions and review the basic concepts.

  1. Explain the ternary operators in Python

Unlike C++, we don’t have that in Python, right? :, but we have this: [on true] if [expression] else [on false]

If the expression is True, the statement in [on True] is executed. Otherwise, the statement in [on False] is executed. Here’s how to use it:

A,b=2 min=a if a<b else b min

Running result: 2

print(“Hi”) if a<b else print(“Bye”)

Run result: Hi

  1. How do you implement multithreading in Python?

A thread is a lightweight process, and multithreading allows us to execute multiple threads at once. As we all know, Python is a multithreaded language with a multithreaded toolkit built in. The GIL (global interpreter lock) in Python ensures that a single thread is executed at a time. One thread saves the GIL and performs some operations before passing it to the next thread, which gives the illusion of parallelism. But in reality, it’s just threads running in rotation on the CPU. Of course, all the passing adds memory stress to program execution. Explain inheritance in Python

When a class inherits from another class, it is said to be a subclass/derived class that inherits from its parent class/base class/superclass. It inherits/retrieves all class members (attributes and methods). Inheritance allows us to reuse code and make it easier to create and maintain applications. Python supports the following types of inheritance: single inheritance: a class inherits from a single base class. Multiple inheritance: a class inherits from multiple base classes. Multi-level inheritance: a class inherits from a single base class, which in turn inherits from another base class

  1. What is a Flask?

Flask is a lightweight Web application framework written in Python. The WSGI toolkit uses Werkzeug and the template engine uses Jinja2. Flask uses BSD authorization. Two of these environmental dependencies are Werkzeug and Jinja2, which means it doesn’t need to rely on external libraries. That’s why we call it a lightweight framework. Flask sessions use signed cookies to let users view and modify session contents. It logs information from one request to another. However, to modify the session, the user must have the key flask.secret_key. 8. How is memory managed in Python?

Python has a private heap space to hold all objects and data structures. As developers, we don’t have access to it; the interpreter manages it. But with the core API, we can access some tools. The Python memory manager controls memory allocation. In addition, the built-in garbage collector reclaims all unused memory, so make it suitable for heap space.

  1. Explain the help() and dir() functions in Python

The Help() function is a built-in function to view a detailed description of the function or module’s purpose:

import copy help(copy.copy)

Help on function copy in module copy: Copy (X) Shallow copy operation on arbitrary Python objects. See the Module’s doc string for more info.

The Dir() function is also built into Python. Without arguments, Dir() returns a list of variables, methods, and defined types in the current range. Returns a list of attributes and methods for a parameter. The following example shows how to use dir:

dir(copy.copy)

The running results are as follows: [‘ annotations’, ‘call’, ‘class’, ‘closure’, ‘code’, ‘defaults’,’ delattr ‘, ‘dict’, ‘dir’, ‘doc’, ‘eq’, ‘format’, “ge”, “Get”, “the getattribute ‘, ‘globals’,’ gt ‘, ‘hash’, ‘init’, ‘init_subclass’, ‘kwdefaults’,” le “, “lt”, “module”, “name”, ‘ne’, ‘new’, ‘qualname’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘the sizeof’, ‘STR’, ‘subclasshook]

  1. Do YOU free all memory when exiting Python?

The answer is No. Modules that loop through references to other objects or objects from a global namespace are not completely released when Python exits. In addition, the memory portion reserved by the C library is not freed.

  1. What is a monkey patch?

Dynamically modify a class or module at run time.

class A:

def func(self):
    print("Hi")
Copy the code

def monkey(self):

print “Hi, monkey”

m.A.func = monkey a = m.A() a.func()

The result is: Hi, Monkey

  1. What is a dictionary in Python?

Dictionaries are something that programming languages like C++ and Java don’t have, and they have key-value pairs.

Roots = {let them therefore, as for, the deputies, 4-2, 1:1} type (roots)

<class ‘dict’>

roots[9]

The result is: 3 the dictionary is immutable, and we can also create it with a derivation.

Roots ={x**2:x for x in range(5,0,-1)} roots={x**2:x for x in range(5,0,-1)} roots

Results: {25:5, 16:4, 9:3, 4:2, 1:1}

  1. Explain the meaning of using args and *kwargs

When we do not know how many arguments to pass to a function, such as when we pass a list or tuple, we use *args.

def func(*args):

for i in args:
    print(i)  
Copy the code

Func (3,2,1,4,7)

The result is 3

2

1

4

Use **kwargs to collect keyword arguments when we do not know how many keyword arguments to pass.

def func(**kwargs):

for i in kwargs:
    print(i,kwargs[i])
Copy the code

func(a=1,b=2,c=7)

The command output is a.1

b.2

c.7

  1. Write Python logic to count the number of uppercase letters in a file

import os

os.chdir(‘C:\Users\lifei\Desktop’) with open(‘Today.txt’) as today:

count=0
for i in today.read():
    if i.isupper():
        count+=1
Copy the code

print(count)

Running result: 26

  1. What is a negative index?

Let’s start by creating a list like this:

Mylist =,1,2,3,4,5,6,7,8 [0]

A negative index is different from a positive index in that it starts from the right.

mylist[-3]

6 It can also be used for slicing in lists:

mylist[-6:-1]

Results: [3, 4, 5, 6, 7]

Finally, how to get 100 Python classic exercises.

The same rules, pay attention to attack zonghao [Python small white training camp] after reply “exercise” can be obtained ~

The article links: mp.weixin.qq.com/s/qvcrduneu…