Python基本資料型別

2021-08-16 04:23:13 字數 3557 閱讀 4341

built-in types

numeric數值型別:int , float , complex 操作

結果x + y

和x - y

差x * y

乘積x / y

x除以y,結果為浮點型

x // y

整除1//2 = 0, (-1)//2 = -1, 1//(-2) = -1, (-1)//(-2) = 0,趨向於小的值

x % y

x/y的餘數

-x取反

+x不變

abs(x)

絕對值int(x)

產生整數,只取整數部分int(-8.99)=8;int(8.99)=8 等同math.trunc(x)

float(x)

產生浮點數

complex(re, im)

產生複數

c.conjugate()

取共軛divmod(x, y)

(x // y, x % y)

pow(x, y)

x^yx ** y同上

可變資料型別

不可變資料型別

list, dictionary, set, numpy array, user defined objects

integer, float, long, complex, string, tuple, frozenset

元組

元組與列表類似,元組tuple也是個有序序列,但是元組是不可變的

1. 元組使用括號巢狀,各個元素之間使用逗號分開(括號可以沒有)

2. 元組中的元素不可更改,但元素可以是可變資料型別

3. 空元組可以用一對括號表示;單元素元組要在元素後面加上逗號

#使用括號巢狀,括號可以沒有;單元素元組和空元素元組的表示

empty=()

singleton=9,

print(empty,singleton)

#可以包含列表

list1=[1,2,3]

list2=[4,5,6]

cell=list1,list2,'str'

print(cell,cell[0])

cell[0][0]=9

print(cell)

*執行結果*

() (9,)

([1, 2, 3], [4, 5, 6], 'str') [1, 2, 3]

([9, 2, 3], [4, 5, 6], 'str')

為什麼需要元組

舊式字串格式化中引數要用元組;

在字典中當作鍵值;

資料庫的返回值……

字典dict3、 字典方法:

#使用{}產生字典

a={}

a['one']='this is number1'

a['two']='this is number2'

print(a,a['one'])

#使用dict()轉化來生成字典

inventory = dict(

[('foozelator', 123),

('frombicator', 18),

('spatzleblock', 34),

('snitzelhogen', 23)

])print(inventory)

*執行結果*

this is number1

#get方法

c=inventory.get('foozelator')

d=inventory.get('a',none)

print(c,d)

*執行結果*

123 none

#pop方法

de=inventory.pop('frombicator')

no=inventory.pop('one','not exit')

print(de,no)

*執行結果*

18not

exit

#update方法

inventory_mod=

print(inventory)

*執行結果*

#in方法

[print(inventory.get(item)) for item in inventory ]

*執行結果*

12334

23#keys方法,values方法,items方法

list1=inventory.keys()

list2=inventory.values()

list3=inventory.items()

print(list1,list2,)

print(list3)

*執行結果*

dict_keys(['foozelator', 'spatzleblock', 'snitzelhogen']) dict_values([123, 34, 23])

dict_items([('foozelator', 123), ('spatzleblock', 34), ('snitzelhogen', 23)])

集合set

生成set的兩種方法

:set()生成空集合,set(list)在set函式內部傳入列表可以初始化集合

:使用可以生成集合,但是不能用來生成空集合

set是無序序列,因為集合是無序的,所以當集合中存在兩個同樣的元素的時候,python只會儲存其中的乙個(唯一性);同時為了確保其中不包含同樣的元素,集合中放入的元素只能是不可變的物件(確定性)

set方法

操作或方法

含義a.union(b) 或者 a b

兩個集合的並,返回包含兩個集合所有元素的集合(去除重複)

a.intersection(b) 或者 a & b

兩個集合的交,返回包含兩個集合共有元素的集合

a.difference(b) 或者 a - b

a 和 b 的差集,返回只在 a 不在 b 的元素組成的集合

a.symmetric_difference(b) 或者 a ^ b

a 和b 的對稱差集,返回在 a 或在 b 中,但是不同時在 a 和 b 中的元素組成的集合

b.issubset(a) 或者 b <= a

要判斷 b 是不是 a 的子集

a.issuperset(b) 或者 a >= b

要判斷 a 是不是 b 的子集

s.add(a)

向s集合新增元素a,如果新增的是已有元素,集合不改變

s.update(seq)

向集合新增多個元素

s.remove(ob)

從集合s中移除元素ob,如果不存在會報錯

t.pop()

由於集合沒有順序,不能像列表一樣按照位置彈出元素,所以pop 方法刪除並返回集合中任意乙個元素;如果集合中沒有元素會報錯

t.discard(a)

作用與 remove 一樣,但是當元素在集合中不存在的時候不會報錯

a.difference_update(b)

從a中去除所有屬於b的元素

python基本資料型別

物件是python中最基本的概念,python中資料以物件的形式出現 無論是python提供的內建物件,還是使用python或是像c擴充套件庫這樣的擴充套件語言工具建立的物件。物件時記憶體中的一部分,包括數值和相關操作的集合。python程式可以分解成模組 語句 表示式以及物件,如下 1 程式由模組...

Python基本資料型別

1 python中一切都是物件。2 每乙個資料都有乙個id標示,用id 可以檢視。也可以用type檢視是什麼型別。3 常用的資料型別 int 整型 數字 boole true 值 賦值,要用大寫 a true string 字串 也稱作序列。list 列表 tuple 元組 dict 字典 set ...

Python基本資料型別

python內建許多資料基本型別。資料型別dt 表示形式 int整形如 1,0,1,float 浮點型如 1.1,0.0,1.1,str字串如 單引號或雙引號括起來的形式 hello python list 列表如 1,2 巢狀列表 1,2,3 tuple 元組如 1,2 set無序列表如 comp...