数字内置方法详解(int/long/float/complex)
一、常用方法
1.1、int
以下是Python2.7的int內(nèi)置函數(shù):
| 序號 | 函數(shù)名 | 作用 | 舉例 |
| 1 | int.bit_length() | 二進制存儲這個整數(shù)至少需要多少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() | 返回復數(shù)的共軛復數(shù) | >>> i = 1 >>> i.conjugate() 1 >>> i = 1+1j >>> i.conjugate() (1-1j) |
| 3 | int.denominator | 返回整數(shù)分母,整數(shù)的分母是1,但是一般和fractions模塊的Fraction類的實例結(jié)合使用 | >>> from fractions import Fraction >>> a = Fraction(1,2) >>> a Fraction(1, 2) >>> a.denominator 2 |
| 4 | int.imag | 返回整數(shù)的虛數(shù)部分,如果是整數(shù)則返回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 | 返回分數(shù)的分母。整數(shù)則返回本身。一般和fractions模塊的Fraction類的實例結(jié)合使用 | >>> i = 2 >>> i.numerator 2 >>> from fractions import Fraction >>> i = Fraction(2,3) >>> i.numerator 2 |
| 7 | int.real | 返回整數(shù)的實數(shù)部分,如果是整數(shù)則返回本身。 | >>> i = 2 >>> i.real 2 >>> i = 2 + 1j >>> i.real 2.0 |
?
1.2、long
以下是Python2.7的long內(nèi)置函數(shù):
| 序號 | 函數(shù)名 |
| 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內(nèi)置函數(shù):
| 序號 | 函數(shù)名 | 作用 | 舉例 |
| 1 | float.as_integer_ratio() | 返回一個由兩個整數(shù)元素構(gòu)成的元組。這兩個整數(shù)元素第一個整數(shù)除以第二個整數(shù)的商則為這個浮點數(shù)。 | >>> 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() | 返回共軛浮點數(shù) | >>> i = 1.4 >>> i.conjugate() 1.4 >>> i = 1.2 +1.4j >>> i.conjugate() (1.2-1.4j) |
| 3 | float.fromhex() | 將float.hex()轉(zhuǎn)換的字符串轉(zhuǎn)換成浮點型數(shù)字。 | >>> h = '0x1.8000000000000p+0' >>> f = float.fromhex(h) >>> f 1.5 |
| 4 | float.hex() | 把浮點型數(shù)字轉(zhuǎn)換為十六進制字符串。 | >>> f = 1.5 >>> f.hex() '0x1.8000000000000p+0' |
| 5 | float.imag | 返回復數(shù)的浮點型虛部數(shù)值。 | >>> f = 1.5-2.5j >>> f.imag -2.5 |
| 6 | float.is_integer() | 判斷浮點型數(shù)字是否是整數(shù)。如果是則返回True,否則返回False | >>> f = 1.5 >>> f.is_integer() False >>> f = 2.0 >>> f.is_integer() True |
| 7 | float.mro() | ? | ? |
| 8 | float.real | 返回復數(shù)的實部的數(shù)值。 | >>> f = 1.5 >>> f.real 1.5 >>> f = 1.5 + 2.4j >>> f.real 1.5 |
?
1.4、complex
以下是Python2.7的float內(nèi)置函數(shù):
| 序號 | 函數(shù)名 | 作用 |
| 1 | complex.conjugate() | 返回復數(shù)的共軛復數(shù)。 |
| 2 | complex.imag | 返回復數(shù)的虛部數(shù)值。 |
| 3 | complex.mro() | ? |
| 4 | complex.real | 返回復數(shù)的實部數(shù)值。 |
二、所有方法詳解
2.1、int
?
2.2、float
?
2.3、complex
轉(zhuǎn)載于:https://www.cnblogs.com/mehome/p/9489706.html
總結(jié)
以上是生活随笔為你收集整理的数字内置方法详解(int/long/float/complex)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让Python中类的属性具有惰性求值的能
- 下一篇: FC及BFC