強制轉型為int(整數)
強制轉型為float(浮點數)
強制轉型為int(整數)
a=1
b=2.8
c="3"
print(type(a))
print(type(b))
print(type(c))
print(a)
print(b)
print(c)
#=========================
x = int(a) #強制把a(整數轉型成整數)沒變化
y = int(b) #強制把b(浮點數轉型成整數)會把小數點去掉
z = int(c) #強制把c(字串轉型成整數)變成可以做計算的整數
print(type(x))
print(type(y))
print(type(z))
print(x)
print(y)
print(z)

強制轉型為浮點數
x = float(1) #1.0
y = float(2.8) #2.8
z = float("3") #3.0
w = float("4.2")#4.2
print(x)
print(y)
print(z)
print(w)

強制轉型為字串
轉為字串後就無法計算了
x = str("s1") #s1
y = str(2) #2
z = str(3.0) #3.0
print(x)
print(y)
print(z)

實作:
a = "5"
b = 7
請計算c=a*b
使用強制轉型的方式印出c變數
參考:
a = "5"
b = 7
c =int(a)*b
print(c)

實作2:
請印出b句子
age=10
b="i'm " + age + " years old"
print(b)
方式一:
age=10
b="i'm " + str(age) + " years old"
print(b)

不同型別除了強制轉型外,也可以使用 逗號(,)連結 印出
age=10
mystr="hi"
a=age,mystr
print(a)

Yiru@Studio - 關於我 - 意如