Python中的int函数

python帮组文档

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 defines __int__(), int(x) returns x.__int__(). If x defines __trunc__(), it returns x.__trunc__(). 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).

个人理解

  • 返回一个整型数据
  • int()---没有参数,直接返回0
  • int(X)---X任意数值,整数直接返回,小数截断返回
  • base-- 进制
      • 有base参数,X必须是一个字符串
      • 将X按照base进制读取,分析
      • 默认是十进制 如 int(‘10’)
      • 返回十进制数

代码演示

1 int() 2 int(10.9) 3 int(10)

    

  

原文地址:https://www.cnblogs.com/ZhaoLong-study/p/zhaolong.html

时间: 2024-08-08 20:20:41

Python中的int函数的相关文章

Python中的int(x,base)函数

ython中的int函数的第二个参数大家一般都不怎么用到,导致我们出现一个盲区,下面说说int()函数的第二个参数: print int('123') #123 这里int函数默认了base参数值为10,也就是说把字符串'123'视为十进制数转换成十进制数: print int('12345',8) # 5349 这里int函数有了第二个参数base=8:说明要int函数把字符串'12345'视为八进制数转换成十进制数: 原文地址:https://www.cnblogs.com/jeavy/p/

python中有趣的函数

filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返回: >>> def f(x): return x % 2 != 0 and x % 3 != 0 >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23] >>> def f

举例详解Python中的split()函数的使用方法

这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下 函数:split() Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)os.path.split():按照路径将文件名和路径分割开 一.函数说明1.split()函数语法:str.

Python中的getattr()函数详解:

Python中的getattr()函数详解: getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception i

python中的map()函数

MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下. 文档中的介绍在这里: map(function, iterable, ...) Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that man

python中的生成器函数是如何工作的?

以下内容基于python3.4 1. python中的普通函数是怎么运行的? 当一个python函数在执行时,它会在相应的python栈帧上运行,栈帧表示程序运行时函数调用栈中的某一帧.想要获得某个函数相关的栈帧,则必须在调用这个函数且这个函数尚未返回时获取,可能通过inspect模块的currentframe()函数获取当前栈帧. 栈帧对象中的3个常用的属性: f_back : 调用栈的上一级栈帧 f_code: 栈帧对应的c f_locals: 用在当前栈帧时的局部变量; 比如: >>&g

python进阶一(函数式编程)【2-2 python中的map函数】

2-2 python中的map()函数 python中map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 原文地址:https://www.cnblogs.com/ucasljq/p/11609544.html

python进阶一(函数式编程)【2-3 python中的reduce函数】

2-3 python中的reduce函数 python中reduce()函数 reduce()函数也是Python内置的一个高阶函数.reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收两个参数,reduce()对list的每个元素反复调用函数f,并返回最终结果值. 例如,编写一个f函数,接收x和y,返回x和y的和: 1 def f(x, y): 2 return x + y 调用 reduce(f, [

Python中关于Lambda函数的使用总结

lambda表达式是一种匿名函数,对应python中的自定义函数def,是定义某个函数时比较高级的一种写法.作为python初学者,本文整理了lambda的一些基本用法和特点. lambda和def的对应关系 定义func函数,计算给定数x的平方 def func(x): return x*x 等价于 func = lambda x: x*x 其中func是函数名,x是输入参数,x*x是输出结果 输入参数可以有多个,可以接收不定参数如*args或者**kwargs. f = lambda x,