国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

淺談Python中的數(shù)據(jù)類型

2020-02-23 01:05:57
字體:
供稿:網(wǎng)友

數(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)化成元組

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 岳阳县| 刚察县| 泾川县| 巴青县| 红安县| 巴林左旗| 灵武市| 光泽县| 博兴县| 米林县| 井陉县| 泾川县| 谢通门县| 施甸县| 京山县| 库伦旗| 庄浪县| 辽宁省| 佛山市| 宿迁市| 陕西省| 台北市| 云霄县| 上高县| 昌宁县| 康马县| 庆城县| 沈丘县| 中山市| 临湘市| 新余市| 兴海县| 习水县| 闻喜县| 宝应县| 铁力市| 沅江市| 岳池县| 织金县| 莲花县| 兴和县|