什么是编译型、解释型?有什么区别?
编译型:一次性将全部代码编译成二进制文件。如C、C++等语言。
优点:运行效率高
缺点:开发速度慢、不能跨平台
解释型:程序运行时从上至下一行一行的解释成二进制。
优点:开发速度快、效率高、可以跨平台
缺点:运行效率低
Python2和Python3的区别?
Python2源码重复率高、不规范,且Python崇尚简洁优美,因此进化为Python3,规范化
Python基础数据类型?
to do
逻辑运算:
优先级:() > not > and > or
# 优先级 () > not > and > or
print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
# 相当于print((2 > 1 and 1 < 4) or (2 < 3 and 9 > 6) or (2 < 4 and 3 < 2))
# 即 print( True or True or False),因此结果为True
print(3>4 or 4<3 and 1==1) # F
print(1 < 2 and 3 < 4 or 1>2 ) # T
print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # T
print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # F
print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F
当运算符两边为数字时,如 x or y ,x非0则返回x,否则返回y。当x and y 时与or相反 x为0返回x,否则返回y
print(0 and 2) # 0
print(1 and 2) # 2
print(0 or 1) # 1
print(1 or 2) # 1
print(1 > 2 and 3 or 4 and 3 < 2) # False
print(1 or 1 < 3 and 2) # 1
程序运行结果?
a, b = [1, 2]
结果:a = 1 b = 2
生成器:注意从生成器真正开始取值开始往回计算
以下打印输出:
def f():
for i in range(4):
yield i
g = f()
g1 = (i for i in g)
g2 = (i for i in g1)
# g、g1、g2都是生成器
# 第一个print相当于遍历g后转成list输出,因此输出[0, 1, 2, 3]
# 第二个print相当于遍历g1后转成list输出,但此时g1已经在第一个print里面全部释放,所以第二个print输出[]
# 相当于
'''
g = (i for i in range(4))
print(list(g)) # [0, 1, 2, 3]
print(list(g)) # []
'''
print(list(g1)) # [0, 1, 2, 3]
print(list(g2)) # []
以下输出:
def add(n, i):
return n + i
def test():
for i in range(4):
yield i
g = test()
for n in [1, 10]:
g = (add(n, i) for i in g)
print(list(g))
'''
只有当执行print(list(g))的时候,前面的所有代码才开始执行
g = test() # 此时如果遍历g得出[0, 1, 2, 3]
n = 1时
g = (add(n, i) for i in g)
n = 10时
g = (add(n, i) for i in g)
= (add(n, i) for i in (add(n, i) for i in g))
= (add(n, i) for i in (add(n, i) for i in [0, 1, 2, 3]))
= (add(10, i) for i in (add(10, i) for i in [0, 1, 2, 3]))
= (add(10, i) for i in [10, 11, 12, 13])
= [20, 21, 22, 23]
'''
匿名函数:
匿名函数通常结合内置函数
现有两元组(('a'), ('b')),( ('c'), ('d')),使用匿名函数生成列表[{'a': 'c'}, {'b': 'd'}]
考察点:匿名函数,内置函数map和zip
# 现有两个元组(('a'), ('b')), (('c'), ('d')),使用匿名函数生成列表[{'a': 'c'}, {'b': 'd'}]
res = list(map(lambda t:{t[0]:t[1]}, zip((('a'), ('b')), (('c'), ('d')))))
print(res) # [{'a': 'c'}, {'b': 'd'}]
以下输出?修改函数产生期望结果:todo,暂不太懂
def multipliers():
return [lambda x:i*x for i in range(4)]
print([m(3) for m in multipliers()]) # [9, 9, 9, 9]
========================================
def multipliers():
return (lambda x:i*x for i in range(4))
print([m(3) for m in multipliers()]) # [0, 3, 6, 9]