淺談Python中的資料類型

來源:互聯網
上載者:User
資料類型:

float — 浮點數可以精確到小數點後面15位int — 整型可以無限大bool — 非零為true,零為falselist — 列表

Float/Int:

運算子:

/ — 浮點運算除
// — 當結果為正數時,取整; 11//5 =2; 11//4 = 2
當結果為負數時,向下取整;-11//5=-3; -11//4=-3

當分子分母都是float,結果為float型

** — 計算冪; 11**2 =121
% — 取餘

其他數學運算:

1.分數:

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:

建立: a_list = [‘a', ‘b', ‘mpilgrim', ‘z', ‘example']

a_list[-1] — ‘example'
a_list[0] — ‘a'
a_list[1:3] — [‘b', ‘mpilgrim', ‘z']
a_list[:3] — [‘a', ‘b', ‘mpilgrim' ]
a_list[3:] — [‘z', ‘example']
a_list[:]/a_list — [‘a', ‘b', ‘mpilgrim', ‘z', ‘example']

*註:a_list[:] 與a_list 返回的是不同的list,但它們擁有相同的元素

a_list[x:y]— 擷取list切片,x指定第一個切片索引開始位置,y指定截止但不包含的切片索引位置。

向list添加元素:

a_list = [‘a']
a_list = a_list + [2.0, 3] — [‘a', 2.0, 3]
a_list.append(True) — [‘a', 2.0, 3, True]
a_list.extend([‘four','Ω']) — [‘a', 2.0, 3, True,'four','Ω']
a_list.insert(0,'Ω') — [‘Ω','a', 2.0, 3, True,'four','Ω']

list其他功能:

a_list = [‘a', ‘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', ‘new', ‘mpilgrim', ‘new']
a_list.remove(‘new') — [‘a', mpilgrim', ‘new']

註:remove只刪除第一個'new'

a_list.pop() — 'new'/[‘a', mpilgrim' ](刪除並返回最後一個元素)
a_list.pop(0) — ‘a' / [‘mpilgrim'] (刪除並返回第0個元素)

空列表為假,其他列表為真。

元組(元素是不可變的列表):

定義:與列表的定義相同,除了整個元素的集合用圓括弧而,不是方括弧閉合

a_tuple = (“a”, “b”, “mpilgrim”, “z”, “example”)
a_tuple = (‘a', ‘b', ‘mpilgrim', ‘z', ‘example')

tuple 只能索引,不能修改。

元組相對於列表的優勢:

1.速度快
2.“防寫保護”,更安全
3.一些元組可以當作字典鍵??

內建的tuple()函數接受一個列表參數並將列錶轉化成元組

同理,list()函數將元群組轉換成列表

同時賦多個值:

v = (‘a',2, True)
(x,y,z) = v — x=‘a', y=2, z=True

range() — 內建函數,進行連續變數賦值

(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) = range(7)

Monday — 0
Thursday — 3
Sunday — 6
range() — 內建函數range()構建了一個整數序列,range()函數返回一個迭代器。

集合(裡面的值是無序的):

建立集合:用逗號分隔每個值,用大括弧{}將所有值包括起來。
a_set = {1}
type(a_set) —
以列表為基礎建立集合:
a_list = [‘a', ‘b', ‘mpilgrim', True, False, 42]
a_set = set(a_list)
a_set — {‘a', ‘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) — 返回所有只在一個集合中出現的元素
a_set.issubset(b_set) — 判斷a_set是否是b_set的子集
b_set.issuperset(a_set) — 判斷b_set是否是a_set的超集

在布爾類型上下文環境中,空集合為假,任何包含一個以上元素的集合為真。

字典(索引值對的無序集合):

建立字典:

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.在字典中不允許有重複的鍵。對現有鍵賦值將會覆蓋原有值;
2.隨時可以添加新的索引值對;
3.字典鍵區分大小寫。

混合值字典:

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'

空字典為假, 所有其他字典為真

以上所述就是本文的全部內容了,希望大家能夠喜歡。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.