1叨恨、數(shù)字
int 奸鬓、float、complex
Python給變量賦值時不需要預先聲明變量類型。int的大小則沒有限制菇怀,只要內(nèi)存夠大瓶堕,想多大都行颠印。float就是帶小數(shù)點的數(shù)昧诱,不包括無限小數(shù),不區(qū)分精度外傅。complex就是復數(shù)纪吮,它包含一個有序?qū)Γ鏰+bj,a是實部萎胰, b是復數(shù)的虛部碾盟。
注意:python3以前的版本,有l(wèi)ong類型技竟,如果賦值超過了int的范圍(沒有超過就是int)冰肴,則自動轉換成long長整型。int的大小范圍與安裝的解釋器的位數(shù)有關榔组。
https://docs.python.org/3.1/whatsnew/3.0.html#integers
2熙尉、字符串
被定義在引號(單引號或雙引號)之間的一組連續(xù)的字符。
字符串的一些操作:
大小轉換:
S.lower() :大寫轉小寫
S.upper():小寫轉大寫
S.swapcase():小寫轉成大寫搓扯,大寫轉成小寫检痰。
S.title():首字母大寫
搜索、替換:
S.find(substr,[start,[end]]):返回在S中出現(xiàn)substr的第一個字母標號锨推,沒有則返回-1铅歼。
S.count(substr,[start,end]):計算substr在S中出現(xiàn)的次數(shù)公壤。
S.replace(oldstr,newstr,[count]):把S中的oldstr替換成newstr,count為替換的次數(shù)。
S.strip([chars]):把S左右兩端的chars中所有字符全部去掉椎椰,一般用于去掉空格厦幅。
S.lstrip([chars]):把S左端的chars中所有字符全部去掉
S.rstrip([chars]):把S右端的chars中所有字符全部去掉
分割、組合:
S.split([sep,[maxsplit]):以sep為分隔符慨飘,把S分割成列表list,maxsplit為分割次數(shù)确憨,默認分割符為空白字符。
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是否是全部是數(shù)字。
S.isspace():S是否是全部是空格医增。
S.islower():S是否是全部字母·寫的慎皱。
S.isupper():S是否是全部字母大寫的。
S.istitle():S是否是首字母大寫的叶骨。
3茫多、列表
列表是一個可變序列,每個元素分配一個位置索引忽刽,從0開始天揖。元素可以是數(shù)字、字符串跪帝、列表今膊、元組、字典伞剑。python用[]來解釋列表斑唬。
列表的一些操作:
創(chuàng)建列表
直接賦值即可。list1 = [] ?空列表 ? list2 = ['a','b','c'] 或 list2 = list('abc')
插入數(shù)據(jù)
list2.insert(0,'hello') ?在第一個位置前插入 hello
list2.insert(-1,'python') 在最后一個元素前插入python
追加數(shù)據(jù)
list2.append('world') 在最后追加world
刪除數(shù)據(jù)
list2.pop(3)刪除第4個元素黎泣。
list2.pop()刪除最后一個元素恕刘。
訪問數(shù)據(jù)
通過下標來訪問
列表分片也是列表訪問的一種方式。
將一個列表分成幾個部分抒倚。操作方法list[index1:index2[:step]]
list2[10:21] ?訪問列表第10到第20的元素褐着。注意下標是從0開始的。
list2[10:21:2]以步長2的距離訪問列表第10到第20的元素托呕。注意下標是從0開始的含蓉。
4频敛、元組
元組是一個不可變的序列,每個元素分配一個位置索引谴餐,從0開始姻政。元素可以是數(shù)字、字符串岂嗓、列表汁展、元組、字典厌殉。python用()來解釋列表食绿。
元組和列表是可以互相轉換的。tuple(list)可以將一個列表轉換成元組公罕,反過來使用list(tuple)將一個元組轉換成一個列表器紧。
5、字典
字典是一個可變序列楼眷,字典的索引叫做鍵铲汪,不能重復。字典的鍵可以是數(shù)字罐柳、字符串掌腰、列表、元組张吉、字典齿梁。python用{}來解釋列表。字典的鍵值是無序的肮蛹。
創(chuàng)建字典
ironman ?= {'name':'tony stark','age':47,'sex':'male'}
繼續(xù)添加
ironman['college'] = 'NYC'
修改
ironman['college'] = 'MIT'
刪除某個元素
del ironman['college']
del 命令可以刪除數(shù)字變量勺择、字符串變量、列表伦忠、元組省核、字典。
ironman.keys() 返回字典所有key的列表
ironman.values() 返回字典所有value的列表
ironman.items()返回字典所有(鍵缓苛,值)元組的列表
ironman.clear()刪除字典中所有的元素
ironman.get(key)返回字典中key所對應的值芳撒。