http://www.tutorialspoint.com/python/python_numbers.htm Copyright © tutorialspoint.com
PYTHON NUMBERS
Number data types store numeric values. They are immutable data types, whichmeans that changing the value of
a number data type results ina newly allocated object.
Number objects are created whenyouassigna value to them. For example:
var1 = 1
var2 = 10
Youcanalso delete the reference to a number object by using the del statement. The syntax of the delstatement
is:
del var1[,var2[,var3[....,varN]]]]
Youcandelete a single object or multiple objects by using the delstatement. For example:
del var
del var_a, var_b
Pythonsupports four different numericaltypes:
int (signed integers): oftencalled just integers or ints, are positive or negative whole numbers with
no decimalpoint.
long (long integers ): or longs, are integers of unlimited size, writtenlike integers and followed by an
uppercase or lowercase L.
float (floating point real values) : or floats, represent realnumbers and are writtenwitha decimal
point dividing the integer and fractionalparts. Floats may also be inscientific notation, withE or e indicating
the power of 10 (2.5e2 = 2.5 x 102 = 250).
complex (complex numbers) : are of the forma + bJ, where a and b are floats and J (or j)
represents the square root of -1 (whichis animaginary number). a is the realpart of the number, and b is
the imaginary part. Complex numbers are not used muchinPythonprogramming.
Examples:
Here are some examples of numbers:
int long float complex
10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j
-0490 535633629843L -90. -.6545+0J
-0x260 -052318172735L -32.54e100 3e+26J
0x69 -4721885298529L 70.2-E12 4.53e-7j
Pythonallows youto use a lowercase L withlong, but it is recommended that youuse only anuppercase L
to avoid confusionwiththe number 1. Pythondisplays long integers withanuppercase L.
A complex number consists of anordered pair of realfloatingpoint numbers denoted by a + bj, where a is
the realpart and b is the imaginary part of the complex number.
Number Type Conversion:
Pythonconverts numbers internally inanexpressioncontaining mixed types to a commontype for evaluation. But
sometimes, you'llneed to coerce a number explicitly fromone type to another to satisfy the requirements of an
operator or functionparameter.
Type int(x)to convert x to a plaininteger.
Type long(x) to convert x to a long integer.
Type float(x) to convert x to a floating-point number.
Type complex(x) to convert x to a complex number withrealpart x and imaginary part zero.
Type complex(x, y) to convert x and y to a complex number withrealpart x and imaginary part y. x and
y are numeric expressions
Mathematical Functions:
Pythonincludes following functions that performmathematicalcalculations.
Function Returns ( description )
abs(x) The absolute value of x: the (positive) distance betweenx and zero.
ceil(x) The ceiling of x: the smallest integer not less thanx
cmp(x, y) -1 if x < y, 0 if x == y, or 1 if x > y
exp(x) The exponentialof x: ex
fabs(x) The absolute value of x.
floor(x) The floor of x: the largest integer not greater thanx
log(x) The naturallogarithmof x, for x> 0
log10(x) The base-10 logarithmof x for x> 0 .
max(x1, x2,...) The largest of its arguments: the value closest to positive infinity
min(x1, x2,...) The smallest of its arguments: the value closest to negative infinity
modf(x) The fractionaland integer parts of x ina two-itemtuple. Bothparts have the same
signas x. The integer part is returned as a float.
pow(x, y) The value of x**y.
round(x [,n]) x rounded to ndigits fromthe decimalpoint. Pythonrounds away fromzero as a
tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.
sqrt(x) The square root of x for x > 0
Random Number Functions:
Randomnumbers are used for games, simulations, testing, security, and privacy applications. Pythonincludes
following functions that are commonly used.
Function Description
choice(seq) A randomitemfroma list, tuple, or string.
randrange ([start,] stop
[,step])
A randomly selected element fromrange(start, stop, step)
random() A randomfloat r, suchthat 0 is less thanor equalto r and r is less than1
seed([x]) Sets the integer starting value used ingenerating randomnumbers. Callthis
functionbefore calling any other randommodule function. Returns None.
shuffle(lst) Randomizes the items of a list inplace. Returns None.
uniform(x, y) A randomfloat r, suchthat x is less thanor equalto r and r is less thany
Trigonometric Functions:
Pythonincludes following functions that performtrigonometric calculations.
Function Description
acos(x) Returnthe arc cosine of x, inradians.
asin(x) Returnthe arc sine of x, inradians.
atan(x) Returnthe arc tangent of x, inradians.
atan2(y, x) Returnatan(y / x), inradians.
cos(x) Returnthe cosine of x radians.
hypot(x, y) Returnthe Euclideannorm, sqrt(x*x + y*y).
sin(x) Returnthe sine of x radians.
tan(x) Returnthe tangent of x radians.
degrees(x) Converts angle x fromradians to degrees.
radians(x) Converts angle x fromdegrees to radians.
Mathematical Constants:
The module also defines two mathematicalconstants:
Constants Description
pi The mathematicalconstant pi.
e The mathematicalconstant e.

Python numbers

  • 1.
    http://www.tutorialspoint.com/python/python_numbers.htm Copyright ©tutorialspoint.com PYTHON NUMBERS Number data types store numeric values. They are immutable data types, whichmeans that changing the value of a number data type results ina newly allocated object. Number objects are created whenyouassigna value to them. For example: var1 = 1 var2 = 10 Youcanalso delete the reference to a number object by using the del statement. The syntax of the delstatement is: del var1[,var2[,var3[....,varN]]]] Youcandelete a single object or multiple objects by using the delstatement. For example: del var del var_a, var_b Pythonsupports four different numericaltypes: int (signed integers): oftencalled just integers or ints, are positive or negative whole numbers with no decimalpoint. long (long integers ): or longs, are integers of unlimited size, writtenlike integers and followed by an uppercase or lowercase L. float (floating point real values) : or floats, represent realnumbers and are writtenwitha decimal point dividing the integer and fractionalparts. Floats may also be inscientific notation, withE or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250). complex (complex numbers) : are of the forma + bJ, where a and b are floats and J (or j) represents the square root of -1 (whichis animaginary number). a is the realpart of the number, and b is the imaginary part. Complex numbers are not used muchinPythonprogramming. Examples: Here are some examples of numbers: int long float complex 10 51924361L 0.0 3.14j 100 -0x19323L 15.20 45.j -786 0122L -21.9 9.322e-36j 080 0xDEFABCECBDAECBFBAEl 32.3+e18 .876j -0490 535633629843L -90. -.6545+0J -0x260 -052318172735L -32.54e100 3e+26J 0x69 -4721885298529L 70.2-E12 4.53e-7j Pythonallows youto use a lowercase L withlong, but it is recommended that youuse only anuppercase L to avoid confusionwiththe number 1. Pythondisplays long integers withanuppercase L.
  • 2.
    A complex numberconsists of anordered pair of realfloatingpoint numbers denoted by a + bj, where a is the realpart and b is the imaginary part of the complex number. Number Type Conversion: Pythonconverts numbers internally inanexpressioncontaining mixed types to a commontype for evaluation. But sometimes, you'llneed to coerce a number explicitly fromone type to another to satisfy the requirements of an operator or functionparameter. Type int(x)to convert x to a plaininteger. Type long(x) to convert x to a long integer. Type float(x) to convert x to a floating-point number. Type complex(x) to convert x to a complex number withrealpart x and imaginary part zero. Type complex(x, y) to convert x and y to a complex number withrealpart x and imaginary part y. x and y are numeric expressions Mathematical Functions: Pythonincludes following functions that performmathematicalcalculations. Function Returns ( description ) abs(x) The absolute value of x: the (positive) distance betweenx and zero. ceil(x) The ceiling of x: the smallest integer not less thanx cmp(x, y) -1 if x < y, 0 if x == y, or 1 if x > y exp(x) The exponentialof x: ex fabs(x) The absolute value of x. floor(x) The floor of x: the largest integer not greater thanx log(x) The naturallogarithmof x, for x> 0 log10(x) The base-10 logarithmof x for x> 0 . max(x1, x2,...) The largest of its arguments: the value closest to positive infinity min(x1, x2,...) The smallest of its arguments: the value closest to negative infinity modf(x) The fractionaland integer parts of x ina two-itemtuple. Bothparts have the same signas x. The integer part is returned as a float. pow(x, y) The value of x**y. round(x [,n]) x rounded to ndigits fromthe decimalpoint. Pythonrounds away fromzero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0. sqrt(x) The square root of x for x > 0 Random Number Functions: Randomnumbers are used for games, simulations, testing, security, and privacy applications. Pythonincludes following functions that are commonly used.
  • 3.
    Function Description choice(seq) Arandomitemfroma list, tuple, or string. randrange ([start,] stop [,step]) A randomly selected element fromrange(start, stop, step) random() A randomfloat r, suchthat 0 is less thanor equalto r and r is less than1 seed([x]) Sets the integer starting value used ingenerating randomnumbers. Callthis functionbefore calling any other randommodule function. Returns None. shuffle(lst) Randomizes the items of a list inplace. Returns None. uniform(x, y) A randomfloat r, suchthat x is less thanor equalto r and r is less thany Trigonometric Functions: Pythonincludes following functions that performtrigonometric calculations. Function Description acos(x) Returnthe arc cosine of x, inradians. asin(x) Returnthe arc sine of x, inradians. atan(x) Returnthe arc tangent of x, inradians. atan2(y, x) Returnatan(y / x), inradians. cos(x) Returnthe cosine of x radians. hypot(x, y) Returnthe Euclideannorm, sqrt(x*x + y*y). sin(x) Returnthe sine of x radians. tan(x) Returnthe tangent of x radians. degrees(x) Converts angle x fromradians to degrees. radians(x) Converts angle x fromdegrees to radians. Mathematical Constants: The module also defines two mathematicalconstants: Constants Description pi The mathematicalconstant pi. e The mathematicalconstant e.