python int()函数实例详解

int(x, [base])

功能:

函数的作用是将一个数字或base类型的字符串转换成整数。(  Python视频教程)

函数原型:

int(x=0)

int(x, base=10),base缺省值为10,也就是说不指定base的值时,函数将x按十进制处理。

适用Python版本: 

Python2.x

立即学习“Python免费学习笔记(深入)”;

Python3.x

注意:

1. x 可以是数字或字符串,但是base被赋值后 x 只能是字符串

2. x 作为字符串时必须是 base 类型,也就是说 x 变成数字时必须能用 base 进制表示

Python英文文档解释:

class int(x=0)

class int(x, base=10)

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or – (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int(‘010’, 0) is not legal, while int(‘010’) is, as well as int(‘010’, 8).

The integer type is described in Numeric Types — int, float, complex.

Changed in version 3.4: If base is not an instance of int and the base object has a base.__index__ method, that method is called to obtain an integer for the base. Previous versions used base.__int__ instead of base.__index__.

Changed in version 3.6: Grouping digits with underscores as in code literals is allowed.

代码实例:

1. x 是数字的情况:

  int(3.14)            # 3  int(2e2)             # 200  int(100, 2)          # 出错,base 被赋值后函数只接收字符串

登录后复制

2. x 是字符串的情况:

  int('23', 16)      # 35  int('Pythontab', 8)      # 出错,Pythontab不是个8进制数

登录后复制

3. base 可取值范围是 2~36,囊括了所有的英文字母(不区分大小写),十六进制中F表示15,那么G将在二十进制中表示16,依此类推….Z在三十六进制中表示35

  int('FZ', 16)      # 出错,FZ不能用十六进制表示  int('FZ', 36)      # 575

登录后复制

4. 字符串 0x 可以出现在十六进制中,视作十六进制的符号,同理 0b 可以出现在二进制中,除此之外视作数字 0 和字母 x

  int('0x10', 16)  # 16,0x是十六进制的符号  int('0x10', 17)  # 出错,'0x10'中的 x 被视作英文字母 x  int('0x10', 36)  # 42804,36进制包含字母 x

登录后复制

以上就是python int()函数实例详解的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2273420.html

(0)
上一篇 2025年2月27日 13:25:05
下一篇 2025年2月24日 08:25:52

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 详解python中strip函数的使用场景

    python strip()函数 介绍,需要的朋友可以参考一下 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm)        删除s字符串中开头、结尾处,位于 rm删除序列的字符 s.lstrip(rm)    …

    编程技术 2025年2月27日
    200
  • 详解python中strip()和split()的使用方法

    这篇文章主要介绍了 python strip() 函数和 split() 函数的详解及实例的相关资料,需要的朋友可以参考下  python strip() 函数和 split() 函数的详解及实例 一直以来都分不清楚strip和split的…

    编程技术 2025年2月27日
    200
  • python中装饰器、内置函数、json的详解

    装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象。 先看简单例子: def run(): time.sleep(1)print(‘run….’) 登录…

    编程技术 2025年2月27日
    200
  • python3中round()函数碰到的问题

    python3的round()函数四舍五入取整时,采用最近偶数原则 >>> round(1.5)2>>> round(2.5)2>>> round(3.5)4>>> r…

    编程技术 2025年2月27日
    200
  • 总结python中的一些函数

    python中函数参数有:默认参数、关键字参数、非关键字可变长参数(元组)、关键字可变长参数(字典) :在函数声明时,指定形参的默认值,调用时可不传入改参数(使用默认值)def foo(x): ##默认参数  print ‘x is %s’…

    编程技术 2025年2月27日
    200
  • 什么是回调函数?

    什么是回调函数? 我们绕点远路来回答这个问题。 编程分为两类:系统编程(system programming)和应用编程(application programming)。所谓系统编程,简单来说,就是编写库;而应用编程就是利用写好的各种库来…

    编程技术 2025年2月27日
    200
  • Python:内置函数大全

        Built-in Functions     abs()divmod()input()open()staticmethod()all()enumerate()int()ord()str()any()eval()isinstance(…

    编程技术 2025年2月27日
    200
  • Python函数有哪些?

      我们可以用一段代码来实现我们需要的功能,但是当我们需要重复使用这段代码时,复制粘贴并不是一个酷的方法,我们可以用到函数来实现这一需求 一、函数定义 函数是逻辑结构化和过程化的一种编程方法,通过一个函数名封装好一串用来完成某一特定功能的代…

    2025年2月27日 编程技术
    200
  • Python基础之内置函数和递归详解

    一、内置函数 下面简单介绍几个: 1.abs() 求绝对值 2.all() 如果 iterable 的所有元素都为真(或者如果可迭代为空),则返回 True 立即学习“Python免费学习笔记(深入)”; 3.any() 如果 iterab…

    2025年2月27日
    200
  • Python函数学习的注意要点

    函数式编程 1.简化代码, 2,调用方便,修改方便 3.调用参数,形参数,与位置参数。关键参数,位置参数只能发在关键参数之后 立即学习“Python免费学习笔记(深入)”; 立即学习“Python免费学习笔记(深入)”; 4.默认参数 5.…

    2025年2月27日
    200

发表回复

登录后才能评论