OldBoy_Python第二天學習記錄

來源:互聯網
上載者:User

標籤:對象   count   blog   nic   style   isl   其他   列表   code   

一、基本數字類型

  數字(整形,長整形,浮點型,複數)

  字串

  位元組串:在介紹字元編碼時介紹位元組bytes類型

  列表

  元組

  字典

  集合

二、數字

整型與浮點型

#整型int  作用:年紀,等級,社會安全號碼,qq號等整型數字相關  定義:    age=10 #本質age=int(10)#浮點型float  作用:薪資,身高,體重,體質參數等浮點數相關    salary=3000.3 #本質salary=float(3000.3)#二進位,十進位,八進位,十六進位

其他數字類型(瞭解)

#長整形(瞭解)    在python2中(python3中沒有長整形的概念):          >>> num=2L    >>> type(num)    <type ‘long‘>#複數(瞭解)      >>> x=1-2j    >>> x.real    1.0    >>> x.imag    -2.0

三、字串

#作用:名字,性別,國籍,地址等描述資訊#定義:在單引號\雙引號\三引號內,由一串字元組成name=‘egon‘#優先掌握的操作:按索引取值(正向取+反向取) :只能取切片(顧頭不顧尾,步長)長度len成員運算in和not in移除空白strip切分split迴圈

其他動作(包括常用)

#stripname=‘*egon**‘print(name.strip(‘*‘))print(name.lstrip(‘*‘))#移除左變的指定字元print(name.rstrip(‘*‘))#移除右邊的指定字元#startswith,endswithname=‘alex_SB‘print(name.endswith(‘SB‘))#以某個字元結尾print(name.startswith(‘alex‘))#以某個字元開頭#replacename=‘alex say :i have one tesla,my name is alex‘print(name.replace(‘alex‘,‘SB‘,1))#修改指定字元#format的三種玩法res=‘{} {} {}‘.format(‘egon‘,18,‘male‘)#類似於預留位置res=‘{1} {0} {1}‘.format(‘egon‘,18,‘male‘)#可以指定索引佔位res=‘{name} {age} {sex}‘.format(sex=‘male‘,name=‘egon‘,age=18)#指定變數佔位#find,rfind,index,rindex,countname=‘egon say hello‘print(name.find(‘o‘,1,3)) #顧頭不顧尾,找不到則返回-1不會報錯,找到了則顯示索引# print(name.index(‘e‘,2,4)) #同上,但是找不到會報錯print(name.count(‘e‘,1,3)) #顧頭不顧尾,如果不指定範圍則尋找所有#splitname=‘root:x:0:0::/root:/bin/bash‘print(name.split(‘:‘)) #預設分隔符號為空白格name=‘C:/a/b/c/d.txt‘ #只想拿到頂級目錄print(name.split(‘/‘,1))name=‘a|b|c‘print(name.rsplit(‘|‘,1)) #從右開始切分#jointag=‘ ‘print(tag.join([‘egon‘,‘say‘,‘hello‘,‘world‘])) #可迭代對象必須都是字串#center,ljust,rjust,zfillname=‘egon‘print(name.center(30,‘-‘))print(name.ljust(30,‘*‘))print(name.rjust(30,‘*‘))print(name.zfill(50)) #用0填充#expandtabsname=‘egon\thello‘print(name)print(name.expandtabs(1))#lower,uppername=‘egon‘print(name.lower())print(name.upper())#captalize,swapcase,titleprint(name.capitalize()) #首字母大寫print(name.swapcase()) #大小寫翻轉msg=‘egon say hi‘print(msg.title()) #每個單詞的首字母大寫#is數字系列#在python3中num1=b‘4‘ #bytesnum2=u‘4‘ #unicode,python3中無需加u就是unicodenum3=‘四‘ #中文數字num4=‘Ⅳ‘ #羅馬數字#isdigt:bytes,unicodeprint(num1.isdigit()) #Trueprint(num2.isdigit()) #Trueprint(num3.isdigit()) #Falseprint(num4.isdigit()) #False#isdecimal:uncicode#bytes類型無isdecimal方法print(num2.isdecimal()) #Trueprint(num3.isdecimal()) #Falseprint(num4.isdecimal()) #False#isnumberic:unicode,中文數字,羅馬數字#bytes類型無isnumberic方法print(num2.isnumeric()) #Trueprint(num3.isnumeric()) #Trueprint(num4.isnumeric()) #True#三者不能判斷浮點數num5=‘4.3‘print(num5.isdigit()) print(num5.isdecimal())print(num5.isnumeric())‘‘‘總結:    最常用的是isdigit,可以判斷bytes和unicode類型,這也是最常見的數字應用情境    如果要判斷中文數字或羅馬數字,則需要用到isnumeric‘‘‘#is其他print(‘===>‘)name=‘egon123‘print(name.isalnum()) #字串由字母或數字組成print(name.isalpha()) #字串只由字母組成print(name.isidentifier())print(name.islower())print(name.isupper())print(name.isspace())print(name.istitle())

 

OldBoy_Python第二天學習記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.