博客
关于我
Python十大装腔语法
阅读量:110 次
发布时间:2019-02-26

本文共 2366 字,大约阅读时间需要 7 分钟。

Python ??????????B??

Python ????????????????????????????????Python ????????????????????????????????????????? Python ?????????????????????

1. for-else????????

??? Python ????????????? else ?? if ????????else ???? for??????for-else ????????????????????

for i in [1,2,3,4]:
print(i, "??else")
else:
print("??else")

????? 1 ??else 2 ??else 3 ??else 4 ??else ??else

????????????????????????break?????else???

2. ???(*)????(**)

???????? Python ???????? (*) ????????????? (**) ????????????????

def multi_sum(*args):
s = 0
for item in args:
s += item
return s
multi_sum(3,4,5) # ??12

???????????????????????????????????????

def do_something(name, age, gender='?', *args, **kwds):
print("??? %s????%d????%s" % (name, age, gender))
print(args)
print(kwds)

3. ?????????????????

?????? Python ???????????????????????expression if condition else expression????

y = 5
print("y?????" if y < 0 else "y??????")

?????y??????

??????????????????????????

4. with - as??????????

with ??????????????????? with ??????????????????????

with open(r"D:\CSDN\Column\temp\mpmap.py", 'r') as fp:
contents = fp.readlines()

???????? try-finally ??????????????

5. ?????????????

?????????????????????????????????????????????

a = [1,2,3,4,5]
result = [i*i for i in a]

???????? for ????????????????

6. ???????????

Python ????????????????????????????

a = [0,1,2,3,4,5]
a[2:4] # [2,3]
a[3:] # [3,4,5]
a[1:] # [1,2,3,4,5]
a[:] # [0,1,2,3,4,5]

??????????????????????????????

7. lambda ??????????

lambda ??????????????????????

lambda x, y: x + y

???????????????????????????????

8. yield ?????????

yield ???????????????????????????????????

def get_square(n):
for i in range(n):
yield pow(i,2)

???????????????????????????

9. ?????????????

????????????????????????????????????

import time
def timer(func):
def wrapper(*args, **kwds):
t0 = time.time()
func(*args, **kwds)
t1 = time.time()
print('??%0.3f' % (t1-t0,))
return wrapper

???????????????????????????

10. ?? assert????????

?? assert ??????????????????

def i_want_to_sleep(delay):
assert isinstance(delay, (int, float)), '?????????????'
print('????')
time.sleep(delay)
print('???')

?????????????????????????????

??

Python ?????????????????????????????????????????????????????????????????????????? Python ??????????????????????????

转载地址:http://qesy.baihongyu.com/

你可能感兴趣的文章
Objective-C实现基于模板的双向链表(附完整源码)
查看>>
Objective-C实现基本二叉树算法(附完整源码)
查看>>
Objective-C实现备忘录模式(附完整源码)
查看>>
Objective-C实现复制粘贴文本功能(附完整源码)
查看>>
Objective-C实现复数类+-x%(附完整源码)
查看>>
Objective-C实现多组输入(附完整源码)
查看>>
Objective-C实现大根堆(附完整源码)
查看>>
Objective-C实现子集总和算法(附完整源码)
查看>>
Objective-C实现字符串boyer moore search博耶摩尔搜索算法(附完整源码)
查看>>
Objective-C实现字符串IP地址转DWORD地址(附完整源码)
查看>>
Objective-C实现字符串jaro winkler算法(附完整源码)
查看>>
Objective-C实现字符串manacher马拉车算法(附完整源码)
查看>>
Objective-C实现字符串wildcard pattern matching通配符模式匹配算法(附完整源码)
查看>>
Objective-C实现字符串word patterns单词模式算法(附完整源码)
查看>>
Objective-C实现字符串Z 函数或 Z 算法(附完整源码)
查看>>
Objective-C实现字符串加解密(附完整源码)
查看>>
Objective-C实现字符串复制功能(附完整源码)
查看>>
Objective-C实现完整的ComplexNumber复数类(附完整源码)
查看>>
Objective-C实现将位转换为浮点数bitsToFloat算法(附完整源码)
查看>>
Objective-C实现将字节数组转换为 base64 编码算法(附完整源码)
查看>>