1. 程式人生 > >python筆記之數字的使用

python筆記之數字的使用

# 數字的使用
# int 整型
# float 浮點型
# bool 布林型別

# +驗證
a = 10
b = 20
print(a + b)

c = 3.14
d = 1.86
print(c + d)

e = True
f = False
res = e + f
print(res,type(res))
print(True + True + False * True) # 2

# 以型別方式定義變數

num = int(10)
print(num,id(num),type(num))
# 10 1496036704 <class 'int'>
# 將可以轉換為整型的其他型別數字,轉換成整型
# 資料型別的轉換
num = int(3.14)
print(num,id(num),type(num))
# 3 1496036480 <class 'int'>

s = "88888"
num = s
print(num,id(num),type(num))
# 88888 2017114471144 <class 'str'>

s = "88888"
num = int(s)
print(num,id(num),type(num))
# 88888 2238146906896 <class 'int'>

# 瞭解:python以換行作為語句的結束標誌(斷句)
# 如果出現一行有多條語句,之間可以用;作為結束標誌
# 需求:交換兩個變數的值
a = 10
b = 20
# 結果:a=20 | b=10
# 藉助第三者
#temp = a
#a = b
#b = temp
#print(a,b)

# 利用計算的演算法
#res = a + b
#a = res - a
#b = res - b
#print(a,b)

# 利用交叉賦值
a,b = b,a
print(a,b)

# 小結
#int: num = 10 => 存放的是整型數字
#float: num = 3.14 => 存放的是浮點型
#bool: r = True => 存放的其實就是數字0(False)和1(True)
#三者皆可以直接做數值的所有運算

#型別的轉換
#以 int("123") float(3) bool(1) 來定義變數

# 交叉賦值
# a,b = b,a
# 按位進行一對一賦值