最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python教程之類型轉(zhuǎn)換詳解

 更新時(shí)間:2022年08月17日 15:55:51   作者:海擁  
Python?定義了類型轉(zhuǎn)換函數(shù)以將一種數(shù)據(jù)類型直接轉(zhuǎn)換為另一種數(shù)據(jù)類型,這在日常和競爭性編程中很有用,本文將和大家一起詳細(xì)聊聊Python中的類型轉(zhuǎn)換

Python 定義了類型轉(zhuǎn)換函數(shù)以將一種數(shù)據(jù)類型直接轉(zhuǎn)換為另一種數(shù)據(jù)類型,這在日常和競爭性編程中很有用。本文旨在提供有關(guān)某些轉(zhuǎn)換函數(shù)的信息。

Python中有兩種類型轉(zhuǎn)換:

  • 隱式類型轉(zhuǎn)換
  • 顯式類型轉(zhuǎn)換

讓我們詳細(xì)討論它們。

隱式類型轉(zhuǎn)換

在 Python 中數(shù)據(jù)類型的隱式類型轉(zhuǎn)換中,Python 解釋器會自動(dòng)將一種數(shù)據(jù)類型轉(zhuǎn)換為另一種數(shù)據(jù)類型,而無需任何用戶參與。要更清楚地了解該主題,請參閱以下示例。

例子:

x = 10

print("x is of type:",type(x))

y = 10.6
print("y is of type:",type(y))

z = x + y

print(z)
print("z is of type:",type(z))

輸出:

x is of type: <class 'int'>
y is of type: <class 'float'>
20.6
z is of type: <class 'float'>

正如我們所見,“z”的數(shù)據(jù)類型自動(dòng)更改為“float”類型,而一個(gè)變量 x 是整數(shù)類型,而另一個(gè)變量 y 是浮點(diǎn)類型。浮點(diǎn)值沒有被轉(zhuǎn)換為整數(shù)的原因是由于類型提升允許通過將數(shù)據(jù)轉(zhuǎn)換為更廣泛的數(shù)據(jù)類型來執(zhí)行操作而不會丟失任何信息。這是python中隱式類型轉(zhuǎn)換的一個(gè)簡單案例。

顯式類型轉(zhuǎn)換

在 Python 中的顯式類型轉(zhuǎn)換中,數(shù)據(jù)類型由用戶根據(jù)需要手動(dòng)更改。使用顯式類型轉(zhuǎn)換,存在數(shù)據(jù)丟失的風(fēng)險(xiǎn),因?yàn)槲覀儚?qiáng)制在某些特定數(shù)據(jù)類型中更改表達(dá)式。下面解釋了各種形式的顯式類型轉(zhuǎn)換:

1. int(a, base)

此函數(shù)將任何數(shù)據(jù)類型轉(zhuǎn)換為整數(shù)。如果數(shù)據(jù)類型是字符串, 'Base' 指定字符串的基數(shù)。

2. float()

該函數(shù)用于將任何數(shù)據(jù)類型轉(zhuǎn)換為浮點(diǎn)數(shù) 。

# 使用 int()、float() 演示類型轉(zhuǎn)換的 Python 代碼

# 初始化字符串
s = "10010"

# 打印字符串轉(zhuǎn)換為 int base 2
c = int(s,2)
print ("After converting to integer base 2 : ", end="")
print (c)

# 打印字符串轉(zhuǎn)換為浮點(diǎn)數(shù)
e = float(s)
print ("After converting to float : ", end="")
print (e)

輸出:

After converting to integer base 2 : 18
After converting to float : 10010.0

3. ord() : 該函數(shù)用于將字符轉(zhuǎn)換為整數(shù)。

4. hex(): 這個(gè)函數(shù)是將整數(shù)轉(zhuǎn)換為十六進(jìn)制字符串

5. oct() : 這個(gè)函數(shù)是將整數(shù)轉(zhuǎn)換為八進(jìn)制字符串。

# 使用 ord()、hex()、oct() 演示類型轉(zhuǎn)換的 Python 代碼

# 初始化整數(shù)
s = '4'

# 打印字符轉(zhuǎn)換為整數(shù)
c = ord(s)
print ("After converting character to integer : ",end="")
print (c)

# 打印整數(shù)轉(zhuǎn)換為十六進(jìn)制字符串
c = hex(56)
print ("After converting 56 to hexadecimal string : ",end="")
print (c)

# 打印整數(shù)轉(zhuǎn)換為八進(jìn)制字符串
c = oct(56)
print ("After converting 56 to octal string : ",end="")
print (c)

輸出:

After converting character to integer : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70

6. tuple() : 該函數(shù)用于轉(zhuǎn)換為元組。

7. set() : 該函數(shù)返回轉(zhuǎn)換為 set 后的類型。

8. list(): 該函數(shù)用于將任何數(shù)據(jù)類型轉(zhuǎn)換為列表類型。

# 使用 tuple()、set()、list() 演示類型轉(zhuǎn)換的 Python 代碼

# 初始化字符串
s = 'geeks'

# 打印字符串轉(zhuǎn)換為元組
c = tuple(s)
print ("After converting string to tuple : ",end="")
print (c)

# 打印字符串轉(zhuǎn)換為設(shè)置
c = set(s)
print ("After converting string to set : ",end="")
print (c)

# 打印字符串轉(zhuǎn)換為列表
c = list(s)
print ("After converting string to list : ",end="")
print (c)

輸出:

After converting string to tuple : ('g', 'e', 'e', 'k', 's')
After converting string to set : {'k', 'e', 's', 'g'}
After converting string to list : ['g', 'e', 'e', 'k', 's']

9. dict() : 該函數(shù)用于將順序?yàn)?(key,value) 的元組轉(zhuǎn)換為字典

10. str() : 用于將整數(shù)轉(zhuǎn)換為字符串。

11. complex(real,imag) : 此函數(shù)將實(shí)數(shù)轉(zhuǎn)換為復(fù)數(shù)(real,imag)。

# 使用 dict()、complex()、str() 演示類型轉(zhuǎn)換的 Python 代碼

# 初始化整數(shù)
a = 1
b = 2

# 初始化元組
tup = (('a', 1) ,('f', 2), ('g', 3))

# 打印整數(shù)轉(zhuǎn)換為復(fù)數(shù)
c = complex(1,2)
print ("After converting integer to complex number : ",end="")
print (c)

# 打印整數(shù)轉(zhuǎn)換為字符串
c = str(a)
print ("After converting integer to string : ",end="")
print (c)

# 打印元組轉(zhuǎn)換為表達(dá)式字典
c = dict(tup)
print ("After converting tuple to dictionary : ",end="")
print (c)

輸出:

After converting integer to complex number : (1+2j)
After converting integer to string : 1
After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}

12. chr(number): 該函數(shù)將數(shù)字轉(zhuǎn)換為對應(yīng)的ASCII字符。

# 將 ASCII 值轉(zhuǎn)換為字符
a = chr(76)
b = chr(77)

print(a)
print(b)

輸出:

LM 
_

到此這篇關(guān)于Python教程之類型轉(zhuǎn)換詳解的文章就介紹到這了,更多相關(guān)Python類型轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

长兴县| 若尔盖县| 洛南县| 双桥区| 武穴市| 双牌县| 商丘市| 利川市| 郎溪县| 五大连池市| 朝阳市| 洪江市| 清原| 修水县| 体育| 双桥区| 阜康市| 仪征市| 平武县| 柳林县| 鱼台县| 临泽县| 且末县| 巴马| 沛县| 东明县| 嘉鱼县| 武城县| 桃园县| 石渠县| 德州市| 化隆| 台山市| 登封市| 高平市| 金秀| 隆回县| 广丰县| 文登市| 蒲城县| 新郑市|