3 ------ advanced functions summary

Anonymous function

Named function: function name there, such as f1 (),

Anonymous function: no name, no way to directly use the name calling, and only in combination with certain methods.

Functions are often used in connection therewith: max / min / filter / sorted / map.

1.max maximum value.

salary_dict= {
    'nick':3000,
    'jason':10000,
    'tank':5000,
    'sean':2000,
}
print(f"max(salary_dict,key=labda name:salary_dict[name]):{max(salary_dict,key=lambda name:salary_dict[name])}")

注:max在字典中使用的时候默认取出的是key的值,

2.min takes a minimum value.

Take the same methods and the largest and most

3.fileter screening

list_res = [1,2,4,3,2,4,2,1,3,4,2,5]

res = filter(lambda num:num > 3,list_res)

print(list(res))

#输出为  [4, 4, 4, 5]

#用法:lambda num:num > 3为需要执行的操作,list_res为需要执行的可迭代对象
    输出需要使用list生成一个列表

Sort 4.sorted

list_name = [2,4,2,5,8,4,5,7,9,0,4,]

res = sorted(list_name,reverse = False)

print(res)

# 输出结果为 [0, 2, 2, 4, 4, 4, 5, 5, 7, 8, 9]
当输入的是True的时候表示的是反向排序,False是正向排序

5.map map

name_list = ['jason', 'tank', 'sean']

res = map(lambda name: f"{name} sb", name_list)
print(f"list(res): {list(res)}")

#输出结果为:list(res): ['jason sb', 'tank sb', 'sean sb']

Built-in functions

It has a lot of built-in functions, but the need to use very little, because some have been there a simple way to deal with, so here only for understanding.

abs(x)             用于返回绝对值

divmod(x,y)        函数中传入两个数字,返回的是x/y的一个结果的元组(商,余数)

pow(x,y)           用于求x的y次方

all(iterable)      函数中传入一个可迭代的对象,如果对象中的所有的数的bool值都为真才会返回True,不否则就返回Flase

any(iterable)      函数中传入一个可迭代的对象,如果对象中有一个数的bool值为真就返回True,如果所有的数都为0,就返回Flase

chr(x)             函数中传入一个ascii码,将ascii转换成对应的字符

ord(x)             函数中传入一个字符,将字符转换为对应的ascii码

Exception Handling

When executing the command, sometimes suggesting a variety of all kinds of errors, this time next command will not be executed again, this time on the need to have a method to handle exceptions.

  • grammar:

    try:
        code1
    except Exception as e:
        print(e) #可加可不加
        code2
    finally:   # 可加可不加
        code3
  • When the program is reported wrong, for processing

  • After being given, the following code is not performed

  • Abnormal capture can only capture logic error

  • finally No matter the role approach is that you do not report an error, execute the code in its retracted

    dic = {'a': 1}
    try:
        print(dic['b'])  # KeyError
        1 / int(num)  # 报错之后,不运行下面的代码
    
    except Exception as e:  # 万能异常,只要有错误,就捕捉
        print(e)  # e存储的是错误的内容,而不是错误的类型
        print('傻逼,不知道什么错误')
    finally:
        print('asdasf')
  • assert + conditions (assertion + condition) This method has now been eliminated

    Condition holds no error, the error does not hold

    num = input('num:')
    assert 1 == int(num)  
  • raise the initiative to report errors (that is, active error, no use)

    x = 1
    raise(x = 1) # 报错

Process-oriented programming

The core process-oriented programming is programming, the simple explanation is similar to the assembly line, to proceed step by step.

Oriented programming advantages and disadvantages

Advantages: go down step by step in the order, the logic is very clear

Cons: Because it is associated with up and down on a step wrong, the next step will follow together wrong.

Why error

In the process of programming, the programming logic error has occurred, it would not be in accordance with the code written

Correct logic to run, and this time will not receive the correct results, so the results will need to print troubleshooting process again step by step printing, it is time to look at what step of printing error has occurred, if it is the correct result is the correct result is calculated according to the correct logic.

Guess you like

Origin www.cnblogs.com/whkzm/p/11586614.html