Methods on Integer type in Python

Methods on Integer type 

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. 

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
integer types method in python

Operations on Integer : 

  • Add 
    • n1 + n2 
  • Diffrence 
    • 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<<
methods on integer type-python

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(lengthbyteorder*signed=False)
    • Return an array of bytes representing an integer.
  • From_bytes 
    • n.from_bytes(bytesbyteorder*signed=False
    • Return the integer represented by the given array of bytes.
Example :
#pyhton program 

n1 = 2   
n2 =3
#quotient 
print(‘Quotient :’ , n2 / n1)

#floored quotient
print(‘Floored quotient :’, n2 // n1)

#Remainder
print(‘Remainder :’,n2 % n1) 

#power
print(‘Power 2**3 :’ , 2**3)

#Bitwise or 
print(‘Bitwise OR :’ ,n1 | n2)

#Bitwise Xor 
print(‘Bitwise Xor :’, n1 ^ n2)

#right shit 
print(‘Right sift :’ , n2>>2)

#bit length 
n=34 #100010
print(‘Bit Length :’ ,n.bit_length())



Output : 

Quotient : 1.5
Floored quotient : 1
Remainder : 1
Power 2**3 : 8
Bitwise OR : 3
Bitwise Xor : 1
Right sift : 0
Bit Length : 6