2025.06.29 Python學習進度
今天學習了型別轉換Type casting:顯式轉換(Explicit)&隱式轉換(Implicit)
# int():轉整數
# float():轉浮點數
# str():轉字串
# bool():轉布林值
# 顯式轉換(Explicit)
演示用
age = 6
weight = 3.7
one = True
name = 'lute'
age = float(age)
print(age)
print(type(age))
weight = int(weight)
print(weight)
print(type(weight))
one = str(one)
print(one)
print(type(one))
name = bool(name)
print(name)
print(type(name))
# 隱式轉換(Implicit)
# 這是出於Python自動作業:當運算兩個不同型別的值時,Python會自動轉換成更高精度的型別來處理。
# 隱式轉換通常僅限於整數、浮點數、布林值,這三種變元使用,
# 目前確定可行的三種隱式轉換:布林轉整數(bool to int)、整數轉浮點數(int to float)、布林轉浮點數(bool to float)
演示用
x = 50
print(x)
print(type(x))
y = 2.5
z = x/y
print(y)
print(z)
print(type(z))
# 額外學習:Python會按照最後一次的賦值為準,不會限制只能用一種型別。