網(wǎng)站首頁 編程語言 正文
描述
int函數(shù)可以將一個指定進(jìn)制的數(shù)字型字符串或者十進(jìn)制數(shù)字轉(zhuǎn)化為整形。
語法
int(object, base)
名稱 | 說明 | 備注 |
object | 一個數(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ù)可省略,省略時默認(rèn)為10 2. 正整型參數(shù),表示object所對應(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ù)部分,而將小數(shù)部分舍去。因此在求浮點(diǎn)數(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里最簡單的數(shù)據(jù)類型是布爾型,它只有兩個可選值:True和False。當(dāng)轉(zhuǎn)換為整數(shù)時,它們分別代表1和0:
>>> int(True)
1
>>> int(False)
0
6. 將整數(shù)字符串轉(zhuǎn)換為整數(shù)
可以將僅包含數(shù)字和正負(fù)號的字符串轉(zhuǎn)換為整數(shù)。
>>> int('99')
99
>>> int('-23')
-23
>>> int('+12')
12
注意事項(xiàng)
1. 所有參數(shù)都省略時,返回整數(shù)0
test = int()
print(test, type(test))
輸出結(jié)果為:
0 <class 'int'>
2. 試圖將一個浮點(diǎn)數(shù)字符串轉(zhuǎn)化為十進(jìn)制整數(shù)時,會報錯:
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'
返回一個值報錯:對于函數(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ù)錯誤
Python會自動計算base參數(shù)的使用范圍。若超出范圍會報錯:
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自動計算出base的合適區(qū)間。
4. 當(dāng)object參數(shù)中存在非法字符時,Python報錯
例如,在八進(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)制符號0b、八進(jìn)制符號0o、十六進(jìn)制符號0x加入數(shù)字字符串中對結(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. 將一個十進(jìn)制數(shù)轉(zhuǎn)化為十進(jìn)制數(shù),沒有任何報錯也不會有任何意義
>>> int(5)
5
也可以將一個十進(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
原文鏈接:https://blog.csdn.net/TCatTime/article/details/82826824
相關(guān)推薦
- 2022-09-28 Dephi逆向工具Dede導(dǎo)出函數(shù)名MAP導(dǎo)入到IDA中的實(shí)現(xiàn)方法_python
- 2023-01-29 Python安裝Talib庫的詳細(xì)圖文教程_python
- 2022-08-23 python使用redis模塊來跟redis實(shí)現(xiàn)交互_python
- 2022-09-20 Springboot整合Redis與數(shù)據(jù)持久化_Redis
- 2022-04-20 Python設(shè)計模式創(chuàng)建型原型模式_python
- 2022-09-02 基于Python實(shí)現(xiàn)配置熱加載的方法詳解_python
- 2022-03-14 跨域:Response to preflight request doesn t pass acce
- 2022-12-09 Python字符串本身作為bytes進(jìn)行解碼的問題_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支