An integer in Python, also known as a 'numeric literal', is simply a name used for a numeric value. For this reason, these values are commonly called integers even if they are assigned the value of a real number in the form of an integer and a decimal value. Integers are immutable data types, which means that changing the value of a number data type results in a newly allocated object.
Integer object is created when you assign a integer value to variable, For Ex;
>>> x = 32 >>> y = 55 >>> type(x) <type 'int'> >>> type(y) <type 'int'>
In above example x is variable name and assigned value '32' and checking it’s type using type() function shows 'int'.
 Python has four different types of integers;int : Integers or int's are whole numbers with no decimal points.
 float : Floating point numbers or floats are real numbers with decimal points.
 long : long integers are of unlimited size.
 complex : Complex integers are x + yj, where x and y are floats and j (or J) represents the square root of 1.
Examples of different integer types.
int : 34, 5, 78
float : 34.5, 2.453213, 666.45
long : 7543856L, 349752587L, 9824365768798L
complex : 5.54j, 8843k+4.32, 23+34k+234
>>> x = 34 >>> type(x) <type 'int'>
>>> y = 5333.33 >>> type(y) <type 'float'> >>> z = 2127372373473747574757 >>> type(z) <type 'long'>
>>> w = 323+45.3434J >>> type(w) <type 'complex'>
Python Convert integer type:
Some time we need change the type of integer to make good sense in programming, Python allows to change the type of integer as shown below;
 To convert any integer type to plain 'int' use int() function as shown in example;
>>> x = 3.14 >>> int(x) 3
 To convert any integer type to float use float() function as shown in example;
>>> x = 3 >>> float(x) 3.0
 To convert any integer type to long number use long() function as shown in example;
>>> x = 3 >>> long(x) 3L
 To convert any integer type to complex number use complex() function as shown in example;
>>> x = 3 >>> complex(x) (3+0j)
Python mathematical operations for integers:
Python has builtin library called 'math' to perform mathematical operations. Following table contains the list of all mathematical functions and example of each function is given below.
Math Function  Description 

abs(x)  The positive distance between x and zero, i.e the absolute value of x. 
ceil(x)  The ceiling of x: the smallest integer not less than x. 
cmp(x, y)  Returns 1 if x < y, Returns 0 if x == y, Returns 1 if x > y. 
exp(x)  The exponential of x. 
fabs(x)  The absolute value of x. 
floor(x)  The floor of x: the largest integer not greater than x. 
log(x)  The natural logarithm of x, for x> 0. 
log10(x)  The base10 logarithm of x for x> 0. 
max(x1, x2,…)  The largest of it’s arguments. 
min(x1, x2,…)  The smallest of it’s arguments. 
modf(x)  The fractional and integer parts of x in a twoitem tuple. Both parts have the same sign as x. The integer part is returned as a float. 
pow(x, y)  The value of x**y. 
round(x [,n])  x rounded to n digit’s from the decimal point. Python rounds away from zero as a tiebreaker: round(0.5) is 1.0 and round(0.5) is 1.0. 
sqrt(x)  The square root of x for x > 0 
Examples of Above functions:
 abs(x) Returns the positive distance between x and zero, i.e the absolute value of x.
>>> x = 33.56 >>> abs(x) 33.56
 ceil(x) Returns the ceiling of x: the smallest integer not less than x.
>>> from math import ceil >>> x = 55.23 >>> ceil(x) 56.0
 cmp(x, y) Returns 1 if x < y, Returns 0 if x == y, Returns 1 if x > y.
>>> x = 2 >>> y = 4 >>> cmp(x, y) 1
 exp(x) The exponential of x.
>>> from math import exp >>> x = 55.32 >>> exp(x) 1.0596702429230294e+24
 fabs(x) The absolute value of x.
>>> from math import fabs >>> x = 55.32 >>> fabs(x) 55.32
 floor(x) The floor of x: the largest integer not greater than x.
>>> from math import floor >>> x = 55.32 >>> floor(x) 55.0
 log(x) The natural logarithm of x, for x> 0.
>>> from math import log >>> x = 2 >>> log(x) 0.6931471805599453
 log10(x) The base10 logarithm of x for x> 0.
>>> from math import log10 >>> x = 2 >>> log10(x) 0.3010299956639812
 max(x1, x2,..) The largest of it’s arguments.
>>> max(0,1,2,3,4,5,6,7,8,9) 9

min(x1, x2,…) The smallest of it’s arguments.
>>> min(0,1,2,3,4,5,6,7,8,9) 0
 modf(x) The fractional and integer parts of x in a twoitem tuple. Both parts have the same sign as x. The integer part is returned as a float.
>>> from math import modf >>> x = 2 >>> modf(2) (0.0, 2.0)

pow(x, y) The value of x**y.
>>> from math import pow >>> x = 2 >>> y = 4 >>> pow(x, y) 16.0
 round(x [,n]) x rounded to n digit’s from the decimal point. Python rounds away from zero as a tiebreaker: round(0.5) is 1.0 and round(0.5) is 1.0.
>>> x = 33.22 >>> round(x) 33.0
 sqrt(x) The square root of x for x > 0.
>>> from math import sqrt >>> x = 4 >>> sqrt(x) 2.0
Trigonometric Functions for Integers:
Python math library includes few trigonometric functions which are useful for mathematical operations;
Functions  Description 

acos(x)  Return the arc cosine of x, in radians. 
asin(x)  Return the arc sine of x, in radians. 
atan(x)  Return the arc tangent of x, in radians. 
atan2(y, x)  Return atan(y / x), in radians. 
cos(x)  Return the cosine of x radians. 
hypot(x, y)  Return the Euclidean norm, sqrt(x*x + y*y). 
sin(x)  Return the sine of x radians. 
tan(x)  Return the tangent of x radians. 
degrees(x)  Converts angle x from radians to degrees. 
radians(x)  Converts angle x from degrees to radians. 
Examples of above functions;
 acos(x) Return the arc cosine of x, in radians.
>>> from math import acos >>> x = 0 >>> acos(x) 1.5707963267948966
 asin(x) Return the arc sine of x, in radians.
>>> from math import asin >>> x = 1 >>> asin(x) 1.5707963267948966
 atan(x) Return the arc tangent of x, in radians.
>>> from math import atan >>> x = 1 >>> atan(x) 0.7853981633974483
 cos(x) Return the cosine of x radians.
>>> from math import cos >>> x = 1 >>> cos(x) 0.5403023058681398
 hypot(x, y) Return the Euclidean norm, sqrt(x*x + y*y).
>>> from math import hypot >>> x = 2 >>> y = 3 >>> hypot(x, y) 3.605551275463989
 sin(x) Return the sine of x radians.
>>> from math import sins >>> x = 1 >>> sin(x) 0.8414709848078965
 tan(x) Return the tangent of x radians.
>>> from math import tan >>> x = 1 >>> tan(x) 1.5574077246549023
 degrees(x) Converts angle x from radians to degrees.
>>> from math import degrees >>> x = 1 >>> degrees(x) 57.29577951308232
 radians(x) Converts angle x from degrees to radians.
>>> from math import radians >>> x = 1 >>> radians(x) 0.017453292519943295
 pi Mathematical constant pi
>>> from math import pi >>> pi 3.141592653589793
 e Mathematical constant pi
>>> from math import e >>> e 2.718281828459045
Recent Comments