Python中int()函数的用法浅析

yipeiwu_com6年前Python基础

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

int()函数可以将一个数转化为整数 

>>> int('12',16) 
18  

这里有两个地方要注意:1)12要以字符串的形式进行输入,如果是带参数base的话

2)这里并不是将12转换为16进制的数,而是说12就是一个16进制的数,int()函数将其用十进制数表示,如下

>>> int('0xa',16) 
10 
>>> int('10',8) 
8 

总结

以上所述是小编给大家介绍Python中int()函数的用法浅析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

python统计文本字符串里单词出现频率的方法

本文实例讲述了python统计文本字符串里单词出现频率的方法。分享给大家供大家参考。具体实现方法如下: # word frequency in a text # tested wit...

python模块smtplib实现纯文本邮件发送功能

python模块smtplib实现纯文本邮件发送功能

今天学到了如何使用Python的smtplib库发送邮件,中间也是遇到了各种各样的错误和困难,还好都一一的解决了。下面来谈一谈我的这段经历。 配置你的邮箱 为什么要配置邮箱呢?具体要配置...

python 模拟创建seafile 目录操作示例

本文实例讲述了python 模拟创建seafile 目录操作。分享给大家供大家参考,具体如下: # !/usr/bin/env python # -*- coding: utf-8...

浅谈Python用QQ邮箱发送邮件时授权码的问题

浅谈Python用QQ邮箱发送邮件时授权码的问题

QQ邮箱最新推出了一个授权码,需已验证的手机号向QQ邮箱服务器发送一条短信获得。该授权码用于第三方客户端登录,代替了第三方登录时使用的个人邮箱密码。 在测试过程中遇到两个问题: 1.提示...

Python3一行代码实现图片文字识别的示例

Python3一行代码实现图片文字识别的示例

自学Python3第5天,今天突发奇想,想用Python识别图片里的文字。没想到Python实现图片文字识别这么简单,只需要一行代码就能搞定 from PIL import Imag...