1. 程式人生 > >python筆記之基礎資料型別

python筆記之基礎資料型別

八大基礎資料型別

int

python 中沒有溢位,再大的值也可以用int

num = 10

num++ 報錯,num只是儲存資料10的容器,容器不可以自增自減

print(num) # 列印容器中存放的值10
地址
print(id(num)) # id() 取變數地址
型別
print(type(num)) # type() 取變數型別

float

小數存在精度問題

num = 3.14

print(num)
print(id(num))
print(type(num))

執行結果:
3.14
2407518572976
<class ‘float’>

str

a = '普通字串'
b = "也是字串"
c = """"多行字串
另一行
再來一行"""

print(a,b,c)
print(type(c))

執行結果:
普通字串 也是字串 多行字串
另一行
再來一行
<class ‘str’>

bool 布林型別 True | False

res = True
print(type(res))

執行結果:
<class ‘bool’>

list(列表) 可變的陣列結構

l = [1,2,3,4,]

print(l)
print(type(l))

執行結果:
[1, 2, 3, 4]
<class ‘list’>

tuple(元組) 不可變的陣列結構

l = (1,2,3,4,5)

print(l)
print(type(l))

執行結果:
(1, 2, 3, 4, 5)
<class ‘tuple’>

set(集合) 不可以存放重複資料的容器

s = {1,2,3,4,5,1}
print(s)
print(type(s))

執行結果:
{1, 2, 3, 4, 5}
<class ‘set’>

dict key-value 形式儲存資料

p = {"name":"zero","age":18,"gender":"male"}
print(p)
print(type(p))

執行結果:
{‘name’: ‘zero’, ‘age’: 18, ‘gender’: ‘male’}
<class ‘dict’>