Python中int()函數的用法淺析


 

int()是Python的一個內部函數 

Python系統幫助里面是這么說的

 
>>> 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
  | long object will be returned instead. 
>>> int ( 12.0
12

int()函數可以將一個數轉化為整數 

>>> int ( '12' , 16
18

這里有兩個地方要注意:1)12要以字符串的形式進行輸入,如果是帶參數base的話

2)這里並不是將12轉換為16進制的數,而是說12就是一個16進制的數,int()函數將其用十進制數表示,如下

?
1
2
3
4
>>> int ( '0xa' , 16
10 
>>> int ( '10' , 8
8

總結

以上所述是給大家介紹Python中int()函數的用法淺析,希望對大家有所幫助,如果大家有任何疑問請給我留言,


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM