Python學習——Python基礎資料型別 (Elementary Data Type)(一),python學習資料類型

來源:互聯網
上載者:User

Python學習——Python基礎資料型別 (Elementary Data Type)(一),python學習資料類型

                                              python3 基礎資料型別 (Elementary Data Type)   
Python3 中的變數不需要聲明。每個變數在使用前都必須賦值,變數賦值以後該變數才會被建立。
Python3 中有6個標準的資料類型:Number(數字);字串(String);列表(list);元組(Tuple);字典:(Dict);集合(Sets)

Number(數字):
Python3支援int,float,bool,complex(複數)
type()函數可以查看變數所指的物件類型

String(字串):
Python中的字串用單引號(')或雙引號(")括起來,同時使用反斜線(\)轉義特殊字元。
注意:'''...'''三元引號在建立短字串時沒有什麼特別用處,主要用於建立多行字串,如下例:
>>> poem =  '''There was a Young Lady of Norway,
... Who casually sat in a doorway;
... When the door squeezed her flat,
... She exclaimed, "What of that?"
... This courageous Young Lady of Norway.'''

Python也允許空串存在,不包含任何字元,完全合法!
數字與字串之間的轉換:

字串轉換成數字:
>>> int('20')
20
>>> float('20')
20.0
>>> int(20)
20

數字轉換成字串:
>>> str(20)
'20'
>>> str(20.0)
'20.0'
>>> str(True)
'True'

使用+拼接
在python中可以試用+將多個字串或字串變數進行拼接
>>> 'Release the Tom!' + 'At once!'
Release the Tom! At once!

使用[]提取字元
在字串後面添加[],在[]中添加位移量,即可取出該位置的字串。第一個字元位移量為0,第二個為1,後面依次類推。 右邊第一個位移量為-1,第二個為-2,從右往左依次類推...
>>> str = 'abcdefghijklmnopqrstuvwxyz'
>>> str[0]
'a'
>>> str[-1]
'z'
>>> str[3]
'd'

字串是不可變的,有時候改變字串,需要組合使用一些字串函數:replace(),以及分區操作
>>> name = 'Henny'
>>> name.replace('H','P')
'Penny'
>>> 'P' + name[1:]
'Penny'

使用[start:end:step]分區:
分區操作:可以從一個字串中抽取子字串。起始位移量start,終止位移量end以及可選的步長step來定義一個分區
1.[:]提取從開頭到結尾的整個字串
2.[start:]從start提取到結尾
3.[:end]從開頭提取到end-1
4.[start:end]從start提取到end-1結尾
5.[start:end:step]從start提取到end-1,每個step提取一個字元

>>> str = 'qwertyuiop'
>>> str[:]
'qwertyuiop'
>>> str[5:]
'yuiop'
>>> str[-3:]
'iop'
>>> str[:-3]
'qwertyu'
>>> str[-6:-3]
'tyu'
步長為3
>>> str[::3]
'qrup'
利用切片反向輸出字串
>>> str[::-1]
'poiuytrewq'

字串其他常用操作:
>>> str = 'qwertyuiop'

計算字串的長度
>>> len(str)
10

使用split()分割:
使用內建的字串函數 split() 可以基於 分隔字元 將字串分割成由若干子串組成的 列表 。
所謂列表(list)是由一系列值組成的序列,值與值之間由逗號隔開,整個列表被方括弧所包裹。
>>> todos = 'get gloves,get mask,give cat vitamins,call ambulance'
>>> todos.split(',')
['get gloves', 'get mask', 'give cat vitamins', 'call ambulance']
上面例子中,字串名為 todos,函數名為 split(),傳入的參數為單一的分隔字元split(),傳入的參數為單一的分隔字元 ','。
如果不指定分隔字元,那麼 split() 將預設使用空白字元——分行符號、空格、定位字元。
>>> todos.split()
['get', 'gloves,get', 'mask,give', 'cat', 'vitamins,call', 'ambulance']

使用join()合并:
可能你已經猜到了,join() 函數與 split() 函數正好相反:它將包含若干子串的列表分解,並將這些子串合成一個完整的大的字串。join()
>>> crypto_list = ['Yeti', 'Bigfoot', 'Loch Ness Monster']
>>> crypto_string = ', '.join(crypto_list)
>>> print('Found and signing book deals:', crypto_string)
Found and signing book deals: Yeti, Bigfoot, Loch Ness Monster

使用replace()替換:
>>> str = 'qwertyuiop'
>>> str.replace('w','ooooo')
'qoooooertyuiop'
最多修改3處
>>> str = 'qwerwerwerwtytewwiitw'
>>> str.replace('w','oooo',3)
'qooooerooooerooooerwtytewwiitw'
計算字串中'w'出現的次數
>>> str.count('w')
7

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.