文章目錄
-
-
- 一、for循環
- 二、數字類型
- 三、字元串類型str
-
一、for循環
while循環 vs for 循環
while循環:稱之為條件循環,循環的次數取決于條件何時為False
for循環:稱之為疊代循環,循環的次數取決于資料的包含的元素的個數
for循環專用來取值,在循環取值方面比while循環要強大,以後但凡遇到循環取值的場景,就應該用for循環
l=['a','b','c']
for i in range(3):
print(i,l[i])
#for+break
names=['egon','kevin','alex','hulaoshi']
for name in names:
if name == 'alex':break
print(name)
#for+continue
names=['egon','kevin','alex','hulaoshi']
for name in names:
if name == 'alex':continue
print(name)
#for+else
names=['egon','kevin','alex','hulaoshi']
for name in names:
print(name)
else:
print('=====>')
#for循環嵌套
for i in range(3):
for j in range(2):
print(i,j)
"""
外層循環第一次:i=0
内層循環
0,0
0,1
外層循環第二次:i=1
内層循環
1,0
1,1
外層循環第三次:i=2
内層循環
2,0
2,1
"""
二、數字類型
-
整型int
用途:記錄年齡、等級、号碼等
定義方式:
類型轉換
了解print(int(3.1)) #結果3 res = (int('11')) print(res,type(res)) #結果11,類型int res = (float('11.1')) print(res,type(res)) #結果11.1,類型float
存一個值,不可變print(bin(12)) #10進制轉2進制 print(oct(12)) #10進制轉8進制 print(hex(12)) #10進制轉16進制
#不可變 x = 10 print(id(x)) #id為1654681344 x = 11 print(id(x)) #id為1654681376
-
浮點型float
用途:記錄身高、體重、薪資等
定義方式
類型轉換
存一個值,不可變print(float(10)) #結果為10.0 print(float(1.1)) #結果為1.1 print(float('1.1')) #結果為1.1
#不可變 x = 10.3 print(id(x)) #id為2114155192440 x = 11.2 print(id(x)) #id為2114155192512
三、字元串類型str
- 用途:記錄描述型值的狀态,比如名字、性别等
-
定義方式
類型轉換:可以把任意類型轉成字元串類型
res1 = str(10) res2 = str(10.3) res3 = str([1, 2, 3]) res4 = str({'x': 1}) print(type(res1)) print(type(res2)) print(type(res3)) print(type(res4))
-
常用操作+内置方法
優先掌握的操作
- 按索引取值(正向取+反向取) 隻能取
msg = 'hello world' print(msg[0]) #h print(msg[-1]) #d
- 切片(顧頭不顧尾,步長)
msg = 'hello world' print(msg[0:5]) #hello print(msg[0:5:2]) #hlo print(msg[0:]) #hello world print(msg[:]) #hello world print(msg[-1:-5:-1]) #dlro print(msg[::-1]) #dlrow olleh
- 長度len:統計的是字元的個數
msg = 'hello world' print(len(msg)) #11
- 成員運算in和not in:判斷一個子字元串是否存在與一個大字元串中
msg = 'hello world' print('ho' in msg) #False print('ho' not in msg) #True
- 移除空白strip:移除字元串左右兩邊的某些字元串
msg = ' hello ' print(msg.strip()) msg = '***h**ello**********' print(msg.strip('*')) #h**ello msg = '*-=+h/ello*(_+__' print(msg.strip('*-=+/(_')) #h/ello #示範 name = input('name>>>: ').strip() pwd = input('password>>>: ').strip() if name == 'egon' and pwd == '123': print('login successfull') else: print('username or password error')
- 切片split:把有規律的字元串切成清單進而友善取值
info = 'egon:18:180:150' res = info.split(':', 1) #1代表切分機次 print(res) #['egon', '18:180:150'] print(res[1]) #18:180:150 info = 'egon:18:180:150' res = info.split(':') print(res) #['egon', '18', '180', '150'] info = 'egon:18:180:150' res = info.split(':') s1='' for item in res: s1+=item print(s1) #egon18180150 info = 'egon:18:180:150' res = info.split(':') s1 = ':'.join(res) print(s1) # egon:18:180:150
- 循環
for i in 'hello': print(i)
- 按索引取值(正向取+反向取) 隻能取
需要掌握的操作
- strip,lstrip,rstrip
msg = '*****hello****' print(msg.strip('*')) #hello print(msg.lstrip('*')) #hello**** print(msg.rstrip('*')) #*****hello
- lower,upper
msg = 'AaBbCc123123123' print(msg.lower()) #aabbcc123123123 print(msg.upper()) #AABBCC123123123
- startswith,endswith
msg = 'alex is dsb' print(msg.startswith('alex')) #True print(msg.endswith('sb')) #True
- format的三種玩法
msg = 'my name is {name} my age is {age}'.format(age=18, name='egon') print(msg) #my name is egon my age is 18 msg = 'my name is {} my age is {}'.format(18, 'egon') print(msg) #my name is 18 my age is egon msg = 'my name is {0}{0} my age is {1}{1}{1}'.format(18, 'egon') print(msg) #my name is 1818 my age is egonegonegon
- split,rsplit
cmd = 'get|a.txt|33333' print(cmd.split('|', 1)) #['get', 'a.txt|33333'] print(cmd.rsplit('|', 1)) #['get|a.txt', '33333']
- replace
msg = 'kevin is sb kevin kevin' print(msg.replace('kevin', 'sb', 2)) #sb is sb sb kevin
- isdigit #當字元串内為純數字時結果為True
了解res = '11111' print(res.isdigit()) int(res) #True #示範 age_of_bk = 18 inp_age = input('your age: ').strip() if inp_age.isdigit(): inp_age = int(inp_age) if inp_age > 18: print('too big') elif inp_age < 18: print('to small') else: print('you got it') else: print('必須輸入純數字')
- find,rfind,index,rindex,count
print('xxxkevin is sb kevin'.find('kevin')) #3 print('xxxkevin is sb kevin'.index('kevin')) #3 print('xxxkevin is sb kevin'.rfind('kevin')) #15 print('xxxkevin is sb kevin'.rindex('kevin')) #15 res = 'xxxkevin is sb kevin'.find('kevasdfsadfin') print(res) #-1 表示沒找到 res = 'xxxkevin is sb kevin'.index('kevasdfsadfin') print(res) #沒找到會報錯 print('kevin is kevin is kevin is sb'.count('kevin')) #3次
- center,ljust,rjust,zfill
print('egon'.center(10,'*')) #***egon*** print('egon'.ljust(10,'*')) #egon****** print('egon'.rjust(10,'*')) #******egon print('egon'.zfill(10)) #000000egon
- captalize,swapcase,title
print('my name is kevin'.capitalize()) #My name is kevin print('AaBbCc'.swapcase()) #aAbBcC print('my name is kevin'.title()) #My Name Is Kevin
- is其他
name = 'egon123' print(name.isalnum()) # True 字元串由字母或數字組成 print(name.isalpha()) # False 字元串隻由字母組成 print(name.islower()) #True print(name.isupper()) #False name = ' ' print(name.isspace()) #True msg = 'I Am Egon' print(msg.istitle()) #True
- find,rfind,index,rindex,count