數(shù)據(jù)類型:
float — 浮點數(shù)可以精確到小數(shù)點后面15位int — 整型可以無限大bool — 非零為true,零為falselist — 列表
Float/Int:
運算符:
/ — 浮點運算除
// — 當結(jié)果為正數(shù)時,取整; 11//5 =2; 11//4 = 2
當結(jié)果為負數(shù)時,向下取整;-11//5=-3; -11//4=-3
當分子分母都是float,結(jié)果為float型
** — 計算冪; 11**2 =121
% — 取余
其他數(shù)學運算:
1.分數(shù):
import fractions;
fractions.Fraction(1,3) — 1/3
import math;
—math.sin()
—math.cos()
—math.tan()
—math.asin()
math.pi —3.1415926…
math.sin(math.pi/2) — 1.0
math.tan(math.pi/4) — 0.9999999999…
math.sin(); math
List:
創(chuàng)建: a_list = [‘a(chǎn)', ‘b', ‘mpilgrim', ‘z', ‘example']
a_list[-1] — ‘example'
a_list[0] — ‘a(chǎn)'
a_list[1:3] — [‘b', ‘mpilgrim', ‘z']
a_list[:3] — [‘a(chǎn)', ‘b', ‘mpilgrim' ]
a_list[3:] — [‘z', ‘example']
a_list[:]/a_list — [‘a(chǎn)', ‘b', ‘mpilgrim', ‘z', ‘example']
*注:a_list[:] 與a_list 返回的是不同的list,但它們擁有相同的元素
a_list[x:y]— 獲取list切片,x指定第一個切片索引開始位置,y指定截止但不包含的切片索引位置。
向list添加元素:
a_list = [‘a(chǎn)']
a_list = a_list + [2.0, 3] — [‘a(chǎn)', 2.0, 3]
a_list.append(True) — [‘a(chǎn)', 2.0, 3, True]
a_list.extend([‘four','Ω']) — [‘a(chǎn)', 2.0, 3, True,'four','Ω']
a_list.insert(0,'Ω') — [‘Ω','a', 2.0, 3, True,'four','Ω']
list其他功能:
a_list = [‘a(chǎn)', ‘b', ‘new', ‘mpilgrim', ‘new']
a_list.count(‘new') — 2
a_list.count(‘mpilgrim') — 1
‘new' in a_list — True
a_list.index(‘new') — 2
a_list.index(‘mpilgrim') — 3
a_list.index(‘c') — through a exception because ‘c' is not in a_list.
del a_list[1] — [‘a(chǎn)', ‘new', ‘mpilgrim', ‘new']
a_list.remove(‘new') — [‘a(chǎn)', mpilgrim', ‘new']
注:remove只刪除第一個'new'
a_list.pop() — 'new'/[‘a(chǎn)', mpilgrim' ](刪除并返回最后一個元素)
a_list.pop(0) — ‘a(chǎn)' / [‘mpilgrim'] (刪除并返回第0個元素)
空列表為假,其他列表為真。
元組(元素是不可變的列表):
定義:與列表的定義相同,除了整個元素的集合用圓括號而,不是方括號閉合
a_tuple = (“a”, “b”, “mpilgrim”, “z”, “example”)
a_tuple = (‘a(chǎn)', ‘b', ‘mpilgrim', ‘z', ‘example')
tuple 只能索引,不能修改。
元組相對于列表的優(yōu)勢:
1.速度快
2.“寫保護”,更安全
3.一些元組可以當作字典鍵??
內(nèi)置的tuple()函數(shù)接受一個列表參數(shù)并將列表轉(zhuǎn)化成元組
新聞熱點
疑難解答
圖片精選