14 三元表达式、内置函数、面向过程编程
目录:三元表达式、内置函数、面向过程编程
一、三元表达式:
def func(): if x>y: return x else: return y res=func(1,2) x=1 y=2 res=x if x>y else y print(res)
二、内置函数
重点掌握:
int float str list tuple dict set bool bytes s=frozenset({1,2,3}) #不可变的集合 print(types(s)) s="print(‘hello')" s="[1,2,3]" l=eval(s) print(l[0]) with open('a.txt',mode='rt',encoding='utf-8') as f: data = f.read() # print(data,type(data)) # '{"name":"egon","age":18}' dic = eval(data) print(dic['name']) print("start===") i=0 while i<3: print(i) i+=1 print("end===") import base64 #解密 exec(base64.b64decode('cHJpbnQoInN0YXJ0Li4uIikKaSA9IDAKd2hpbGUgaSA8IDM6CiAgICBwcmludChpKQogICAgaSs9MQpwcmludCgnZW5kLi4uJykK')) print(chr(65)) print(ord("A")) l=[1,"a",3] res1=l[::-1] res1=list(reversed(l)) #反转 print(res1) print(round(3.6)) #四舍五入 x="hello" y=[1,2,3] res=zip(x,y) #拉链原理,一一对应,没有的则不要 print(res) print(list(res)) m=__import__("time") m.sleep(3)
面向对象需要学习的:
object
classmethod
staticmethod
property
hasattr
getattr
setattr
delattr
isinstance
issubclass
需要掌握的
max min sorted # filter #过滤 # names = ['egon','liusir_dsb',"housir_dsb"] # # print([name for name in names if name.endswith('dsb')]) #res=filter(lambda name:name.endswith("dsb"),names) #print(res) #print(list(res)) # map #映射 # names = ['lxx', 'liusir', "housir"] # # # res = [name+"_sb" for name in names] # # print(res) # # res = map(lambda x: x + "_sb", names) # # print(res) # print(list(res)) # reduce #合并 from functools import reduce res = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5],100) res = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]) res = reduce(lambda x, y: x + y, ["a",'b','c']) print(res)
了解:
print(abs(-1)) # print(all([1, "aa", True])) # print(any([0,"",True])) # print(all("")) # True # print(any([])) # False # print(bin(11)) # print(oct(11)) # print(hex(11)) # print(callable(len)) # print(callable(10)) # aaaa = 1111111111 # def func(): # x = 111111111 # y = 2222222222222 # print(globals()) # print(locals()) # # func() # print(pow(10,2,3)) # 10 ** 2 % 3 # l = [1,2,3,4,5,6,7] # # s = slice(0,5,2) # print(l[s]) # # # l1 = [11,222,333,444,555,666,777,88,999] # print(l1[s])
三、面向过程编程:
面向过程的核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么
基于面向过程设计程序就好比在设计一条流水线,是一种机械式的思维方式
优点:复杂的问题流程化,进而简单化
缺点:可扩展性差,修改流水线的任意一个阶段,都会牵一发而动全身
应用:扩展性要求不高的场景,典型案例如linux内核,git,httpd