在本教程中,我們將學習如何在 Python 中進行類型轉換。
我們遇到不同類型的算術運算,其中涉及多種數據類型,然後相應地獲得結果。
在這裡我們將討論兩者,
- 隱式類型轉換
- 顯式類型轉換
讓我們藉助程序來理解它們-
隱式類型轉換
在隱式類型轉換期間,用戶不應該在轉換期間提及任何特定的數據類型。
下面的程序說明了如何用 Python 來實現。
#program to demonstrate implicit type conversion
#initializing the value of a
a = 10
print(a)
print("The type of a is ", type(a))
#initializing the value of b
b = 4.5
print(b)
print("The type of b is ", type(b))
#initializing the value of c
c = 4.0
print(c)
print("The type of c is ", type(c))
#initializing the value of d
d = 5.0
print(d)
print("The type of d is ", type(d))
#performing arithmetic operations
res = a * b
print("The product of a and b is ", res)
add = c + d
print("The addition of c and d is ", add)
輸出:
10
The type of a is <class 'int'>
4.5
The type of b is <class 'float'>
4.0
The type of c is <class 'float'>
5.0
The type of d is <class 'float'>
The product of a and b is 45.0
The addition of c and d is 9.0
解釋-
讓我們看一下這個節目的解說。
- 為了檢查這些值在執行操作時是如何轉換的,我們初始化了 a、b、c 和 d 的值。
- 在此之後,我們檢查了它們每個的數據類型。
- 最後,我們對變量 a 和 b 執行加法,對變量 c 和 d 執行乘法。
- 在執行上述程序時,我們可以觀察到,在產品的情況下,最終結果是一個浮點值(a 是整數,b 是浮點)。
現在,是時候繼續下一件事了,那就是顯式類型轉換。
顯式類型轉換
在這種類型轉換中,用戶應該在函數中傳遞值以獲得所需的數據類型。
int()、float()和 str()主要用於顯式類型轉換。
考慮下面給出的程序,
#program to demonstrate explicit type conversion
#initializing the value of a
a=10.6
print("The type of 'a' before typecasting is ",type(a))
print(int(a))
print("The type of 'a' after typecasting is",type(a))
#initializing the value of b
b=8.3
print("The type of 'b' before typecasting is ",type(b))
print(int(b))
print("The type of 'b' after typecasting is",type(b))
#initializing the value of c
c=7
print("The type of 'c' before typecasting is ",type(c))
print(float(c))
print("The type of 'c' after typecasting is",type(c))
輸出:
The type of 'a' before typecasting is <class 'float'>
10
The type of 'a' after typecasting is <class 'float'>
The type of 'b' before typecasting is <class 'float'>
8
The type of 'b' after typecasting is <class 'float'>
The type of 'c' before typecasting is <class 'int'>
7.0
The type of 'c' after typecasting is <class 'int'>
解釋-
讓我們了解一下我們在這個項目中做了什麼。
- 我們已經初始化了 a、b 和 c 的值。
- 我們在這個程序中使用了 int()和 float()來查看顯式類型轉換是如何發生的。
- 在執行這個程序時,我們可以驗證數據類型是如何變化的。
最後,讓我們看一下本文的最後一個程序,它涵蓋了顯式類型轉換的可能類型。
#program to demonstrate explicit type conversion
#initializing the value of a
a=10
print("The type of 'a' before typecasting is ",type(a))
print(str(a))
print("The type of 'a' after typecasting is",type(a))
#initializing the value of b
b='8'
print("The type of 'b' before typecasting is ",type(b))
print(int(b))
print("The type of 'b' after typecasting is",type(b))
#initializing the value of c
c='7.9'
print("The type of 'c' before typecasting is ",type(c))
print(float(c))
print("The type of 'c' after typecasting is",type(c))
輸出:
The type of 'a' before typecasting is <class 'int'>
10
The type of 'a' after typecasting is <class 'int'>
The type of 'b' before typecasting is <class 'str'>
8
The type of 'b' after typecasting is <class 'str'>
The type of 'c' before typecasting is <class 'str'>
7.9
The type of 'c' after typecasting is <class 'str'>
解釋-
該方法類似於上面的程序,在這個程序中,我們包含了使用所有三個 int()、str()和 float()的轉換。
結論
在本教程中,我們學習了什麼是 python 中的類型轉換,它的類型是什麼,以及如何在 Python 中執行。
原創文章,作者:HEYWN,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/127529.html