涓€闃舵鈥斺€旂煡璇嗙偣鏁村悎

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓€闃舵鈥斺€旂煡璇嗙偣鏁村悎相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%a7%a3%e7%a0%81' title='瑙g爜'>瑙g爜   瀛愰泦   lap   remove   exp   杈撳叆   color   鍦板潃   dac   

1銆亀hile while...else 寰幆璇彞

鎶€鏈浘鐗? id=
number=0
sum=0
while number<=100:
    sum+=number
    number+=1
print(sum)#缁撴灉涓?050

#濡傛灉寰幆琚玝reak鎵撴柇锛屽氨涓嶄細鎵цelse锛屽弽涔嬫墽琛宔lse
count=0
while count<=5:
    count+=1
    if count==2:
        break
    print(鈥?/span>loop鈥?/span>,count)
else:
    print(鈥?/span>寰幆瀹屾垚鈥?/span>)

count=0
while count<=5:
    count+=1
    if count==2:
        pass
    print(鈥?/span>loop鈥?/span>,count)
else:
    print(鈥?/span>寰幆瀹屾垚鈥?/span>)
View Code

2銆乮f 鍒ゆ柇璇彞

鎶€鏈浘鐗? id=
#绗竴绉嶇敤娉?/span>
number=5
if number==5:
    print(鈥?/span>璇ユ暟瀛楃瓑浜?鈥?/span>)
else:
    print(鈥?/span>涓嶇瓑浜?鈥?/span>)

#绗簩绉嶇敤娉?/span>
number=5
if number==1:
    pass
elif number==5:
    print(鈥?/span>鐩哥瓑鈥?/span>)
else:
    print(涓嶇浉绛?

#绗笁绉嶇敤娉?/span>
number=5
if number==5:
    print(鈥?/span>璇ユ暟瀛楃瓑浜?鈥?/span>)
View Code

3銆佹暟鎹被鍨?int銆乻tr銆乥ool

鎶€鏈浘鐗? id=
#int锛氭暟瀛楃被鍨?/span>
i=2
b=4
print(i+b,i*b,i/b,i%b,i-b,i**b)#缁撴灉涓? 8 0.5 2 -2 16

#str锛氬瓧绗︿覆绫诲瀷 鍙互璺焛nt绫诲瀷鐩镐箻锛屼篃鍙互涓や釜瀛楃涓茬浉鍔?/span>
str1=鈥?/span>asd鈥?/span>
str2=鈥?/span>鍏?/span>鈥?/span>
print(str1+str2,str1*2)#缁撴灉涓篴sd鍏?asdasd

#bool True鍜孎alse
View Code

4銆乮nput 鐢ㄦ埛浜や簰

鎶€鏈浘鐗? id=
1 username=input(鈥?/span>璇疯緭鍏ユ偍鐨勮处鍙?gt;>>鈥?/span>)
2 password=input(鈥?/span>璇疯緭鍏ユ偍鐨勫瘑鐮?gt;>>鈥?/span>)
View Code

5銆?d锛堣緭鍑虹殑鏄暟瀛楃被鍨嬶級 %s锛堣緭鍑虹殑鏄瓧绗︿覆绫诲瀷锛?鏍煎紡鍖栬緭鍑?/p>

鎶€鏈浘鐗? id=
username=input(鈥?/span>璇疯緭鍏ユ偍鐨勮处鍙?gt;>>鈥?/span>)
password=input(鈥?/span>璇疯緭鍏ユ偍鐨勫瘑鐮?gt;>>鈥?/span>)
msg=鈥?/span>鎮ㄧ殑璐﹀彿鏄?s锛屾偍鐨勫瘑鐮佹槸%d鈥?/span>%(username,int(password))#int(password)鏄妸瀛楃涓茬被鍨嬪彉鎴愭暟瀛楃被鍨?/span>
print(msg)#缁撴灉涓? 鎮ㄧ殑璐﹀彿鏄痲we锛屾偍鐨勫瘑鐮佹槸123
View Code

6銆侀€昏緫杩愮畻

鎶€鏈浘鐗? id=
鈥樷€樷€?/span>
not锛歯ot False==Trun锛屽弽涔嬬瓑浜嶧alse
and锛氫竴鐪熶竴鍋囦负鍋囷紝涓や釜鐪熶负鐪燂紱涓や釜鍋囦负鍋?
or锛氫竴鐪熶竴鍋囦负鐪燂紱涓や釜鐪熶负鐪燂紱涓や釜鍋囦负鍋?
浼樺厛绾у叧绯伙細() > not > and > or
鈥樷€樷€?/span>
#6.1銆乤nd銆乷r銆乶ot杩愮畻绗﹁繍绠?/span>
print(not 2>1 and 3<2 or 4>1)#F and F or T -->F or T --> T

#6.2銆亁 or y 濡傛灉x涓篢rue锛屽垯杩斿洖x锛涘惁鍒欒繑鍥瀥锛泋 and y 濡傛灉x涓篢rue锛屽垯杩斿洖y锛涘惁鍒欒繑鍥瀤
print(1 or 2) #缁撴灉涓?
print(0 or 3) #缁撴灉涓?
print(1 and 2) #缁撴灉涓?
print(0 and 2) #缁撴灉涓?

#6.3銆佹贩鍚堣繍绠?/span>
print(0 or 1 and 3 or 4 and 5) #0 or 3 or 5 --> 3 or 5 -->3
print(1>2 and 3 or 4 and 3<2) #F and 3 or 4 and F --> F or F --> F
View Code

7銆佹暟鎹被鍨嬭浆鎹?/p>

鎶€鏈浘鐗? id=
#int --> bool 闈為浂杞崲鎴怲rue锛岄浂杞崲鎴怓alse
print(bool(1),bool(-1),bool(0))#True True False

#bool --> int 鍙湁1鍜?
print(int(True),int(False))#1 0

#str --> int 涓嶈兘杞崲甯﹀瓧姣嶅拰鐗规畩瀛楃鐨?/span>
s=鈥?/span>123e鈥?/span>
print(int(s))#鎶ラ敊
s=鈥?/span>123鈥?/span>
print(int(s),type(int(s)))#123

#int --> str
i=1
print(str(i),type(str(i)))

#str --> bool 绌哄瓧绗︿覆杞崲鎴怓alse锛岄潪绌哄瓧绗︿覆杞崲鎴怲rue
s=鈥樷€?/span>
s1=鈥?/span>as鈥?/span>
print(bool(s),bool(s1))
View Code

8銆乮nt鏁板瓧绫诲瀷鐨勬搷浣滄柟娉?/p>

鎶€鏈浘鐗? id=
#bit_length 璁$畻璇ユ暟瀛楃殑浜岃繘鍒剁殑鏈夋晥浣嶆暟
i=100
print(i. bit_length())#7
View Code

9銆佸瓧绗︿覆鐨勭储寮曞拰鍒囩墖 (PS锛氱储寮曚粠宸﹀線鍙虫槸浠?寮€濮?鍗筹細a鏄?锛涗粠鍙冲線宸︽槸浠?1寮€濮嬶紝鍗筹細g鏄?1锛涘垏鐗囷細椤惧ご涓嶉【灏?

鎶€鏈浘鐗? id=
s=鈥?/span>abcdefg鈥?/span>
print(s[0],s[-2])#a f
print(s[0:3],s[:],s[0:])#abc abcdefg abcdefg
print(s[0:5:2],s[-1::-1],s[::-1],s[-1:-6:-2])#ace gfedcba gfedcba gec
View Code

10銆佸瓧绗︿覆鐨勬搷浣滄柟娉?/p>

鎶€鏈浘鐗? id=
s=鈥?/span> xiao Le Xian Sheng 1asda23\ta鈥?/span>
print(s.capitalize())#棣栧瓧姣嶅ぇ鍐?/span>
print(s.upper())#瀛楁瘝鍏ㄩ儴澶у啓
print(s.lower())#瀛楁瘝鍏ㄩ儴灏忓啓
print(s.swapcase())#鍙嶈浆锛屽皬鍐欏瓧姣嶅彉澶у啓锛屽ぇ鍐欏瓧姣嶅彉灏忓啓
print(s.title())#鍙鐢ㄧ┖鏍兼垨鐗规畩瀛楃杩樻湁鏁板瓧闅斿紑鐨勫崟璇嶉兘棣栧瓧姣嶅ぇ鍐?/span>
print(s.center(32,鈥?/span>*鈥?/span>))#灞呬腑锛屼笉濉啓濉厖鐗╋紝榛樿涓虹┖鐧藉~鍏咃紝涔熷彲浠ヨ嚜宸辫瀹氬~鍏呯墿
print(s.expandtabs())#鍙瀛楃涓插嚭鐜?lsquo;\t’鐨勶紝鍦?lsquo;\t’鍓嶉潰瀛楃鑷姩琛ュ叏鍒?浣嶏紝瓒呰繃8浣嶇殑琛ュ叏鍒?6浣嶏紝瓒呰繃16浣嶇殑琛ュ叏鍒?2浣?/span>
print(s.startswith(鈥?/span>x鈥?/span>),s.startswith(鈥?/span>x鈥?/span>,1,5))#妫€鏌ヤ互浠€涔堝紑澶达紝杩斿洖True鍜孎alse锛岃繕鍙互鎸囧畾浣嶇疆妫€鏌?/span>
print(s.endswith(鈥?/span>a鈥?/span>),s.endswith(鈥?/span>3鈥?/span>,-3,0))#妫€鏌ヤ互浠€涔堢粨灏撅紝杩斿洖True鍜孎alse锛岃繕鍙互鎸囧畾浣嶇疆妫€鏌?/span>
print(s.find(鈥?/span>a鈥?/span>),s.find(鈥?/span>q鈥?/span>),s.find(鈥?/span>ao鈥?/span>),s.find(鈥?/span>a鈥?/span>,1,8))#閫氳繃鍏冪礌鎵剧储寮曪紝鎵句笉鍒拌繑鍥?1锛屽彲浠ュ涓厓绱犱竴璧锋壘锛屼絾鍙繑鍥炴渶鍓嶉潰閭d釜鍏冪礌鐨勭储寮?/span>
# print(s.index(鈥榓鈥?,s.index(鈥榪鈥?)#閫氳繃鍏冪礌鎵剧储寮曪紝浣嗘壘涓嶅埌浼氭姤閿?/span>
print(s.strip(),s.strip(鈥?/span>a鈥?/span>))#榛樿鍒犻櫎鍓嶅悗绌烘牸锛屽彲浠ユ坊鍔犲垹闄ょ殑鍏冪礌锛屼絾鍙細鍒犻櫎鍓嶅悗鐨勶紝閬囧埌瀛楃灏卞仠涓嬫潵
print(s.rstrip(鈥?/span>a鈥?/span>))#鍙垹闄ゅ悗闈㈢殑鎸囧畾鍏冪礌锛岄粯璁ゅ垹闄ょ┖鏍?/span>
print(s.lstrip())#鍙垹闄ゅ墠闈㈢殑鎸囧畾鍏冪礌锛岄粯璁ゅ垹闄ょ┖鏍?/span>
print(s.count(鈥?/span>x鈥?/span>),s.count(鈥?/span>q鈥?/span>))#缁熻涓暟,娌℃湁杩斿洖0
print(s.split(),s.split(鈥?/span>x鈥?/span>))#鎷嗗垎鍒嗗壊锛岄粯璁や负绌烘牸涓哄垎鍓茬鍙凤紝涔熷彲鎸囧畾鍏冪礌涓哄垎鍓茬鍙凤紙PS锛歴tr杞崲鎴恖ist鐨勬柟娉曪級
print(s.replace(鈥?/span>xiao Le鈥?/span>,鈥?/span>灏忔▊鈥?/span>),s.replace(鈥?/span>xiao Le鈥?/span>,鈥?/span>灏忔▊鈥?/span>,1))#鏇挎崲锛岄粯璁ゆ浛鎹㈠叏閮紝涔熷彲鎸囧畾娆℃暟
print(s.isalpha())#鍒ゆ柇瀛楃涓叉槸鍚︾敱瀛楁瘝缁勬垚
print(s.isdigit())#鍒ゆ柇瀛楃涓叉槸鍚︾敱鏁板瓧缁勬垚
print(s.isalnum())#鍒ゆ柇瀛楃涓叉槸鍚︾敱瀛楁瘝鍜屾暟瀛楃粍鎴?/span>
#for 寰幆杈撳嚭瀛楃涓插厓绱?/span>
for i in s:
    print(i)
View Code
鎶€鏈浘鐗? id=
#format涓夌鐢ㄦ硶
s=鈥?/span>鎴戝彨锛屽枩娆,鍐嶈涓€娆℃垜鍙珄鈥?/span>.format(鈥?/span>liuel鈥?/span>,鈥?/span>youxi鈥?/span>,鈥?/span>liule鈥?/span>)
print(s)
s1=鈥?/span>鎴戝彨0锛屽枩娆1,鍐嶈涓€娆℃垜鍙珄0鈥?/span>.format(鈥?/span>liuel鈥?/span>,鈥?/span>youxi鈥?/span>)
print(s1)
s3=鈥?/span>鎴戝彨name锛屽枩娆hobby,鍐嶈涓€娆℃垜鍙珄name鈥?/span>.format(hobby=鈥?/span>youxi鈥?/span>,name=鈥?/span>liule鈥?/span>)
print(s3)
View Code

11銆佸垪琛ㄧ殑绱㈠紩鍜屽垏鐗囷紙璺熷瓧绗︿覆绱㈠紩鍜屽垏鐗囦竴鏍凤級

鎶€鏈浘鐗? id=
li=[鈥?/span>alex鈥?/span>,[1,2,3],鈥?/span>user鈥?/span>,鈥?/span>age鈥?/span>,鈥?/span>鍏?/span>鈥?/span>,鈥?/span>abc鈥?/span>]
print(li[0],li[0:],li[0:5:2])
View Code

12銆佸垪琛ㄧ殑澧炲垹鏀规煡

鎶€鏈浘鐗? id=
#澧?/span>
li=[鈥?/span>alex鈥?/span>,[1,2,3],鈥?/span>user鈥?/span>,鈥?/span>age鈥?/span>,鈥?/span>鍏?/span>鈥?/span>,鈥?/span>abc鈥?/span>]
li.append(鈥?/span>hhh鈥?/span>)#灏嗘坊鍔犵殑鍏冪礌娣诲姞鍒版渶鍚庨潰
li.insert(2,鈥?/span>name鈥?/span>)#灏嗛渶瑕佹坊鍔犲埌瑙勫畾绱㈠紩鐨勫厓绱狅紝娣诲姞鍒拌瀹氱殑绱㈠紩
li.extend(鈥?/span>asd鈥?/span>)#杩唬澧炲姞鍒板垪琛ㄧ殑鍚庨潰锛屼笉鑳借凯浠nt绫诲瀷鐨勬暟鎹?/span>

#鍒?/span>
li=[鈥?/span>alex鈥?/span>,[1,2,3],鈥?/span>user鈥?/span>,鈥?/span>age鈥?/span>,鈥?/span>鍏?/span>鈥?/span>,鈥?/span>abc鈥?/span>]
li.pop(0)#鎸夌储寮曞垹闄ゅ厓绱狅紝鏈夎繑鍥炲€?/span>
li.remove(鈥?/span>user鈥?/span>)#鎸夊厓绱犲垹闄わ紝娌℃湁浼氳繑鍥炲€硷紝浼氳繑鍥炰竴涓狽one
del li[0:2]#鎸夊垏鐗囧幓鍒犻櫎锛屼絾椤惧ご涓嶉【灏撅紝姣斿锛氬垹闄ゅ埌绱㈠紩涓?鐨勫厓绱狅紝浠栧彧浼氬垹闄ょ储寮曚负0鍜?鐨勫厓绱?/span>
li.clear()#娓呯┖鍒楄〃閲岀殑鍏冪礌
del li#鍒犻櫎鏁翠釜鍒楄〃

#鏀?/span>
li=[鈥?/span>alex鈥?/span>,[1,2,3],鈥?/span>user鈥?/span>,鈥?/span>age鈥?/span>,鈥?/span>鍏?/span>鈥?/span>,鈥?/span>abc鈥?/span>]
li[0]=鈥?/span>name鈥?/span>#鎸夌储寮曞幓鏀?/span>
li[0:2]=鈥?/span>123鈥?/span>#鍒囩墖鍘绘敼锛屽厛鍒犻櫎绱㈠紩涓?鍜?鐨勫厓绱狅紝涓嶄細鍒犻櫎绱㈠紩涓?鐨勫厓绱狅紙椤惧ご涓嶉【灏撅級锛屽啀鎶婇渶瑕佹坊鍔犵殑鍏冪礌杩唬澧炲姞

#鏌?/span>
li=[鈥?/span>alex鈥?/span>,[1,2,3],鈥?/span>user鈥?/span>,鈥?/span>age鈥?/span>,鈥?/span>鍏?/span>鈥?/span>,鈥?/span>abc鈥?/span>]
print(li[0:2])#鍒囩墖鏌ョ湅锛岄【澶翠笉椤惧熬
#for寰幆鏌ョ湅
for i in li:
    print(i)
View Code

13銆佹帓搴?/p>

鎶€鏈浘鐗? id=
li=[1,3,5,7,8,2,4,6]
li.sort()#姝e悜鎺掑簭
li.sort(reverse=True)#鍙嶅悜鎺掑簭
li.reverse()#鍙嶈浆
print(li)
View Code

14銆佸垪琛ㄥ祵濂?/p>

鎶€鏈浘鐗? id=
li=[鈥?/span>alex鈥?/span>,[1,2,鈥?/span>name鈥?/span>],鈥?/span>user鈥?/span>,鈥?/span>age鈥?/span>,鈥?/span>鍏?/span>鈥?/span>,鈥?/span>abc鈥?/span>]
print(li[0][2])#鏌ユ壘绱㈠紩涓?鐨勫厓绱狅紝鏌ユ壘鍒颁箣鍚庯紝鏌ユ壘杩欎釜鍏冪礌绱㈠紩涓?鐨勫厓绱?/span>
print(li[1][2][2])
View Code

15銆佸厓绁?nbsp; 鍙兘鏌ワ紝涓嶈兘鏀?/p>

鎶€鏈浘鐗? id=
tu=(1,2,鈥?/span>alex鈥?/span>,[1,2,鈥?/span>liu鈥?/span>],3,鈥?/span>xiao鈥?/span>)
print(tu[0])#閫氳繃绱㈠紩鏌ユ壘鐪嬪厓绱?/span>
print(tu[0:2])#鍒囩墖鏌ョ湅锛岄【澶翠笉椤惧熬
#for寰幆杈撳嚭鍏冪
for i in tu:
    print(i)
View Code

16銆乯oin鍜宺ange鎿嶄綔鏂规硶

鎶€鏈浘鐗? id=
s=鈥?/span>alex鈥?/span>
li=[鈥?/span>ad鈥?/span>,鈥?/span>cd鈥?/span>]
print(鈥?/span>_鈥?/span>.join(s),鈥樷€?/span>.join(li),type(鈥樷€?/span>.join(li)))#浠ヨ嚜宸辫瀹氱殑绗﹀彿闅斿紑锛屼篃鏄垪琛ㄨ浆鎴愬瓧绗︿覆鐨勪竴绉嶆柟娉?/span>

for i in range(0,10):
    print(i)#姝e簭杈撳嚭1鍒?
for i in range(10,0,-1):
    print(i)#鍊掑簭杈撳嚭10鍒?
for i in range(0, 10, -1):
    print(i)  # 缁撴灉鏄┖鐧斤紝涓嶄細鎶ラ敊
View Code

17銆佸瓧鍏哥殑澧炲垹鏀规煡

鎶€鏈浘鐗? id=
#澧?/span>
dic1=
    鈥?/span>age鈥?/span>:18,
    鈥?/span>name鈥?/span>:鈥?/span>liu鈥?/span>,
    鈥?/span>sex鈥?/span>:鈥?/span>鐢?/span>鈥?/span>,

dic1[鈥?/span>hobby鈥?/span>]=鈥?/span>name鈥?/span>
dic1[鈥?/span>age鈥?/span>]=20#鏈夐敭鍊煎灏辫鐩栵紝娌℃湁灏卞鍔狅紝涔熷彲鐢ㄤ綔瀛楀吀鐨勪慨鏀?/span>
dic1.setdefault(鈥?/span>yundong鈥?/span>)
dic1.setdefault(鈥?/span>age鈥?/span>,鈥?/span>23鈥?/span>)#鏈夐敭鍊煎鐨勪笉鍋氫换浣曚慨鏀癸紝娌℃湁鐨勫氨澧炲姞锛屾病鏈夊€间細榛樿娣诲姞None
print(dic1)

#鍒?/span>
dic1=
    鈥?/span>age鈥?/span>:18,
    鈥?/span>name鈥?/span>:鈥?/span>liu鈥?/span>,
    鈥?/span>sex鈥?/span>:鈥?/span>鐢?/span>鈥?/span>,
    鈥?/span>hobby鈥?/span>:鈥?/span>youxi鈥?/span>,
    鈥?/span>user鈥?/span>:鈥?/span>xiaole鈥?/span>,
    鈥?/span>pass鈥?/span>:123

dic1.pop(鈥?/span>name鈥?/span>)#鎸夐敭鍘诲垹锛屾湁杩斿洖鍊?/span>
dic1.popitem()#闅忔満鍒犻櫎
del dic1[鈥?/span>age鈥?/span>]#鎸夐敭鍘诲垹锛屾病鏈夎繑鍥炲€?/span>
dic1.clear()#鍒犻櫎瀛楀吀閲屾墍鏈夊厓绱?/span>
del dic1#鍒犻櫎鏁翠釜瀛楀吀
print(dic1)

#鏀?/span>
dic1=
    鈥?/span>age鈥?/span>:18,
    鈥?/span>name鈥?/span>:鈥?/span>liu鈥?/span>,
    鈥?/span>sex鈥?/span>:鈥?/span>鐢?/span>鈥?/span>,

dic2=
    鈥?/span>age鈥?/span>:20,
    鈥?/span>hobby鈥?/span>:鈥?/span>youxi鈥?/span>

dic1.update(dic2)#鎶奷ic2鐨勯敭鍊煎瑕嗙洊鍒癲ic1鐨勯敭鍊煎鍘伙紝鏈夐敭鍊煎鐨勮鐩栵紝娌℃湁灏卞鍔?/span>
print(dic1,dic2)

#鏌?/span>
dic1=
    鈥?/span>age鈥?/span>:18,
    鈥?/span>name鈥?/span>:鈥?/span>liu鈥?/span>,
    鈥?/span>sex鈥?/span>:鈥?/span>鐢?/span>鈥?/span>,

print(dic1.keys(),dic1.values(),dic1.items())#渚濇鎵撳嵃閿€佸€煎拰閿€煎
print(dic1[鈥?/span>name鈥?/span>])#閫氳繃閿幓鏌ョ湅锛屾病鏈夎繖涓敭浼氭姤閿?/span>
print(dic1.get(鈥?/span>hobby鈥?/span>,None))#get涔熸槸閫氳繃閿幓鏌ョ湅锛屾病鏈夎繖涓敭鐨勪細杩斿洖浣犺缃繑鍥炲€?/span>
#for寰幆鏌ョ湅
for i in dic1.keys():
    print(i)
for i in dic1.values():
    print(i)
for x,y in dic1.items():
    print(x,y)
View Code

18銆侀泦鍚堢殑澧炲垹鏌?/p>

鎶€鏈浘鐗? id=
#澧?/span>
set=鈥?/span>alex鈥?/span>,鈥?/span>liu鈥?/span>,鈥?/span>xiao鈥?/span>,鈥?/span>age鈥?/span>
set.add(鈥?/span>name鈥?/span>)#闅忔満鍦ㄦ煇涓綅缃鍔?/span>
set.update(鈥?/span>pass鈥?/span>)#闅忔満鍦ㄨ凯浠e鍔?/span>
print(set)

#鍒?/span>
set1=鈥?/span>alex鈥?/span>,鈥?/span>liu鈥?/span>,鈥?/span>xiao鈥?/span>,鈥?/span>age鈥?/span>
set1.pop()#闅忔満鍒犻櫎
set1.remove(鈥?/span>alex鈥?/span>)#鎸囧畾鍏冪礌鍒犻櫎
set1.clear()#鍒犻櫎闆嗗悎閲屾墍鏈夊厓绱?/span>
del set1#鍒犻櫎鏁翠釜闆嗗悎
print(set1)

#鏌?/span>
set=鈥?/span>alex鈥?/span>,鈥?/span>liu鈥?/span>,鈥?/span>xiao鈥?/span>,鈥?/span>age鈥?/span>
for i in set:
    print(i)
View Code

19銆佷氦闆嗭紙&鍜宨ntersection锛夈€佸苟闆嗭紙|鍜寀nion锛夈€佸樊闆嗭紙-鍜宒ifference锛夈€佸弽浜ら泦锛坁鍜宻ymmetric_difference锛夈€佸瓙闆嗭紙<鍜宨ssubset锛夊拰瓒呴泦锛?gt;鍜宨ssuperset锛?/p>

鎶€鏈浘鐗? id=
set1=1,2,3,4,5
set2=4,5,6,7,8
print(set1.intersection(set2),set1 & set2)
print(set1.union(set2),set1 | set2)
print(set1.difference(set2),set1 - set2)
print(set1.symmetric_difference(set2),set1 ^ set2)
print(set1.issubset(set2),set1 < set2)
print(set2.issuperset(set1),set2 > set1)
View Code

20銆佹繁娴卌opy

鎶€鏈浘鐗? id=
#娴卌opy(copy鍜?)  鎷疯礉鍑烘潵鐨勫垪琛ㄧ涓€灞備笌鍘熸潵鏄笉鍚岀殑鍐呭瓨鍦板潃,绗簩灞傛寚鍚戝師鏉ョ殑鍦板潃
l1=[1,2,3,[鈥?/span>a鈥?/span>,鈥?/span>b鈥?/span>,1],鈥?/span>c鈥?/span>]
l2=l1.copy()
l1[0]=5
l1[3][0]=鈥?/span>s鈥?/span>
print(l1,l2)

#娣眂opy  鎷疯礉鎵€鏈夊唴瀹? 鍖呮嫭鍐呴儴鐨勬墍鏈?褰㈡垚涓€涓柊鐨勫璞?铏界劧涓庝箣鍓嶇殑鍊煎拰鍐呭涓€妯′竴鏍?浣嗘槸瀹冧滑瀹屽畬鍏ㄥ叏鐨勪袱涓璞?/span>
import copy
l1=[1,2,3,[鈥?/span>a鈥?/span>,鈥?/span>b鈥?/span>,1],鈥?/span>c鈥?/span>]
l2=copy.deepcopy(l1)
l1[0]=5
l1[3][0]=鈥?/span>s鈥?/span>
print(l1,l2)
View Code

21銆佺紪鐮佸拰瑙g爜

鎶€鏈浘鐗? id=
#str --->byte  encode 缂栫爜
s = 鈥?/span>浜屽摜鈥?/span>
b = s.encode(鈥?/span>utf-8鈥?/span>)
print(b)

#byte --->str decode 瑙g爜
s1 = b.decode(鈥?/span>utf-8鈥?/span>)
print(s1)
View Code

22銆佹枃浠舵搷浣?/p>

鎶€鏈浘鐗? id=
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)
print(x.read())
x.close()
y=open(鈥?/span>鏌愮洏:\鏂囦欢鍚?鍚庣紑鈥?/span>,mode=鈥?/span>r鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)
print(y.read())
y.close()
鐩稿璺緞鍜岀粷瀵硅矾寰勮鍙栨枃浠?/span>
鎶€鏈浘鐗? id=
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)
print(x.read())
x.close()
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>rb鈥?/span>)
print(x.read())
x.close()
鍙 r rb
鎶€鏈浘鐗? id=
#濡傛灉娌℃湁杩欎釜鏂囦欢灏卞垱寤猴紝鏈夎繖涓枃浠跺氨瑕嗙洊閲岄潰鐨勫唴瀹?/span>
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>w鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)
x.write(鈥?/span>hhhh鈥?/span>)
x.close()
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r鈥?/span>)
x.write(鈥?/span>xxxxx鈥?/span>.encode(鈥?/span>utf-8鈥?/span>))
x.close()
鍙啓 w wb
鎶€鏈浘鐗? id=
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>a鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)
x.write(鈥?/span>hhhh鈥?/span>)
x.close()
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>ab鈥?/span>)
x.write(鈥?/span>xxxxx鈥?/span>.encode(鈥?/span>utf-8鈥?/span>))
x.close()
杩藉姞 a ab
鎶€鏈浘鐗? id=
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r+鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)#鍏堣鍙栨枃浠讹紝鍦ㄦ坊鍔?/span>
print(x.read())
x.write(鈥?/span>hhhh鈥?/span>)
x.close()

x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r+鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)#鍏堟坊鍔犲唴瀹癸紝浼氳鐩栧師鏈夌殑涓€浜涘唴瀹癸紝鍦ㄨ鍙栦负瑕嗙洊鐨勫唴瀹?/span>
x.write(鈥?/span>hhhh鈥?/span>)
print(x.read())
x.close()

x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r+b鈥?/span>)#璇诲彇鏄痓ytes绫诲瀷
print(x.read())
x.write(鈥?/span>hhhh鈥?/span>.encode(鈥?/span>utf-8鈥?/span>))
x.close()
璇诲啓 r+ r+b
鎶€鏈浘鐗? id=
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>w+鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)
x.write(鈥?/span>hhhh鈥?/span>)
x.seek(0)
print(x.read())
x.close()

x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>w+b鈥?/span>)
x.write(鈥?/span>hhhh鈥?/span>.encode(鈥?/span>utf-8鈥?/span>))
x.seek(0)
print(x.read())
x.close()
鍐欒 w+ w+b

23銆佹枃浠舵搷浣滅殑鎿嶄綔鏂规硶

鎶€鏈浘鐗? id=
x=open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>)
print(x.read(3))#鎸夊瓧绗﹀畾鍏夋爣锛岀劧鍚庤緭鍑哄瓧绗?/span>
x.seek(0)#鎸夊瓧鑺傚畾鍏夋爣浣嶇疆
print(x.tell())#妫€娴嬪厜鏍囦綅缃?/span>
print(x.readable())#妫€娴嬫槸鍚﹀彲璇?/span>
print(x.readline())#涓€琛屼竴琛岀殑璇?/span>
print(x.readlines())#鎶婃瘡涓€琛岄兘褰撴垚鍏冪礌锛屾坊鍔犲埌鍒楄〃
#x.truncate(2)#鎴彇锛岃鍦╮+妯″紡鎵嶈兘鐢?/span>
x.close()

#鍚屾椂鎵撳紑澶氫釜鏂囦欢锛岃繖涓篃鍙互涓嶇敤close鍏抽棴鏂囦欢鐨勫懡浠?/span>
with open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>) as x,        open(鈥?/span>鏂囦欢鐨勫悕瀛?/span>鈥?/span>,mode=鈥?/span>r鈥?/span>,encoding=鈥?/span>utf-8鈥?/span>) as y:
    print(x.read())
    print(y.read())
View Code

 

以上是关于涓€闃舵鈥斺€旂煡璇嗙偣鏁村悎的主要内容,如果未能解决你的问题,请参考以下文章

redis鐨勫嚑涓煡璇嗙偣

鏁版嵁搴撶煡璇嗙偣

鍏充簬姊害鐨勪竴浜涚煡璇嗙偣

Kafka鈥斺€擲pringBoot鏁村悎锛堟秷璐硅€咃級

ksar銆乻ar鍙婄浉鍏冲唴鏍哥煡璇嗙偣瑙f瀽

html5 瀛︿範鎬荤粨