Python 常用技巧

if else 语句:

表达式 c = a if a > b else b:

1 >>>a = 1
2 >>>b = 2
3 >>>c = a if a > b else b
4 #2

二维列表:

c = [b,a][a>b]

1 >>>a = 1
2 >>>b = 2
3 >>>c = [b,a][a>b]
4 #2

c = (a > b and [a] or [b])[0]

1 >>>a = 1
2 >>>b = 2
3 >>>c = (a > b and [a] or [b])[0]
4 #2

 

输入n个数转换为list:

1 li = list(map(int, input().strip().split()))

 a//b

>>> 49//2
#24

 反转:str[::-1]

>>>a = '1234'
>>>a[::-1]
#4321

 

posted @ 2018-01-02 12:04  starry_sky  阅读(202)  评论(0编辑  收藏  举报