Integers in Python

Integers in Python

Let’s see the methods of the int data:

>>> dir(int)
[‘__abs__’, ‘__add__’, ‘__and__’, ‘__bool__’, ‘__ceil__’, ‘__class__’, ‘__delatt
r__’, ‘__dir__’, ‘__divmod__’, ‘__doc__’, ‘__eq__’, ‘__float__’, ‘__floor__’, ‘_
_floordiv__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getnewargs__’, ‘__g
t__’, ‘__hash__’, ‘__index__’, ‘__init__’, ‘__int__’, ‘__invert__’, ‘__le__’, ‘_
_lshift__’, ‘__lt__’, ‘__mod__’, ‘__mul__’, ‘__ne__’, ‘__neg__’, ‘__new__’, ‘__o
r__’, ‘__pos__’, ‘__pow__’, ‘__radd__’, ‘__rand__’, ‘__rdivmod__’, ‘__reduce__’,
 ‘__reduce_ex__’, ‘__repr__’, ‘__rfloordiv__’, ‘__rlshift__’, ‘__rmod__’, ‘__rmu
l__’, ‘__ror__’, ‘__round__’, ‘__rpow__’, ‘__rrshift__’, ‘__rshift__’, ‘__rsub__
‘, ‘__rtruediv__’, ‘__rxor__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__sub__’
, ‘__subclasshook__’, ‘__truediv__’, ‘__trunc__’, ‘__xor__’, ‘bit_length’, ‘conj
ugate’, ‘denominator’, ‘from_bytes’, ‘imag’, ‘numerator’, ‘real’, ‘to_bytes’]
>>>

Not many, but they are important.

 int.bit_length() -> int

>>> w = 4

>>> bin(4)
‘0b100’
>>> bin(w)
‘0b100’
>>> w.bit_length()
3
>>>

>>> v = 2345
>>> bin(2345)
‘0b100100101001’
>>> bin(v)
‘0b100100101001’
>>> v.bit_length()
12
>>>

int.conjugate() -> int

>>> w.conjugate()
4
>>> v.conjugate()
2345
>>>

int.denominator -> int

>>> w.denominator
1
>>>

int.numerator -> int

>>> w.numerator
4
>>>

 int.from_bytes(bytes, byteorder, *, signed=False) -> int

>>> x = [1,2,3]
>>> w.from_bytes(x, ‘little’)
197121
>>> w.from_bytes(x, ‘big’)
66051
>>>

>>> v = 234
>>> v.from_bytes(x, ‘little’)
197121
>>> v.from_bytes(x, ‘big’)
66051
>>>

>>> y = (6,7,8,9)
>>> w.from_bytes(y, ‘little’)
151521030
>>> w.from_bytes(y, ‘big’)
101124105
>>> v.from_bytes(y, ‘little’)
151521030
>>> v.from_bytes(y, ‘big’)
101124105
>>>

Return the integer represented by the given array of bytes.

The bytes argument must either support the buffer protocol or be an
iterable object producing bytes.  Bytes and bytearray are examples of
built-in objects that support the buffer protocol.

The byteorder argument determines the byte order used to represent the
integer.  If byteorder is ‘big’, the most significant byte is at the
beginning of the byte array.  If byteorder is ‘little’, the most
significant byte is at the end of the byte array.  To request the native
byte order of the host system, use `sys.byteorder’ as the byte order value.

The signed keyword-only argument indicates whether two’s complement is
used to represent the integer.

w.imag

>>> w.imag
0
>>>

>>> v.imag
0
>>>

int.real

>>> w.real
4
>>> v.real
234
>>>

 int.to_bytes(length, byteorder, *, signed=False) -> bytes

>>> w.to_bytes(v, ‘little’)
b’\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00′
>>> w.to_bytes(v, ‘big’)
b’\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04′
>>> v.to_bytes(w, ‘little’)
b’\xea\x00\x00\x00′
>>> v.to_bytes(w, ‘big’)
b’\x00\x00\x00\xea’
>>>

Return an array of bytes representing an integer.

The integer is represented using length bytes.  An OverflowError is
raised if the integer is not representable with the given number of
bytes.

The byteorder argument determines the byte order used to represent the
integer.  If byteorder is ‘big’, the most significant byte is at the
beginning of the byte array.  If byteorder is ‘little’, the most
significant byte is at the end of the byte array.  To request the native
byte order of the host system, use `sys.byteorder’ as the byte order value.

The signed keyword-only argument determines whether two’s complement is
used to represent the integer.  If signed is False and a negative integer
is given, an OverflowError is raised.

Leave a comment