In Python, There are three distinct numeric types : integers , floating point number and complex numbers. In addition, Booleans are a subtype of integers. Integers have unlimited precision. Integer data types (int) have several built-in methods and operations that you can use to manipulate and perform various operations on integer values.
Methods on Integer Type in Python
Integer have several built-in methods. The constructors int() , float() and complex() used to produce a specific type of numbers.
All numeric type except complex number supports various operations on themselves.
Example :
n1=5 , n2=7
Add operation n1 + n2
power of (n1,n2) i.e. 5**7
Operations on Integer :
Add
n1 + n2
Difference
n1 – n2
Multiplication
n1 * n2
Division
n1 / n2 (quotient)
Floored quotient
n1 // n2
Remainder of n1 / n2
n1 % n2
Absolute value
abs ( n )
Divmod
divmod( n1, n2)
( n1//n2, n1 % n2 )
Power of Integer
pow ( n1 , n2) or n1**n2
Binary Operations :
Bitwise OR
n1 | n2
Bitwise AND
n1 & n2
Bitwise NOT
~n1
Bitwise XOR
n1 ^ n2
Bitwise Right Shift
n >>
Bitwise Left Shift
n<<
Additional Methods on Integer :
The int type implements the numbers.Integral abstract base class.
Bit length
n.bit_length( ) #returns the bit length of binary repersentation of a number.
More precisely, if n is nonzero, then n.bit_length() is the unique positive integer x such that 2**(x – 1) <= abs(n) < 2**x .
To_bytes
n.to_bytes(length, byteorder, *, signed=False)
Return an array of bytes representing an integer.
From_bytes
n.from_bytes(bytes, byteorder, *, signed=False)
Return the integer represented by the given array of bytes.
Login/Signup to comment