数字内置方法详解(int/long/float/complex)
一、常用方法
1.1、int
以下是Python2.7的int内置函数:
序号 |
函数名 |
作用 |
举例 |
1 |
int.bit_length() |
二进制存储这个整数至少需要多少bit(位)。 |
>>> l.bit_length() 1 >>> l = 2 >>> l.bit_length() 2 >>> bin(2) '0b10' >>> l = 1024 >>> l.bit_length() 11 >>> bin(1024) '0b10000000000' |
2 |
int.conjugate() |
返回复数的共轭复数 |
>>> i = 1 >>> i.conjugate() 1 >>> i = 1+1j >>> i.conjugate() (1-1j) |
3 |
int.denominator |
返回整数分母,整数的分母是1,但是一般和fractions模块的Fraction类的实例结合使用 |
>>> from fractions import Fraction >>> a = Fraction(1,2) >>> a Fraction(1, 2) >>> a.denominator 2 |
4 |
int.imag |
返回整数的虚数部分,如果是整数则返回0 |
>>> i = 1 >>> i.imag 0 >>> i = 1+1j >>> i.imag 1.0 >>> i = 1+2.3j >>> i.imag 2.3 |
5 |
int.mro() |
||
6 |
int.numerator |
返回分数的分母。整数则返回本身。一般和fractions模块的Fraction类的实例结合使用 |
>>> i = 2 >>> i.numerator 2 >>> from fractions import Fraction >>> i = Fraction(2,3) >>> i.numerator 2 |
7 |
int.real |
返回整数的实数部分,如果是整数则返回本身。 |
>>> i = 2 >>> i.real 2 >>> i = 2 + 1j >>> i.real 2.0 |
1.2、long
以下是Python2.7的long内置函数:
序号 |
函数名 |
1 |
long.bit_length() |
2 |
long.conjugate() |
3 |
long.denominator |
4 |
long.imag |
5 |
long.mro() |
6 |
long.numerator |
7 |
long.real |
1.3、float
以下是Python2.7的float内置函数:
序号 |
函数名 |
作用 |
举例 |
1 |
float.as_integer_ratio() |
返回一个由两个整数元素构成的元组。这两个整数元素第一个整数除以第二个整数的商则为这个浮点数。 |
>>> i = 1.5 >>> i.as_integer_ratio() (3, 2) >>> i = 1.3 >>> i.as_integer_ratio() (5854679515581645L, 4503599627370496L) >>> float(5854679515581645/4503599627370496) 1.0 >>> float(5854679515581645)/float(4503599627370496) 1.3 |
2 |
float.conjugate() |
返回共轭浮点数 |
>>> i = 1.4 >>> i.conjugate() 1.4 >>> i = 1.2 +1.4j >>> i.conjugate() (1.2-1.4j) |
3 |
float.fromhex() |
将float.hex()转换的字符串转换成浮点型数字。 |
>>> h = '0x1.8000000000000p+0' >>> f = float.fromhex(h) >>> f 1.5 |
4 |
float.hex() |
把浮点型数字转换为十六进制字符串。 |
>>> f = 1.5 >>> f.hex() '0x1.8000000000000p+0' |
5 |
float.imag |
返回复数的浮点型虚部数值。 |
>>> f = 1.5-2.5j >>> f.imag -2.5 |
6 |
float.is_integer() |
判断浮点型数字是否是整数。如果是则返回True,否则返回False |
>>> f = 1.5 >>> f.is_integer() False >>> f = 2.0 >>> f.is_integer() True |
7 |
float.mro() |
||
8 |
float.real |
返回复数的实部的数值。 |
>>> f = 1.5 >>> f.real 1.5 >>> f = 1.5 + 2.4j >>> f.real 1.5 |
1.4、complex
以下是Python2.7的float内置函数:
序号 |
函数名 |
作用 |
1 |
complex.conjugate() |
返回复数的共轭复数。 |
2 |
complex.imag |
返回复数的虚部数值。 |
3 |
complex.mro() |
|
4 |
complex.real |
返回复数的实部数值。 |
二、所有方法详解
2.1、int
2.2、float
2.3、complex
数字内置方法详解(int/long/float/complex)的更多相关文章
- Python内置方法详解
1. 字符串内置方法详解 为何要有字符串?相对于元组.列表等,对于唯一类型的定义,字符串具有最简单的形式. 字符串往往以变量接收,变量名. 可以查看所有的字符串的内置方法,如: 1> count ...
- for循环与内置方法详解
''' for循环与内置方法详解 ''' # 循环:重复(按照某种规律的)做一件事情 # lt = [1, 2, 3, 4] # # ind = 0 # # while True: # print(l ...
- 序列内置方法详解(string/list/tuple)
一.常用方法集合 1.1.string,字符串常用方法 以下举例是python2.7测试: 函数名称 作用 举例 str.capitalize() 字符串第一个字符如果是字母,则把字母替换为大写字母. ...
- Python_序列对象内置方法详解_String
目录 目录 前言 软件环境 序列类型 序列的操作方法 索引调用 切片运算符 扩展切片运算符 序列元素的反转 连接操作符 重复运算符 成员关系符 序列内置方法 len 获取序列对象的长度 zip 混合两 ...
- Python_List对象内置方法详解
目录 目录 前言 软件环境 列表List 修改列表的元素 插入列表元素 extend 将序列中的元素迭代的附加到list中 insert 在指定的索引号中插入一个元素 删除列表元素 del 删除Lis ...
- python内置常用内置方法详解
# print(locals()) # print(globals()) def func(): x = 1 y = 1 print(locals()) # 函数内部的变量 print(globals ...
- python基础-内置函数详解
一.内置函数(python3.x) 内置参数详解官方文档: https://docs.python.org/3/library/functions.html?highlight=built#ascii ...
- python3 内置函数详解
内置函数详解 abs(x) 返回数字的绝对值,参数可以是整数或浮点数,如果参数是复数,则返回其大小. # 如果参数是复数,则返回其大小. >>> abs(-25) 25 >&g ...
- MYSQL常用内置函数详解说明
函数中可以将字段名当作变量来用,变量的值就是该列对应的所有值:在整理98在线字典数据时(http://zidian.98zw.com/),有这要一个需求,想从多音字duoyinzi字段值提取第一个拼音 ...
随机推荐
- HDU1296 Polynomial Problem
http://acm.hdu.edu.cn/showproblem.php?pid=1296 随手练习 #include <bits/stdc++.h> using namespace s ...
- https://www.safaribooksonline.com/home/
https://www.safaribooksonline.com/home/ https://www.safaribooksonline.com/library/view/instant-sikul ...
- jquery扩展方法详解
http://www.jb51.net/article/51079.htm https://www.cnblogs.com/xuxiuyu/p/5989743.html ---更详细
- 【手撸一个ORM】第九步、orm默认配置类 MyDbConfiguration,一次配置,简化实例化流程
这个实现比较简单,事实上可配置的项目很多,如有需要,请读者自行扩展 using System; namespace MyOrm { public class MyDbConfiguration { p ...
- GUI的最终选择 Tkinter(八):Message组件、Spinbox组件、PanedWindow组件、Toplevel组件
Message组件 Message(消息)组件是Label组件的变体,用于显示多行文本消息,Message组件能够自动执行,并调整文本的尺寸使其适应给定的尺寸. from tkinter import ...
- 2、linux基础知识与技能
2.1.linux内核.发行版linux本身指的是一个操作系统内核,只有内核是无法直接使用的.我们需要的,可以使用的操作系统是一个包含了内核和一批有用的应用程序的一个集合体,这个就叫linux发行版. ...
- docker mysql安装
Docker MySQL-Server 安装1.搜索docker search mysql# 一般会选择mysql-server 版本 2.拉取 docker pull mysql-server 3. ...
- nodejs 实践:express 最佳实践 (一) 项目结构
express 最佳实践 (一) 第二篇: express 最佳实践(二):中间件 最近,一直在使用 nodejs 做项目,对 nodejs 开发可以说深有体会. 先说说 nodejs 在业务中的脚色 ...
- webpack.config.js====CSS相关:css和scss配置loader
1. 安装: //loader加载器加载css和sass模块 cnpm install style-loader css-loader node-sass sass-loader --save-dev ...
- 【踩坑】springMVC 接收String参数没有判断为空
今天在调试iReview项目的接口时,发现新增词条和新增库的时候,某些字段即使留空POST到后台时也能当做不为空. 经过排查,发现后台是使用 String 变量名 == null 这样的语句去判断变量 ...