Methods on Integer Type in Python

Methods on Integer Type in Python 

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.

integer types method in python

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
methods on integer type-python

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(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 :

Run

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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription