淺談Python中的數(shù)據(jù)類型
數(shù)據(jù)類型:
float — 浮點數(shù)可以精確到小數(shù)點后面15位 int — 整型可以無限大 bool — 非零為true,零為false list — 列表
Float/Int:
運算符:
/ — 浮點運算除
// — 當(dāng)結(jié)果為正數(shù)時,取整; 11//5 =2; 11//4 = 2
當(dāng)結(jié)果為負(fù)數(shù)時,向下取整;-11//5=-3; -11//4=-3
當(dāng)分子分母都是float,結(jié)果為float型
** — 計算冪; 11**2 =121
% — 取余
其他數(shù)學(xué)運算:
1.分?jǐn)?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.一些元組可以當(dāng)作字典鍵??
內(nèi)置的tuple()函數(shù)接受一個列表參數(shù)并將列表轉(zhuǎn)化成元組
同理,list()函數(shù)將元組轉(zhuǎn)換成列表
同時賦多個值:
v = (‘a(chǎn)',2, True)
(x,y,z) = v — x=‘a(chǎn)', y=2, z=True
range() — 內(nèi)置函數(shù),進行連續(xù)變量賦值
(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) = range(7)
Monday — 0
Thursday — 3
Sunday — 6
range() — 內(nèi)置函數(shù)range()構(gòu)建了一個整數(shù)序列,range()函數(shù)返回一個迭代器。
集合(里面的值是無序的):
創(chuàng)建集合:用逗號分隔每個值,用大括號{}將所有值包括起來。
a_set = {1}
type(a_set) — <class ‘set'>
以列表為基礎(chǔ)創(chuàng)建集合:
a_list = [‘a(chǎn)', ‘b', ‘mpilgrim', True, False, 42]
a_set = set(a_list)
a_set — {‘a(chǎn)', ‘b', ‘mpilgrim', True, False, 42}
a_set = set() — 得到一個空的set
a_dic = {} — 得到一個空的dic
修改集合:
a_set = {1,2}
a_set.add(4) — {1,2,4}
len(a_set) — 3
a_set.add(1) — {1,2,4}
a_set.update({2,4,6}) — {1,2,4,6}
a_set.update({3,6,9}, {1,2,3,5,8,13}) — {1,2,3,4,5,6,8,9,13}
a_set.update([15,16]) — {1,2,3,4,5,6,8,9,13,15,16}
a_set.discard(16) — {1,2,3,4,5,6,8,9,13,15}
a_set.discard(16) — {1,2,3,4,5,6,8,9,13,15}
a_set.remove(15) —{1,2,3,4,5,6,8,9,13}
a_set.remove(15) — through a exception
a_set.pop() — return 1 / {2,3,4,5,6,8,9,13}
注:a_set.pop()隨機刪掉集合中的某個值并返回該值。
a_set.clear() — set()
a_set.pop() — through exception.
集合的其他運算:
a_set = {2,3,4,5,6,8,9,13}
30 in a_set — False
4 in a_set — True
b_set = {3,4,10,12}
a_set.union(b_set) — 兩個集合的并
a_set.intersetion(b_set) — 兩個集合的交集
a_set.difference(b_set) — a_set中有但是b_set中沒有的元素
a_set.symmetric_difference(b_set) — 返回所有只在一個集合中出現(xiàn)的元素
a_set.issubset(b_set) — 判斷a_set是否是b_set的子集
b_set.issuperset(a_set) — 判斷b_set是否是a_set的超集
在布爾類型上下文環(huán)境中,空集合為假,任何包含一個以上元素的集合為真。
字典(鍵值對的無序集合):
創(chuàng)建字典:
a_dic = {‘server':'db.diveintopython3.org',
‘databas':'mysql'}
a_dic[‘server'] — ‘db.diveintopython3.org'
a_dic[‘database'] — ‘mysql'
修改字典:
a_dic[‘user'] = ‘mark' — {'user': 'mark', 'server': 'db.diveintopython3.org', 'database': ‘blog'}
a_dic[‘database'] = ‘blog' — {'user': 'mark', 'server': 'db.diveintopython3.org', 'database': ‘blog'}
a_dic[‘user'] = ‘bob' — {'user': 'bob', 'server': 'db.diveintopython3.org', 'database': ‘blog'}
a_dic[‘User'] = ‘mark' — {'user': 'bob', ‘Uuser': 'mark', 'server': 'db.diveintopython3.org', 'database': ‘blog'}
注:1.在字典中不允許有重復(fù)的鍵。對現(xiàn)有鍵賦值將會覆蓋原有值;
2.隨時可以添加新的鍵值對;
3.字典鍵區(qū)分大小寫。
混合值字典:
suffixes = { 1000:[‘KB', ‘MB', ‘GB', ‘TB', ‘PB', ‘EB', ‘ZB', ‘YB'],
1024: [‘KiB', ‘MiB', ‘GiB', ‘TiB', ‘PiB' , ‘EiB', ‘ZiB', ‘YiB']}
len(suffixes) — 2
1000 in suffixes — True
suffixes[1024] — [‘KiB', ‘MiB', ‘GiB', ‘TiB', ‘PiB' , ‘EiB', ‘ZiB', ‘YiB']
suffixes[1000][3] — ‘TB'
空字典為假, 所有其他字典為真
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
- python基礎(chǔ)教程之基本內(nèi)置數(shù)據(jù)類型介紹
- 常用python數(shù)據(jù)類型轉(zhuǎn)換函數(shù)總結(jié)
- Python基本數(shù)據(jù)類型詳細介紹
- 從零學(xué)Python之入門(二)基本數(shù)據(jù)類型
- Python3基礎(chǔ)之基本數(shù)據(jù)類型概述
- Python內(nèi)置數(shù)據(jù)類型詳解
- python基礎(chǔ)教程之基本數(shù)據(jù)類型和變量聲明介紹
- 跟老齊學(xué)Python之?dāng)?shù)據(jù)類型總結(jié)
- python通過裝飾器檢查函數(shù)參數(shù)數(shù)據(jù)類型的方法
- Python最基本的數(shù)據(jù)類型以及對元組的介紹
- 詳細解析Python當(dāng)中的數(shù)據(jù)類型和變量
- 詳細解析Python中的變量的數(shù)據(jù)類型
- Python中常見的數(shù)據(jù)類型小結(jié)
相關(guān)文章
django channels使用和配置及實現(xiàn)群聊
本文主要介紹了django channels使用和配置及實現(xiàn)群聊,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Python+tkinter實現(xiàn)動態(tài)連接數(shù)據(jù)庫
在使用 Tkinter (tk) 開發(fā) GUI 程序時,可以通過多種方式讓用戶自由更改數(shù)據(jù)庫連接地址,本文主要介紹了三種常用方法,感興趣的小伙伴可以了解下2025-03-03
Python必備shelve與dbm本地持久化存儲數(shù)據(jù)的兩個強大工具
當(dāng)涉及存儲大量數(shù)據(jù)并且需要高效訪問時,shelve和dbm模塊是Python中用于本地持久化存儲數(shù)據(jù)的兩個強大工具,它們允許開發(fā)人員以鍵值對的形式存儲數(shù)據(jù),并支持快速的檢索和更新操作,在本文將深入探討這兩個模塊,展示它們的優(yōu)勢和應(yīng)用場景2024-01-01

