Python笔记(一)——int(),operator比较数值
来源:互联网 发布:windows会话管理器 编辑:程序博客网 时间:2024/05/10 05:06
int()函数:在python3中,替换了原有的long与long()函数,取而代之的是int型能表示更大的整数。int()函数能将参数转换为int类型数值并返回。
int()函数能接收两个参数,一个代表要操作的值,另一个代表进制,下面是Python帮助文档的部分内容:
>>> help(int)Help on class int in module builtins:class int(object) | int(x=0) -> integer | int(x, base=10) -> integer | | Convert a number or string to an integer, 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 the | given base. The literal can be preceded by '+' or '-' and be surrounded | by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. | Base 0 means to interpret the base from the string as an integer literal. | >>> int('0b100', base=0) | 4
从文档可以看出,当有两个参数时,第一个参数必须当作字符串输入,第二个参数是第一个参数原本的进制;若只有一个参数,默认为十进制,有无”都无所谓。无论如何,该函数返回十进制,下面是几个例子:
>>> int(5.23)5>>> int('20')20>>> int('20',2)Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> int('20',2)ValueError: invalid literal for int() with base 2: '20'>>> int('100',2)4>>> int('14',16)20
在Python2以前,经常使用cmp()函数来比较一些数值,但在新版Python中,已经弃用了这种方式。取而代之的是operator模块中增加了以下函数:
operator.lt(a, b) //a<boperator.le(a, b) //a<=boperator.eq(a, b) //a==boperator.ne(a, b) //a!=boperator.ge(a, b) //a>=boperator.gt(a, b) //a>boperator.__lt__(a, b) operator.__le__(a, b) operator.__eq__(a, b) operator.__ne__(a, b) operator.__ge__(a, b) operator.__gt__(a, b)
这几个函数返回都是bool类型,以下是几个例子:
>>> operator.le ('sdss','sssssssss')True>>> operator.lt ('ss','sssss')True>>> operator.ge(3,5)False>>> operator.eq(100,100)True
0 0
- Python笔记(一)——int(),operator比较数值
- 初始化的数值(int、double等)(一)
- 极客班 c++(下)第二周学习笔记 ——重载 operator new & operator delete
- operator int()
- operator++(int)
- operator++() 和 operator++(int)
- Python数据类型(一):数值布尔类型
- Python学习(一) 数值
- python学习笔记(十七)——数值,日期,和时间处理
- Python 标准库——operator
- Python 基础—— operator 模块
- python operator — 标准函数操作
- python operator — 标准函数操作
- Python——数值类型
- 用Groovy实现判断两个int数值大小(不用比较运算符,考虑溢出)
- 黑马学习笔记——operator?:
- Python笔记——(一)
- operator++()和operator++(int) 区别
- AFN简单使用
- 解析方式对比
- CharmingAndroid框架类整理之VPIndicator类——ViewPager的指示器
- 0041 linux 下配置安装mysql以及配置
- Volatile详解
- Python笔记(一)——int(),operator比较数值
- Android 真机投影工具 Vysor 使用教程
- 加密Spring加载的Properties文件
- 第十三章 搜索
- Ubuntu下硬盘读写时声音很响问题的解决
- socket端口与服务
- 前端面试题系列
- (4.6.11)从Android源码到apk——apk打包过程
- HDU 5093 Battle ships 二分图!