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+OpenCV实现将图像转换为二进制格式

在学习tensorflow的过程中,有一个问题,tensorflow在训练的过程中读取的是二进制图像数据库文件,而不是图像文件,因此 在进行训练、测试之前需要将图像文件转换为二进制格式。...

pandas Dataframe行列读取的实例

如下所示: import matplotlib.pyplot as plt import tkinter import numpy as np import pandas as...

Django外键(ForeignKey)操作以及related_name的作用详解

Django外键(ForeignKey)操作以及related_name的作用详解

之前已经写过一篇关于Django外键的文章,但是当时并没有介绍如何根据外键对数据的操作,也就是如何通过主表查询子表或者通过子表查询主表的信息 首先我定义了两个模型,一个是老师模型,一个是...

python进行两个表格对比的方法

如下所示: # -*- coding:utf-8 -*- import xlrd import sys import re import json dict1={} dict2={...

一个简单的python程序实例(通讯录)

核心代码: 复制代码 代码如下:#!/usr/bin/python#Filename:friendbook.pyimport cPickle as pimport sysimport t...