What was learned on the fourth day

scope dependent

globals() - get global variables

locals() - get local variables in the namespace where this method is executed

Data type related:

type(o) returns the data type of the variable o

id(o) o is a parameter, returns the memory address of a variable

hash(o) o is a parameter that returns the hash value of a hashable variable. An error will be reported after a non-hashable variable is hashed.

File operation related:

open() opens a file and returns a file operator (file handle)

There are 6 modes for operating files: r, w, a, r+, w+, a+, each of which can be operated in binary form (rb, wb, ab, rb+, wb+, ab+)

The encoding can be specified with encoding.

View all built-in methods of the type the parameter belongs to

dir() defaults to view properties in the global space, and also accepts a parameter to view the method or variable in this parameter

The filter() function receives a function f and a list. The function of this function f is to judge each element and return True or False. According to the judgment result, filter() automatically filters out the elements that do not meet the conditions, and returns the elements that meet the conditions. A new list composed.

The map() function receives two parameters, one is a function and the other is a sequence, map applies the incoming function to each element of the sequence in turn, and returns the result as a new list.

Sorts a given List L,
method 1. Sorts with List's member function sort, sorts locally, does not return a copy
Method 2. Sorts with built-in function sorted (since 2.4), returns copy, original input unchanged

iterable: iterable type;
key: pass in a function name, the parameter of the function is each item in the iterable type, sorted according to the size of the return value of the function;
reverse: sorting rule. reverse = True descending order or reverse = False ascending order , with default values.

Builder

Monitoring example:

import time


def tail(filename):
    f = open(filename)
    f.seek(0, 2) #count from the end of the file
    while True:
        line = f.readline() # read a new line of text in the file
        if not line:
            time.sleep(0.1)
            continue
        yield line

tail_g = tail('tmp')
for line in tail_g:
    print(line) 
produces clothes:
def produce():
    """Produce clothes"""
    for i in range(2000000):
        yield "Produced %s garment"%i

product_g = produce()
print(product_g.__next__()) #Want a dress
print(product_g.__next__()) #One more dress
print(product_g.__next__()) #One more dress
num = 0
for i in product_g: #Want a batch of clothes, such as 5 pieces
    print(i)
    num +=1
    if num == 5:
        break 
generator expression:

1. Replace the [] of the list comprehension with () to get the generator expression

2. List comprehension and generator expressions are both convenient programming methods, but generator expressions are more memory-efficient

3.Python not only uses the iterator protocol, but also makes the for loop more general. Most of the built-in functions also use the iterator protocol to access objects.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325330224&siteId=291194637