小編給大家分享一下Python中int()函數(shù)的使用方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
int()是Python的一個內(nèi)部函數(shù) ,Python系統(tǒng)幫助里面是這么說的。
>>> help(int)
Help on class int in module __builtin__:
class int(object)
| int(x[, base]) -> integer
|
| Convert a string or number to an integer, if possible. A floating point
| argument will be truncated towards zero (this does not include a string
| representation of a floating point number!) When converting a string, use
| the optional base. It is an error to supply a base when converting a
| non-string. If base is zero, the proper base is guessed based on the
| string content. If the argument is outside the integer range a
| long object will be returned instead.
>>> int(12.0)
12
int()函數(shù)可以將一個數(shù)轉(zhuǎn)化為整數(shù)
>>> int('12',16)
18
這里有兩個地方要注意:
1)12要以字符串的形式進(jìn)行輸入,如果是帶參數(shù)base的話
2)這里并不是將12轉(zhuǎn)換為16進(jìn)制的數(shù),而是說12就是一個16進(jìn)制的數(shù),int()函數(shù)將其用十進(jìn)制數(shù)表示,如下
>>> int('0xa',16)
10
>>> int('10',8)
8
以上是Python中int()函數(shù)的使用方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站設(shè)計公司行業(yè)資訊頻道!