欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中的int函數(shù)使用

 更新時(shí)間:2022年11月03日 09:41:23   作者:TCatTime  
這篇文章主要介紹了Python中的int函數(shù)使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

描述

int函數(shù)可以將一個(gè)指定進(jìn)制的數(shù)字型字符串或者十進(jìn)制數(shù)字轉(zhuǎn)化為整形。

語法

int(object, base)
名稱說明備注
object一個(gè)數(shù)字或字符串參數(shù)

1. 數(shù)字參數(shù)可以是整數(shù)、浮點(diǎn)數(shù)(小數(shù)點(diǎn)表示和指數(shù)e表示皆可)

2. 字符串參數(shù)僅能包含在指定進(jìn)制下所涵蓋的字符

3. 該參數(shù)可省略

base進(jìn)制數(shù)

1. 該參數(shù)可省略,省略時(shí)默認(rèn)為10

2. 正整型參數(shù),表示object所對(duì)應(yīng)的進(jìn)制

舉例

1. 浮點(diǎn)數(shù)轉(zhuǎn)化為整型

test = [12.96, -34.21, 12.0e3]
?
for number in test:
? ? print(int(number))

輸出結(jié)果為:

12
-34
12000

注意:無論浮點(diǎn)數(shù)的小數(shù)部分值是什么,使用int( )函數(shù)轉(zhuǎn)化時(shí),只會(huì)保留整數(shù)部分,而將小數(shù)部分舍去。因此在求浮點(diǎn)數(shù)的四舍五入之類的問題時(shí),應(yīng)該避免直接使用int函數(shù)。

2. 二進(jìn)制數(shù)轉(zhuǎn)化為十進(jìn)制數(shù)

test = ['111011011111', '0b101']
?
for number in test:
? ? print(int(number, 2))

輸出結(jié)果為:

3807
5

3. 八進(jìn)制數(shù)轉(zhuǎn)化為十進(jìn)制數(shù)

test = ['-1537202', '0o147']
?
for number in test:
? ? print(int(number, 8))

輸出結(jié)果為:

-441986
103

4. 將十六進(jìn)制數(shù)轉(zhuǎn)化為十進(jìn)制數(shù)

test = ['34A', '0x17']
?
for number in test:
? ? print(int(number, 16))

輸出結(jié)果為:

842
23

5. 布爾值轉(zhuǎn)換為整數(shù)

Python里最簡(jiǎn)單的數(shù)據(jù)類型是布爾型,它只有兩個(gè)可選值:True和False。當(dāng)轉(zhuǎn)換為整數(shù)時(shí),它們分別代表1和0:

>>> int(True)
1
>>> int(False)
0

6. 將整數(shù)字符串轉(zhuǎn)換為整數(shù)

可以將僅包含數(shù)字和正負(fù)號(hào)的字符串轉(zhuǎn)換為整數(shù)。

>>> int('99')
99
>>> int('-23')
-23
>>> int('+12')
12

注意事項(xiàng)

1. 所有參數(shù)都省略時(shí),返回整數(shù)0

test = int()
print(test, type(test))

輸出結(jié)果為:

0 <class 'int'>

2. 試圖將一個(gè)浮點(diǎn)數(shù)字符串轉(zhuǎn)化為十進(jìn)制整數(shù)時(shí),會(huì)報(bào)錯(cuò):

test = '23.1314'
print(int(test))

輸出結(jié)果為:

Traceback (most recent call last):
  File "/Users/Test2.py", line 3, in <module>
    print(int(test))
ValueError: invalid literal for int() with base 10: '23.1314'

返回一個(gè)值報(bào)錯(cuò):對(duì)于函數(shù)int,使用了無效的文字轉(zhuǎn)化成十進(jìn)制:23.1314.

正確的使用方法是,現(xiàn)將浮點(diǎn)數(shù)字符串轉(zhuǎn)化為浮點(diǎn)數(shù)類型,再將浮點(diǎn)數(shù)類型轉(zhuǎn)化為整數(shù)。

test = '23.1314'?
print(int(float(test)))

返回23.

注意:int()函數(shù)可以接受浮點(diǎn)數(shù)或由數(shù)字組成的字符串,但無法接受包含小數(shù)點(diǎn)或指數(shù)的字符串。

3.  base參數(shù)錯(cuò)誤

Python會(huì)自動(dòng)計(jì)算base參數(shù)的使用范圍。若超出范圍會(huì)報(bào)錯(cuò):

test = '110'?
print(int(test, -2))

輸出結(jié)果為:

Traceback (most recent call last):
  File "/Users/Test2.py", line 3, in <module>
    print(int(test, -2))
ValueError: int() base must be >= 2 and <= 36, or 0

根據(jù)object參數(shù)值,Python自動(dòng)計(jì)算出base的合適區(qū)間。

4. 當(dāng)object參數(shù)中存在非法字符時(shí),Python報(bào)錯(cuò)

例如,在八進(jìn)制數(shù)字字符中引入字符‘A’,或者十六進(jìn)制字符中引入字符‘H’

test = '110S'?
print(int(test, 16))

輸出結(jié)果為:

Traceback (most recent call last):
  File "/Users/Test2.py", line 3, in <module>
    print(int(test, 16))
ValueError: invalid literal for int() with base 16: '110S'

5.  二進(jìn)制符號(hào)0b、八進(jìn)制符號(hào)0o、十六進(jìn)制符號(hào)0x加入數(shù)字字符串中對(duì)結(jié)果沒有影響,且可以省略

test_0b = ['0b1011', '1011']
test_0o = ['0o735', '735']
test_0x = ['0xFA', 'FA']
?
for number in test_0b:
? ? print(int(number, 2))
?
for number in test_0o:
? ? print(int(number, 8))
?
for number in test_0x:
? ? print(int(number, 16))

輸出結(jié)果為:

11
11
477
477
250
250

6. 將一個(gè)十進(jìn)制數(shù)轉(zhuǎn)化為十進(jìn)制數(shù),沒有任何報(bào)錯(cuò)也不會(huì)有任何意義

>>> int(5)
5

也可以將一個(gè)十進(jìn)制整數(shù)字符串轉(zhuǎn)化為十進(jìn)制(類型轉(zhuǎn)化)

>>> int('23')
23

7. 自定義進(jìn)制轉(zhuǎn)化為十進(jìn)制

int函數(shù)擁有強(qiáng)大的自定義進(jìn)制轉(zhuǎn)化為十進(jìn)制功能。例如,將17進(jìn)制數(shù)字字符轉(zhuǎn)化為十進(jìn)制數(shù)字:

test_17 = 'GG'
print(int(test_17, 17))

輸出結(jié)果為:

288

8. 合法的數(shù)字字符字母不區(qū)分大小寫

例如在十六進(jìn)制中,A和a都可以轉(zhuǎn)化成十進(jìn)制數(shù),且轉(zhuǎn)化結(jié)果相同。

>>> int('a', 16)
10
>>> int('A', 16)
10

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論