# Review the basics of python

#### 1. What are iterators and generators.

##### 1.1 iterators

Iterator is one of the most powerful functions in python. It is a collection of stored data.

The iterator can iterate over and access each element.

Each iterator contains an iter() and next() method.

iter() creates an iterator and next() gets the next value.

```#Instantiate an iterator
list = [1, 2, 3, 4]
it = iter(list)
print(next(it)) #Traversal to get the value in it
print(next(it))
print(next(it))
print(next(it))
---
#The value obtained is
1
2
3
4
#Iterators can also use a for loop to iterate.

```

Create an iterator object. The iterator must contain the iter() method and the next() method

```class Number():
def __iter__(self): #Create iter() method
self.x = 1
return self
def __next__(self):#Create next() method
a = self.x
self.x += 1
return a
n1 = Number()
n2 = iter(n1)

print(next(n2))
print(next(n2))
print(next(n2))
print(next(n2))
----
#The obtained value is
1
2
3
4
```
##### 2.2 generator

In python, the generator that uses the yield function is called. The generator is a function used to generate iterators. In short, yield is an iterator. In the generator, if the function is not called once, yield will pause and save the value of the current function, and continue to run from the current position the next time the next() method is executed

Take a Fibonacci sequence as an example

```import sys
def fibonacci(n):
a, b, container = 0, 1, 0
while True:
if container > n:
return
yield a
a, b = b, a+b
container += 1
fn = fibonacci(10)
while True:
try:
print(next(fn), end=" ")
except:
sys.exit()
```

#### 2. The difference between deep copy and shallow copy

When it comes to shallow copy and deep copy, we have to talk about variable objects and immutable objects.

##### 1.1 variable object

Variable object means that the value pointed to by the object address can be modified without changing the address pointed to by the object

##### 1.2 immutable object

Immutable object means that the value pointed to by an object address cannot be modified. If this value is modified, the address pointed to by this object will be modified.

##### 1.3 the difference between deep copy and shallow copy is mainly reflected in variable objects

In shallow copy, the value pointed to by the modified object is the same as the original value, but the address pointed to is different, but the address pointed to by the elements in the variable object is the same.

In shallow copy, the value pointed to by the modified object is the same as the original value, but the address pointed to and the address pointed to by the elements in the variable object are different.

```import copy
a=[1,2,3,4,5,['a','b']]
b = copy.copy(a) #Shallow copy
c = copy.deepcopy(a) #Deep copy
print('a',id(a), id(a))
print('b:',b, id(b), id(b))
print('c:',c, id(c), id(c))
----
#Gets the value of the
a 140351872525768 140351872525832
b: [1, 2, 3, 4, 5, ['a', 'b']] 140351872525768 140351872525960
c: [1, 2, 3, 4, 5, ['a', 'b']] 140351872575432 140351872528072
```

#### 3. Explain what is a decorator and its function

Decorator is to add its function without modifying the function. Simple and easy to use Zhang Shiqi.

Decorators are mainly used in:

Log output

Type check

Authority authentication

retry

Configure domain name

#### 4. How to read files in Python

In python, you need to use the open method to read files or the with open... as method. The first is to call the close() method to close the cursor object after calling open.

Using with open as will automatically call the close() method, so we don't need to call the close() method ourselves.

#### 5. How to execute (operate) database in Python

Link database in python, such as mysql database

```import pymysql
#Create cursor object
conn = pymysql.connect(host='localhost', port=3306, user='root',

cursor = conn.cursor()

sql = 'select * from books;'

cursor.execute(sql)

items = cursor.fetchall()

for item in items:

print(item)

cursor.close()
```
```The specific steps are
Step 1: create a link mysql Database objects
The second step is to create a cursor object conn
The third step is to implement sql sentence
The fourth step is to close the cursor object
The fifth step is to close the linked object
```

Tags: Python

Posted by zoki on Mon, 09 May 2022 14:17:39 +0300