python里面的几个用法,not in,c if a else b,[fun(a) for a in [...]] , a,b=b,a,'内容'.join([string array])

1.not in

>>> a=2

>>> a not in [2,3,4]
False
>>> a in [2,3,4] 

2. c if a else b   #这里注意,一定要有b,而且b不能为pass

>>> a=3 if 2>3 else 4
>>> a
4
>>> a=3 if 2<3 else 4 
>>> a


3.[fun(a) for a in [...]] 

>>> [a+1 for a in [2,3,4,5,6]]
[3, 4, 5, 6, 7]

4.a,b=b,a

>>> a=1
>>> b=2
>>> a,b=b,a
>>> a
2
>>> b
1

5.'内容'.join([string array])

>>> '.'.join[2,3,4,5,6]  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
>>> '.'.join([2,3,4,5,6]) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> '.'.join(['2','3','4','5','6']) 
'2.3.4.5.6'
 

猜你喜欢

转载自blog.csdn.net/abcx3261/article/details/84871359
今日推荐