pythonnotin用法
㈠ python中如何使用not in
name=''while not name:
name=raw_input(u'請輸入姓名:')
print name
python中的not具體表示是什麼:
在python中not是邏輯判斷詞,內用於布爾型True和False,not True為False,not False為True,以下是幾個常用容的not的用法:
(1) not與邏輯判斷句if連用,代表not後面的表達式為False的時候,執行冒號後面的語句。比如:
a = False
if not a: (這里因為a是False,所以not a就是True)
print "hello"
這里就能夠輸出結果hello
(2) 判斷元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,這句話的意思是如果a不在列表b中,那麼就執行冒號後面的語句,比如:
a = 5
b = [1, 2, 3]
if a not in b:
print "hello"
這里也能夠輸出結果hello
㈡ Python中關系運算符in,not in在字元串表達式和列表的使用時有什麼區別和注意點
Membership test operations
For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expressionxinyis equivalent toany(xiseorx==eforeiny).
For the string and bytes types,xinyisTrueif and only ifxis a substring ofy. An equivalent test isy.find(x)!=-1. Empty strings are always considered to be a substring of any other string, so""in"abc"will returnTrue.
翻譯:
對容器類型,例如list、tuple、set、frozenset、dict或collections.deque,表達式x in y等價於any(x is e or x == e for e in y)。
對字元串和bytes類型,x in y為真當且僅當x是y的子串。等價測試為y.find(x) != -1。空字元串永遠被視作是其他任何字元串的子集,因此"" in "abc"將返回True。
㈢ python in和not in 在list中可以用嗎
自然是可以的.元組列表都是可以用的.
Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange
在上面這些類型裡面都是可以使用in/not in的.
㈣ python中的not具體表示是什麼,舉個例子說一下,衷心的感謝
在python中not是邏輯判斷詞,用於布爾型True和。
布爾"非" :如果 x 為 True,返回 False 。如果 x 為 False,它返回 True。 例如:
a = 0;
b = 1;
if not ( a and b ):
print "變數 a 和 b 都為 false,或其中一個變數為 false";
else:
print "變數 a 和 b 都為 true";
輸出結果為:變數 a 和 b 都為 false,或其中一個變數為 false。
(4)pythonnotin用法擴展閱讀
1、not 和 in 連接的用法:
not in ,如果在指定的序列中沒有找到值返回 True,否則返回 False。x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。例如:
b = 20;
list = [1, 2, 3, 4, 5 ];
if ( b not in list ):
print "變數 b 不在給定的列表中 list 中";
else:
print "變數 b 在給定的列表中 list 中";
2、is 和 not 連接的用法:
is not , 是判斷兩個標識符是不是引用自不同對象,x is not y, 類似id(a) != id(b)。如果引用的不是同一個對象則返回結果 True,否則返回 False。例如:
a = 20;
b = 30;
if ( a is not b ):
print "4 - a 和 b 沒有相同的標識";
else:
print "4 - a 和 b 有相同的標識";
㈤ 關於python中sin函數的用法
因為 pow 是內置的方法,而 sin 是 math 包提供的方法。
如果你自己定義了一個 pow 方法,就會覆蓋掉默認的 pow 方法,但是如果你 import 了兩個包,兩個裡面都有 sin 方法,那麼使用的時候如果不指定是哪個的話就無法准確執行了。同理,如果引用的是外部包的方法或者類的話,就一定要使用包名引用。如果每次打包名嫌麻煩的話,可以使用 import math as m 這樣的縮寫,下次就可以使用 m.sin() 了。
㈥ python3 把for循環轉換成while循環,不能用'in'關鍵字,不能用切片([i:]) 代碼在圖片里,self不要
下面的代碼可以實現獲取傳入字元串的最長不重復字元串
def get_longest_no_repeat_sub_str(s):
"""
循環給定的字元串的每個個字元,每個字元串分別向後截取所有不重復的子字元串,
將獲取到的所有子字元串放入列表,列表再按照每個子字元串的長度進行正向排序,
:return:最長不重復字元串
"""
tmp_list = []
length = len(s)
for i in range(length):
tmp = s[i]
for j in range(i + 1, length):
if s[j] not in tmp:
tmp += s[j]
else:
break
tmp_list.append(tmp)
tmp_list.sort(key=lambda x: len(x))
return tmp_list[-1]
ss = 'andafjojqoq'
string = get_longest_no_repeat_sub_str(ss)
print('{}最長非重復子串為:{}'.format(ss, string))
㈦ python中的if in是什麼意思
in後面是一字典,如果freebuf.com存在這個字典裡面,則執行。看這條語句的意思,一般都是用來獲取包含這個域名的所有URL。
㈧ python if not in 用 java 如何實現
是的:
array.contains();
㈨ Python :一個for循環無法實現,出現「x not in list」,菜鳥一個,請問大神怎麼回事
a=[1,3,3,4,5,6,3343,52]
ad=[]
importrandom
foriinrange(len(a)):#range(8),i=[0,1,2,3,4,5,6,7]
print(random.choice(a))
ad=a.remove(i)#wheni=2,inotina,Error!
print(ad)
print(a)
㈩ python if not in 多條件 判斷怎麼寫
s=['1','2']
sta='12345'
if all( t not in sta t for s):
print sta