新聞中心
Python int()使用小結(jié)

在下城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作按需定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都全網(wǎng)營銷推廣,外貿(mào)網(wǎng)站制作,下城網(wǎng)站建設(shè)費用合理。
int()的基本語法格式是int(x,[base=10]),其中base可以省略
int()的作用是把不同進制的數(shù)字或數(shù)字字符串轉(zhuǎn)為十進制整數(shù)。使用中,其行為,參數(shù)有一些tricky,需要特別注意。
不帶參數(shù)返回0,即int()
>>> int() 0
取整是簡單截斷,不是四舍五入,如int(1.5) = 1
>>> int(1.5) 1
參數(shù)可以是整數(shù),浮點數(shù),或算術(shù)表達式如100/3,但不能是復(fù)數(shù),如1+2j
>>> int(3) 3 >>> int(3.5) 3 >>> int(100/3) 33 >>> int(1+2j) Traceback (most recent call last): File "", line 1, in int(1+2j) TypeError: can't convert complex to int
數(shù)字字符串可以是整數(shù)字符串如’123’,但不能是算術(shù)表達式字符串如’100/3’,或字符形式的浮點數(shù)如’1.5’
>>> int('123')
123
>>> int(100/3)
33
>>> int('100/3')
Traceback (most recent call last):
File "", line 1, in
int('100/3')
ValueError: invalid literal for int() with base 10: '100/3'
>>> int('1.5')
Traceback (most recent call last):
File "", line 1, in
int('1.5')
ValueError: invalid literal for int() with base 10: '1.5' base缺省值是10,表示十進制,如果包括base參數(shù),則前面的x必須是符合當(dāng)前進制的數(shù)字字符串
此時int的作用是把base進制代表的數(shù)字字符串x,轉(zhuǎn)換為10進制數(shù)
>>> int('45',8)# 把8進制'45'轉(zhuǎn)換為十進制數(shù)37
37
>>> int('ab',16) #
171
>>> int(45,8)
Traceback (most recent call last):
File "", line 1, in
int(45,8)
TypeError: int() can't convert non-string with explicit base
>>> int(ab,16)
Traceback (most recent call last):
File "", line 1, in
int(ab,16)
NameError: name 'ab' is not defined 本文轉(zhuǎn)載自:https://blog.csdn.net/
標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:Pythonint()使用小結(jié)
本文路徑:http://www.dlmjj.cn/article/dphohse.html


咨詢
建站咨詢
