Copyright © tutorialspoint.com

Python - Numbers

previous next


Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.

Number objects are created when you assign a value to them. For example:

var1 = 1
var2 = 10

You can also delete the reference to a number object by using the del statement. The syntax of the del statement is:

del var1[,var2[,var3[....,varN]]]]

You can delete a single object or multiple objects by using the del statement. For example:

del var
del var_a, var_b

Python supports four different numerical types:

Examples:

Here are some examples of numbers:

intlongfloatcomplex
1051924361L0.03.14j
100-0x19323L15.2045.j
-7860122L-21.99.322e-36j
0800xDEFABCECBDAECBFBAEl32.3+e18.876j
-0490535633629843L-90.-.6545+0J
-0x260-052318172735L-32.54e1003e+26J
0x69-4721885298529L70.2-E124.53e-7j

Number Type Conversion:

Python converts numbers internally in an expression containing mixed types to a common type for evaluation. But sometimes, you'll need to coerce a number explicitly from one type to another to satisfy the requirements of an operator or function parameter.

Built-in Number Functions:

Mathematical Functions:

Python includes following functions that perform mathematical calculations.

FunctionReturns ( description )
abs(x)The absolute value of x: the (positive) distance between x and zero.
ceil(x) The ceiling of x: the smallest integer not less than x
cmp(x, y)-1 if x < y, 0 if x == y, or 1 if x > y
exp(x) The exponential of x: ex
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 base-10 logarithm of 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 fractional and integer parts of x in a two-item 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 digits from the decimal point. Python rounds away from zero 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:

Random numbers are used for games, simulations, testing, security, and privacy applications. Python includes following functions that are commonly used.

FunctionReturns ( description )
choice(seq)A random item from a list, tuple, or string.
randrange ([start,] stop [,step]) A randomly selected element from range(start, stop, step)
random() A random float r, such that 0 is less than or equal to r and r is less than 1
seed([x]) Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.
shuffle(lst) Randomizes the items of a list in place. Returns None.
uniform(x, y)A random float r, such that x is less than or equal to r and r is less than y

Trigonometric Functions:

Python includes following functions that perform trigonometric calculations.

FunctionDescription
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.

Mathematical Constants:

The module also defines two mathematical constants:

ConstantDescription
piThe mathematical constant pi.
eThe mathematical constant e.

previous next

Copyright © tutorialspoint.com