数字内置方法详解(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
轉載于:https://www.cnblogs.com/mehome/p/9489706.html
總結
以上是生活随笔為你收集整理的数字内置方法详解(int/long/float/complex)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让Python中类的属性具有惰性求值的能
- 下一篇: FC及BFC