day 014 summary

Yesterday Review

Iterator

Iterator object must be iterable, iterables not necessarily iterator object

Iterables

Containing the __iter__data type of the method

In addition to numeric types are iterables

Iterator object

Contains __iter__, __next__the data type of method

File is the iterator object

Iterables plus __iter__will become iterator object

Iterator object plus __iter__method still iterator object

for circulation principle

lt[1,2]
lt_iter=lt.__iter__()
while True:
    try:
        print(lt_iter.__next__())
    except StopIteration:
        break

A triplet of expressions

Condition holds if condition else condition is not satisfied

List comprehensions

[i for i in range(10)]

Dictionary of formula

{i :i for i in range(10)}

zip

A plurality of read iterables disposable objects each iteration of the elements, makes up a tuple

Generator expressions

(i for i in range(10))

Builder

Nature is an iterator, is custom iterators, function containing the yield keyword

def ge():
    yield

yield

  1. Pause function
  2. A yield value taken by the next
def range(start, stop, step):
    while start < stop:
        yield start
        start += step
       
for i in range(1,10,2):
    print(i)

Recursion

Calls the function itself, there is an exit condition

count = 0
def a():
    global count
    print(count)
    
    if count == 100:
        return
    count += 1
    a()
    
a()

Content Today

# Anonymous function

Anonymous function with no name

Lambda keyword need anonymous

lambda 参数:返回值

Anonymous functions generally not used alone, and the filter () / map () / sorted () / list sort () built-in method used in conjunction

salary_dict = {
    'nick': 3000,
    'jason': 100000,
    'tank': 5000,
    'sean': 2000
}

sort

salary_list = list(salary_dict.items())
salary_list.sort(key=lambda i:i[1])
print(salary_list)#[('sean', 2000), ('nick', 3000), ('tank', 5000), ('jason', 100000)]

sorted

salary_list=list(salary_dict.items())
s=sorted(salary_list,key=lambda i:i[1])
print(s)#[('sean', 2000), ('nick', 3000), ('tank', 5000), ('jason', 100000)]

max min filter

salary_list=list(salary_dict.items())
print(max(salary_list,key=lambda i:i[1]))#('jason', 100000)
print(min(salary_list,key=lambda i:i[1]))#('sean', 2000)
print(list(filter(lambda i: i[1] < 5000, salary_list)))  #[('nick', 3000), ('sean', 2000)]

Do not convert to the list

salary_dict = {    
    'nick': 3000,    
    'jason': 100000,    
    'tank': 5000,    
    'sean': 2000
}
#sorted
s=sorted(salary_dict,key=lambda i:salary_dict[i])
print(s)#['sean', 'nick', 'tank', 'jason']
#max
res = max(salary_dict, key=lambda name: salary_dict[name])
#min
res = min(salary_dict, key=lambda name: salary_dict[name])
#filter
res = filter(lambda item: salary_dict[item] > 3000, salary_dict)
#map
def function1(item):
    return item + 2
res = map(function1, [1, 2, 3, ])
print(res)
print(list(res))

Built-in functions

bytes decoded characters

res = bytes('中国', encoding='utf8')
print(res)#b'\xe4\xb8\xad\xe5\x9b\xbd'

chr / ord chr () refer to ASCII code table into corresponding digital characters; the ord () converts the character into a corresponding digital

print(chr(97))#a
print(ord('a'))#97

Rounding divmod / modulo

print(divmod(10,4))#(2,2)

enumerate iteration with indexes (******)

lt=[1,2,3]
for i in enumerate(lt):
    print(i)
#(0, 1)
(1, 2)
(2, 3)

eval (***) to quote strings removed, what is left behind is what

s='[1,2,3]'
print(eval(s))#[1,2,3]

determine whether the hash hash

print(hash(1))#1

abs absolute value

print(abs(-1))#1

all elements within an iteration object that can be compared with all True True

print(all([1,2,3]))#true

As long as any one element compared to True True

print(any([1,0,0]))#true

bin / oct / hex 2 binary 8 Hexadecimal

print(bin(123))
print(oct(123))
print(hex(123))

dir lists all methods modules

frozenset: not set change, similar to the tuple

s=frozenset({1,2,3})
print(s)

gloabls / locals # list all global variables / local variables

print(globals())
print(locals())

pow

print(pow(2,2))#4

slice

s=slice(1,5,2)
lt=[1,2,3,4,5,6]
print(lt[s])#2 4

sum

print(sum([1,2,3,4,5,6]))

__import__By introducing the string module

time=__import__('time')
print(time.time())

Exception Handling

num=input('请输入数字')
dic={'a':1}
try:
    print(dic['b'])#KeyError
    1/int(num)
except Exception as e:#万能异常,只要有错误就捕捉
    print(e)#错误的描述信息
finally:#无论你报不报错,都会执行这一行
    print('finally')

After the try in error, do not run the following code

Abnormal capture can only capture logic error

Process-oriented programming

Against procedural programming

Oriented Programming process: Step by step (a function of a function), a function output is the input of the next function

Advantages: improve the independence of the code, the process clear

Disadvantages: a mistake, let the program directly bounced off

Guess you like

Origin www.cnblogs.com/zqfzqf/p/11586904.html
014