整數與浮點數python
## python2.7
a = 123848488483993932002093939L ## 要加L,py2.7有長整型
b = 23.03
c = 10 / 4 ## 2
d = 10 / 4.0 ## 2.5
e = 10 // 4 ## 2
f = 10 // 4.0 ## 2.0
## python3.5
a = 1238484884839939320020939393839303 ## 不須要加L,python3.x是屬於單個整數類型,不區分長整型
b = 29.90
c = 10 / 4 ## 2.5
d = 10 / 4.0 ## 2.5
e = 10 // 4 ## 2
f = 10 // 4.0 ## 2.0複製代碼
Boolean,只有兩個值:True && Falsepython3.x
a = False
b = True複製代碼
十進制轉16進制、8進制、2進制python2.7
num = 29 ## 10進制
## 轉16進制
hex(num) ## 0x1d
## 轉8進制
oct(num) ## 0o35
## 轉2進制
bin(num) ## 0b11101複製代碼
16進制、8進制、2進制轉十進制this
## 16進制轉10進制
str_hex = r'0x1d'
int(str_hex,base = 16)
## 8進制轉10進制
str_oct = r'0o35'
int(str_oct,base = 8)
## 2進制轉10進制
str_bin = r'0b11101'
int(str_bin,base = 2)複製代碼
序列類型簇spa
基本操做符code
序列比較cmp原理(A > B or A < B)orm
String是由零個或者多個字符組成的有限序列,一般以串的總體做爲操做對象,字符串以引號包含標識對象
## 字符串定義
a = 'single' ## 單引號表示
b = "double" ## 雙引號表示
c = """...spam...""" ## 三引號表示
d = '''...spam...''' ## 三引號表示
e = "s\tp\na\0m" ## 轉義序列
f = r"C:\new\test.spm" ## raw 字符串,即用r禁用轉義字符
bs = b'sp\x01am' ## 字節字符串
us = u"中文" ## Unicode字符串複製代碼
==若是字符串包含中文,應當聲明爲unicode字符串==排序
字符串格式化表達式索引
## 格式化表達式:"...%s..." % (values) Or "...%(keyname)s..." % {keyname:keyvalue}
>>> "this is my %s" % ('name')
'this is my name'
>>> "this is my %(name)s" % {'name':'xiao'}
'this is my xiao'
## 格式化方法調用:"...{}...".format(values) Or "...{keyname1}...".format(keyname1=value1)
>>> "my name is {}".format('xiaokunliu')
'my name is xiaokunliu'
>>> "this is my {firstname},and {lastname}".format(firstname="xiaokun",lastname="liu")
'this is my xiaokun,and liu'複製代碼
字符串格式化結構:%[(keyname)][flags][width][.precision]typecode
## keyname:字典對應索引的keyname
>>> '%(name)s' % {'name':'xiaokun'}
'xiaokun'
## flags:格式化符號(-、+、space空格鍵<sp>、#、0、m.n)
## -:左對齊;
## +:對正數輸出正值符號;
## m.n:m是顯示最小總寬度,n是小數點後的位數;
## #:在8進制數前面顯示0,16進制前面顯示"0x"或者"0X"
## 0:數字大小不足m.n的要求時用0補位;
## <sp>:數字大小不足m.n的要求時用空格補位;
x = 1.23456789
>>> "%-6.2f,%+3.4f,%#2.3f,%06.9f,% 3.9f" % (x,x,x,x,x)
'1.23 ,+1.2346,1.235,1.234567890, 1.234567890'
## width:指定顯示字符串的寬度
>>> '%−6.2f' % x
'1.23 '
'
## .precision:整數格式化爲字符串的時候須要保留的小數位數
>>> '%.3f' % x
'1.235'
## typecode:格式化符號
%s String (or any object’sstr(X)string)
%r Same ass, but usesrepr, not str
%c Character (int or str)
%d Decimal (base-10 integer)
%i Integer
%u Same asd(obsolete: no longer unsigned)
%o Octal integer (base 8)
%x Hex integer (base 16)
%X Same asx, but with uppercase letters
%e Floating point with exponent, lowercase
%E Same ase, but uses uppercase letters
%f Floating-point decimal
%F Same asf, but uses uppercase letters
%g Floating-point e or f
%G Floating-point E or F
%% Literal%(coded as%%)複製代碼
定義
簡單示例
## 定義
>>> tuple1 = ('you',283,93,'English',4,5.6)
## 讀取
>>> tuple1[3]
## 截取子元組
>>> subTuple = tuple1[3:]
## 能夠對元組變量從新賦值,注意是元組變量不是元組對象
>>> tuple1 = tuple1 + (2,3,'playing')
## 獲取長度
>>> len(tuple1)
## 不能修改
>>> tuple1[2] = "update" ### 這裏將發生錯誤
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
## 元素可重複
>>> tuple1 = tuple1 * 2複製代碼
定義
簡單示例
## 定義
>>> color_list = ['green','red','pink','blue','black','white']
## 讀取元素
>>> color_list[2]
## 截取子列表
>>> color_list[3:]
## 修改指定序列的內容
>>> color_list[2] = "update_pink"
## 獲取長度
>>> len(color_list)
## 元素遍歷
>>> for color in color_list:
... print color
...
## 排序,排序後自己的color_list不變
>>> sorted_color_list = sorted(color_list)
## 求和
>>> num_list = [3.4,5,6,3,5.7,12,27,32]
>>> sum(num_list),'%i' % sum(num_list) '%.2f' % sum(num_list)
## 以指定的方式排序 list.sort(fn = None,key = None,reverse = False)
>>> num_list = [29,39,4,23,42,13,19,21]
>>> num_list.sort() ## 改變list對象自己
>>> sorted(num_list) ## 返回一個排序後的list對象,不改變num_list
## 按照key值排序
>>> key_list = [('name':3),('age':10),('num':23),('key':24)]
>>> key_list.sort(key = lambda x:x[1])
## 倒序排序
>>> key_list.sort(reverse = True)
## 正向排序
>>> key_list.sort(reverse = False)複製代碼
類型定義
簡單示例
## 初始化
set_list = [2,39,9,3,4,5,28,34]
>>> s1 = set(set_list)
{2, 3, 4, 5, 34, 39, 9, 28}
>>> s2 = frozenset(set_list)
frozenset({2, 3, 4, 5, 34, 39, 9, 28})
## 判斷包含關係
>>> print 9 in s1
## 判斷子集關係
>>> print s1 >= s2
## 差運算
>>> print s1 - s2
## 交運算
>>> print s1 & s2
## 執行並運算並賦值給s1
>>> print s1 |= s2
## 對稱差運算
>>> print s1 ^ s2 ## 獲取s1不在s2的元素以及s2不在s1的元素
## 集合遍歷
>>> for ele in s1:
... print(ele)複製代碼
類型定義
簡單示例
## 字典定義
>>> dc = {"name":"xiaokun","age":4,"title":"english name"}
## 讀取元素
>>> print dc['name']
## 直接經過下標增長字典字段
>>> dc['new_name'] = 'new_value'
## 合併字典
>>> dc1 = {"name2":"xiaokun","age2":4,"title2":"english name"}
>>> dc.update(dc1)複製代碼