python的標準數據類型

1、數字

int 、float、complex

Python給變量賦值時不需要預先聲明變量類型。int的大小則沒有限制,只要內存夠大,想多大都行。float就是帶小數點的數,不包括無限小數,不區分精度。complex就是復數,它包含一個有序對,如a+bj,a是實部, b是復數的虛部。

注意:python3以前的版本,有long類型,如果賦值超過了int的范圍(沒有超過就是int),則自動轉換成long長整型。int的大小范圍與安裝的解釋器的位數有關。

https://docs.python.org/3.1/whatsnew/3.0.html#integers


2、字符串

被定義在引號(單引號或雙引號)之間的一組連續的字符。

字符串的一些操作:

大小轉換:

S.lower() :大寫轉小寫

S.upper():小寫轉大寫

S.swapcase():小寫轉成大寫,大寫轉成小寫。

S.title():首字母大寫

搜索、替換:

S.find(substr,[start,[end]]):返回在S中出現substr的第一個字母標號,沒有則返回-1。

S.count(substr,[start,end]):計算substr在S中出現的次數。

S.replace(oldstr,newstr,[count]):把S中的oldstr替換成newstr,count為替換的次數。

S.strip([chars]):把S左右兩端的chars中所有字符全部去掉,一般用于去掉空格。

S.lstrip([chars]):把S左端的chars中所有字符全部去掉

S.rstrip([chars]):把S右端的chars中所有字符全部去掉

分割、組合:

S.split([sep,[maxsplit]):以sep為分隔符,把S分割成列表list,maxsplit為分割次數,默認分割符為空白字符。

S.join(seq):用S把seq代表的字符串序列連接起來。

編碼、解碼:

Python 3.0 uses the concepts of?text?and (binary)data?instead of Unicode strings and 8-bit strings. All text is Unicode; however?encoded?Unicode is represented as binary data. The type used to hold text is?str, the type used to hold data is?bytes. The biggest difference with the 2.x situation is that any attempt to mix text and data in Python 3.0 raises?TypeError, whereas if you were to mix Unicode and 8-bit strings in Python 2.x, it would work if the 8-bit string happened to contain only 7-bit (ASCII) bytes, but you would get?UnicodeDecodeErrori f it contained non-ASCII values. This value-specific behavior has caused numerous sad faces over the years.

As a consequence of this change in philosophy, pretty much all code that uses Unicode, encodings or binary data most likely has to change. The change is for the better, as in the 2.x world there were numerous bugs having to do with mixing encoded and unencoded text. To be prepared in Python 2.x, start using unicode for all unencoded text, and?str?for binary or encoded data only. Then the 2to3 tool will do most of the work for you.

You can no longer use u"..." literals for Unicode text. However, you must use b"..."literals for binary data.

As the?str?and?bytes?types cannot be mixed, you must always explicitly convert between them. Use?str.encode()?to go from?str?to?bytes, and?bytes.decode()?to go from?bytes?to?str. You can also use bytes(s,encoding=...) and str(b,encoding=...), respectively.

Like?str, the?bytes?type is immutable. There is a separate?mutable?type to hold buffered binary data,?bytearray. Nearly all APIs that accept?bytes?also accept?bytearray. The mutable API is based on?collections.MutableSequence.

https://docs.python.org/3.1/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit

S.decode([encoding]):將以encoding編碼的S解碼成unicode編碼。

S.encode([encoding]):將以unicode編碼的S編碼成encoding編碼。encoding可以是gb2312、gbk、big5...

測試:

S.isalpha():S是否是全部是字母。

S.isdigit():S是否是全部是數字。

S.isspace():S是否是全部是空格。

S.islower():S是否是全部字母·寫的。

S.isupper():S是否是全部字母大寫的。

S.istitle():S是否是首字母大寫的。

3、列表

列表是一個可變序列,每個元素分配一個位置索引,從0開始。元素可以是數字、字符串、列表、元組、字典。python用[]來解釋列表。

列表的一些操作:

創建列表

直接賦值即可。list1 = [] ?空列表 ? list2 = ['a','b','c'] 或 list2 = list('abc')

插入數據

list2.insert(0,'hello') ?在第一個位置前插入 hello

list2.insert(-1,'python') 在最后一個元素前插入python


追加數據

list2.append('world') 在最后追加world

刪除數據

list2.pop(3)刪除第4個元素。

list2.pop()刪除最后一個元素。

訪問數據

通過下標來訪問

列表分片也是列表訪問的一種方式。

將一個列表分成幾個部分。操作方法list[index1:index2[:step]]

list2[10:21] ?訪問列表第10到第20的元素。注意下標是從0開始的。

list2[10:21:2]以步長2的距離訪問列表第10到第20的元素。注意下標是從0開始的。

4、元組

元組是一個不可變的序列,每個元素分配一個位置索引,從0開始。元素可以是數字、字符串、列表、元組、字典。python用()來解釋列表。

元組和列表是可以互相轉換的。tuple(list)可以將一個列表轉換成元組,反過來使用list(tuple)將一個元組轉換成一個列表。

5、字典

字典是一個可變序列,字典的索引叫做鍵,不能重復。字典的鍵可以是數字、字符串、列表、元組、字典。python用{}來解釋列表。字典的鍵值是無序的。

創建字典

ironman ?= {'name':'tony stark','age':47,'sex':'male'}

繼續添加

ironman['college'] = 'NYC'

修改

ironman['college'] = 'MIT'

刪除某個元素

del ironman['college']

del 命令可以刪除數字變量、字符串變量、列表、元組、字典。

ironman.keys() 返回字典所有key的列表

ironman.values() 返回字典所有value的列表

ironman.items()返回字典所有(鍵,值)元組的列表

ironman.clear()刪除字典中所有的元素

ironman.get(key)返回字典中key所對應的值。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容