python3.6的數據類型之int類型:
python每個版本的各種數據類型的屬性不太一樣,針對所使用的具體的版本,最好用dir()查看一下該版本下的各種數據類型的屬性有哪些。
int類型:
Jupyter QtConsole 4.2.1
Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
a=9 dir(a) Out[2]: ['__abs__', #a.__abs__()求a的絕對值 '__add__', #a.__add__(2) a+2 '__and__', #與C語言中的按位與一樣,a.__and__(30) Out[10]: 8 先轉化為二進制,再按比特位做與運算得到的結果
'__bool__', #a.__bool__() 零為false,非零為True '__ceil__', #貌似沒什么意義,a.__ceil__() 大於或等於該整數的最小的整數是它自身 '__class__', # a.__class__() 是0,對於float類型的數為0.0 '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__',##a=11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111隨着整數a的位數的變化,a.__sizeof__()的大小在變化 '__str__', '__sub__', ##a.__sub__(d) 即為 a-d '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', #轉化為二進制后的位數a.bit_length() 'conjugate', ##求該數的共軛復數 c=4+3j c.conjugate() (4-3j) 'denominator', ###a.denominator返回1 a的分母是1 注意不用圓括號 'from_bytes', 'imag', ##a.imag 是0,float類型的返回0.0 'numerator', ##a.numerator返回其自身的值9 'real', ##a.real返回9 'to_bytes'] ###a.to_bytes(10,"little") 返回:b'\t\x00\x00\x00\x00\x00\x00\x00\x00\x00'不知道是什么意思 a.to_bytes(3,"big")
d=["a","b","c","d"]
for k,v in enumerate(d,1):
print(k,v)
1 a
2 b
3 c
4 d
十進制轉化為2進制、8進制、16進制
a=123
bin(a)
Out[130]: '0b1111011'
hex(a)
Out[131]: '0x7b'
oct(a)
Out[132]: '0o173'
二進制、8進制、16進制字符串轉化為10進制整數
a=123
b=bin(a)
type(b)
Out[144]: str
int(b,2)
Out[145]: 123
bin(a)
Out[146]: '0b1111011'
a1=int(b,2)
type(a1)
Out[148]: int