一、可變不可變型別
1、可變型別:值改變,但是id不變,證明就是在改變原值,是可變型別
2、不可變型別:值改變,id也變,證明是產生了新值,并沒有改變原值,原值是不可變型別
#數字 x = 123 print(id(x))#8791380317664 x = 456 print(id(x))#31218128 #串列 l1=[111,222,333] print(id(l1))#31582720 l1[0] = 1111111111111111 print(l1)#[1111111111111111, 222, 333] print(id(l1))#31582720
ps:python是一門解釋型 強型別 動態語言
二、數字型別及其常用操作
整型int
1、用途:年齡、個數、號碼、出生年等
2、定義方式
age = 18 # age = int(18)
# int功能可以把純整數數字的字串轉換成int型別,float不行,會直接報錯,
res = int("18")#<class 'int'> # res = int("1.8")#float型別會報錯 print(type(res))
了解(***)
#進制轉換 print(bin(11)) # 0b1011 print(oct(11)) # 0o13 print(hex(11)) # 0xb
3、常用操作+內置的方法
算數運算子與比較運算
浮點型float
1、用途:薪資、身高、體重等
2、定義方式
salary = 3.1 # salary = float(3.1)
# float功能可以把浮點陣列成的字串轉換成float型別
res = float("1.8") print(type(res))#<class 'float'>
3、常用操作+內置的方法
算數運算子與比較運算
總結:
數字型別都是只能存一個值,是不可變型別
無常用操作,無內置的方法
三、字串型別及其常用操作
1、用途:記錄描述性質的狀態,例如名字、性別、國籍等
2、定義方式:在引號('',"",'''''',""""""")內包含一串字串
# str功能可以把任意型別轉換成str型別
res=str([1,2,3]) print(res,type(res))# "[1,2,3]" <class 'str'>
3、常用操作+內置的方法
=========優先掌握的操作=========
1、按索引取值(正向取+反向取) :只能取
s = "hello world" print(s[0],type(s[0])) # "h" <class 'str'> print(s[-1])#'d' # s[1] = "E" # 報錯,不能修改 # s[2222]# 非法操作 # s[11] = "A" #超出索引,#非法操作
2、切片(顧頭不顧尾,步長)=>屬于拷貝操作
#可以省略步長,默認為1
#可以省略起始位置與步長,默認起始位置為0 步長為1
s = "hello world" # new_s=s[1:7] # print(new_s) # print(s) # new_s=s[1:7:2] #1 3 5 # print(new_s) # print(s) # new_s=s[:7:2] # new_s=s[::2] # 0 2 4 6 8 10 # h l o w r d # print(new_s) # new_s=s[::] # 完整拷貝字串,只留一個冒號就可以new_s=s[:] # print(new_s)
3、長度len
s = "hello world" print(len(s)) res=print("sfd") print(res)
4、成員運算in和not in
s = "hello world" print("hel" in s) print("egon" not in s) # 語意明確,推薦使用 # print(not "egon" in s)
5、移除左右兩邊空白(\n \t也算空白)strip 移除左右兩邊特定的字符,空白也是字符
s = " \n hel lo \t " new_s = s.strip() print(new_s) print(s) # 沒有改變原字串
# 應用案列: # name = input("your name>>> ").strip() # name = "egon " # pwd = input("your pwd>>> ").strip()
去除左右兩邊的非空白字符strip
print("**+=-%^#*$^&@!***he**llo**%^#**+=**".strip("*+=-%^$^@!&#"))
6、切分split:把字串按照某個分隔符切成一個串列
userinfo = "egon_dsb:123:18:3.1" res = userinfo.split(":") print(res)#['egon_dsb', '123', '18', '3.1'] 注意得到的是串列 print(res[0])#egon_dsb
# 純字串組成的串列
l = ["aaaa", "bbb", "ccc"] # res=l[0]+":"+l[1]+":"+l[2]#原始方式,利用字串相加 res = ":".join(l) #join方式 print(res, type(res)) #aaaa:bbb:ccc <class 'str'>
7、join 連接的元素必須是字串
userinfo = "egon_dsb:123:18:3.1"#字串 print("-".join(userinfo))#e-g-o-n-_-d-s-b-:-1-2-3-:-1-8-:-3-.-1 userinfo = "egon_dsb:123:18:3.1" res = userinfo.split(":")#得到串列['egon_dsb', '123', '18', '3.1'] print("-".join(res)) #egon_dsb-123-18-3.1
7、回圈
for i in "hello": print(i)
==========需要掌握的操作=========
1、strip,lstrip,rstrip
print("***hello***".strip("*")) print("***hello***".lstrip("*")) print("***hello***".rstrip("*"))
2、lower,upper
msg = "AbCDEFGhigklmn" res1 = msg.lower() res2 = msg.upper() print(res1) print(res2)
3、swapcase大小寫反轉
msg = "AbCDEFGhigklmn" res3=msg.swapcase() print(res3)
4、startswith,endswith
msg = "sb is lxx sb" print(msg.startswith("sb"))#True print(msg.endswith("b"))#True print(msg.endswith("c"))#False
5、split,rsplit
userinfo="egon:123:18" print(userinfo.split(":"))#['egon', '123', '18'] print(userinfo.split(":",1))#['egon', '123:18'] print(userinfo.rsplit(":",1))#['egon:123', '18']
6、replace replace('原值','現值')
msg = "***egon hello***" res=msg.replace('*','').replace(' ','') res1=msg.strip('*').replace(' ','') print(res) print(res1)
print(msg)#不變
7、format的三種玩法
(1)%s的方式
(2)format的方式
(3)f'' (不要亂用python3.6開始才有此功能)
#(1)%s的方式 name = "egon" age = 18 res1="my name is %s my age is %s" % (name,age) print(res1) #(2)format的方式 name = "egon" age = 18 res1="my name is {} my age is {}".format(name,age) res2="{0}{0}{0}{1}".format(name,age)#egonegonegon18 res3="my name is {name} my age is {age}".format(age=18,name="egon") print(res1) print(res2) print(res3) #(3)f'' name = "egon" age = 18 res1 = f'my name is {name} my age is {age}' print(res1)
了解:f搭配{}可以執行字串中的代碼
res=f'{len("hello")}' print(res) f'{print("hello")}'
f包含的字串可以放到多行
name = "egon" age = 18 res1 = f"my name is {name} " \ f"my age is {age}" print(res1)
# {}內不能有\以及# 外層{}有取消掉內層{}的意思
print(f'my name is {{egon}}')#{}類似于\ print('勝率是 %s%%' %70)
8、isdigit:判斷字串是否由純數字組成,只能判斷整形
print("adsf123".isdigit())#False print("123".isdigit())#True print("12.3".isdigit())#False
age = input('>>>: ') # age = " 18 " if age.isdigit():#可先判斷是否為數字,減少程式bug age=int(age) if age > 18: print('猜大了') elif age < 18: print('猜小了') else: print('猜對了') else: print('必須輸入數字,小垃圾')
==========需要了解的操作=========
1、find,rfind,index,rindex,count
總結:find 查找找到所要查找的位置的索引值,結果為-1代表沒找到;
rfind從右往左開始查找找到所要查找的索引的位置
index查找找到所要查找的位置的索引值,找不到報錯
rindex從右往左查找,找不到報錯
count計數
x = "hello egon egon egon" # res=x.find("egon") #代表從索引6開始 # res=x.find("egon123") # -1代表沒有找到 # print(res) res=x.rfind("egon") print(res) res=x.find("egon",0,3)#從索引0 到3里查找 print(res) #index # res = x.index("egon123") # 找不到則報錯 res = x.index("egon") # 代表從索引6開始 print(res) #count a = "hello,world!" d = {} for i in a: d[i] = a.count(i) print (d)#{'h': 1, 'e': 1, 'l': 3, 'o': 2, ',': 1, 'w': 1, 'r': 1, 'd': 1, '!': 1}
2、center,ljust,rjust,zfill
總結:center居中;
ljust居左;
rjust居右;
zfill居右,用0補全,默認右對齊
x = "egon" print(x.center(50,'*'))#居中,共50的字符,除了x外其余用*填滿 print(x.ljust(50,"*"))#居右,共50的字符,除了x外其余用*填滿 print(x.rjust(50,"*"))#居左,共50的字符,除了x外其余用*填滿 print(x.zfill(50))#居右,共50的字符,除了x外其余默認用0填滿 print(x.rjust(50,"0"))#居右,共50的字符,除了x外其余用0填滿
3、expandtabs展開選項卡----指定\t制表符的長度
expandtabs() 方法把字串中的 tab 符號('\t')轉為空格,tab 符號('\t')默認的空格數是 8,
從頭開始數,數到第一個\t正好為8個空格,不足則補空格,如果還有\t,接著從第一個\t數到第二個\t仍然為8個空格,以此類推直到最后一個\t結束,
print("hello\tworld".expandtabs(1)) #hello world 長度為11 print("hello\tworld".expandtabs(2)) #hello world長度為11 print("hello\tworld".expandtabs(3)) #hello world長度為11 print("hello\tworld".expandtabs(4)) #hello world長度為13 print("hello\tworld".expandtabs(5)) #hello world長度為15 print("hello\tworld".expandtabs(6)) #hello world長度為11
4、captalize,swapcase,title
總結:captalize首字母大寫
swapcase大小寫翻轉
title每個單詞首字母大寫 利用IDE開發工具,自動提示的功能
print("hello world egon".capitalize())#首字母大寫 print("aBcDeF".swapcase())#大小寫翻轉 print("hello world egon".title())#每個單詞首字母大寫
5、is其他
name='egon123' print(name.isalnum()) #字串由字母或數字組成 print(name.isalpha()) #字串只由字母組成 name="aaainputbbbbb" print(name.isidentifier())# name="abc123" print(name.islower())#是否都是大寫 print(name.isupper())#是否都是小寫 name=" " print(name.isspace())#字串是否為空 name="My Name Is Egon" print(name.istitle())#字串每個單詞首字母為大寫
6、is數字系列
#在python3中 num1=b'4' #bytes num2=u'4' #unicode,python3中無需加u就是unicode num3='四' #中文數字 num4='Ⅳ' #羅馬數字
#1、 bytes、unicode # print(num1.isdigit()) # print(num2.isdigit()) # print(num3.isdigit()) # print(num4.isdigit()) # 2、unicode、中文數字、羅馬數字 # print(num2.isnumeric()) # print(num3.isnumeric()) # print(num4.isnumeric()) # 3、unicode # print(num2.isdecimal()) # print(num3.isdecimal()) # print(num4.isdecimal())
str型別總結:
存一個值,有序,不可變
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/89931.html
標籤:Python
上一篇:頁眉頁腳的設定
