Python Numbers

Python Numbers

June 14, 2018
11 minutes read time
1 likes
0 comments
424 times viewed


header image

Description

This article will guide you about the use of numbers in python. It will explain various functionalities and builtin functions associated with it.

Content

Numbers

Type Conversion

int(x)

Convert x to a plain integer

>>> int(3.45)
3
>>> int("34")
34

float(x)

Convert x to a floating-point number

>>> float(34)
34.0
>>> float("123.45")
123.45

complex(x, y)

Convert x and y to a complex number with real part x and imaginary part y. x and y are numeric expressions.

Note: if y is not provided then y or imaginary part is considered 0.

>>> complex(23)
(23+0j)
>>> complex(2,5)
(2+5j)

chr(x)

Converts an integer to a character.

>>> chr(65)
'A'
>>> chr(97)
'a'

hex(x)

Converts an integer to a hexadecimal string.

>>> hex(16)
'0x10'
>>> hex(34)
'0x22'

oct(x)

Converts an integer to an octal string.

>>> oct(8)
'0o10'
>>> oct(20)
'0o24'

Mathematical Functions

abs(x)

The absolute value of x: the (positive) distance between x and zero

>>> abs(12.45)
12.45
>>> abs(-32)
32

ceil(x)

The ceiling of x: the smallest integer not less than x

>>> import math
>>> math.ceil(-12.75)
-12
>>> math.ceil(18.12)
19
>>> math.ceil(95.98)
96
>>> math.ceil(math.pi)
4

exp(x)

The exponential of x: ex

>>> import math
>>> math.exp(1.2)
3.3201169227365472
>>> math.exp(-4)
0.01831563888873418

floor(x)

The floor of x: the largest integer not greater than x

>>> import math
>>> math.floor(-12.75)
-13
>>> math.floor(15.32)
15
>>> math.floor(20.95)
20
>>> math.floor(math.pi)
3

log(x)

The natural logarithm of x, for x > 0 i.e ln(x)

>>> import math
>>> math.log(45)
3.8066624897703196

log10(x)

The base-10 logarithm of x for x > 0

>>> import math
>>> math.log10(1000)
3.0
>>> math.log10(56)
1.7481880270062005

max(x1, x2,…)

The largest of its arguments: the value closest to positive infinity

>>> max(10, 32, -45, 90, 29, 67)
90

min(x1, x2,…)

The smallest of its arguments: the value closest to negative infinity

>>> min(10, 32, -45, 90, 29, 67)
-45

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.

>>> import math
>>> math.modf(27.12)
(0.120000000000001, 27.0)
>>> math.modf(34.72)
(0.7199999999999989, 34.0)
>>> math.modf(math.pi)
(0.14159265358979312, 3.0)

pow(x, y)

The value of x**y.

>>> import math
>>> math.pow(100, 2)
10000.0
>>> math.pow(100, -2)
0.0001
>>> math.pow(2, 4)
16.0
>>> math.pow(3, 0)
1.0

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

>>> round(70.23456)
70
>>> round(90.54)
91
>>> round(56.659,1)
56.7
>>> round(80.264, 2)
80.26
>>> round(100.000056, 3)
100.0
>>> round(-100.000056, 3)
-100.0

sqrt(x)

The square root of x for x > 0

>>> import math
>>> math.sqrt(100)
10.0
>>> math.sqrt(7)
2.6457513110645907

Random Number Functions

choice(seq)

A random item from a list, tuple, or string

>>> import random
>>> random.choice(range(100))
76
>>> random.choice([1, 2, 3, 5, 9])
1
>>> random.choice('Hello World')
'l'

randrange ([start,] stop [,step])

A randomly selected element from range(start, stop, step)

>>> import random
>>> random.randrange(1, 100, 2) # Randomly choose odd number
85
>>> random.randrange(100)
67
>>> random.randrange(100)
72

random()

A random float r, such that 0 is less than or equal to r and r is less than 1

>>> import random
>>> random.random()
0.196397224741498
>>> random.random()
0.7937653884184298

shuffle(lst)

Randomizes the items of a list in place. Returns None.

>>> import random
>>> list = [20, 16, 10, 5]
>>> random.shuffle(list)
>>> list
[5, 16, 10, 20]
>>> random.shuffle(list)
>>> list
[10, 5, 16, 20]

uniform(x, y)

A random float r, such that x is less than or equal to r and r is less than y

>>> import random
>>> random.uniform(5, 10)
8.651180490478254
>>> random.uniform(7, 14)
10.700635491471733
>>> random.uniform(7, 14)
11.845388203697608

Trigonometric Functions

degrees(x)

Converts angle x from radians to degrees.

>>> import math
>>> math.degrees(3)
171.88733853924697
>>> math.degrees(-3)
-171.88733853924697
>>> math.degrees(0)
0.0
>>> math.degrees(math.pi)
180.0
>>> math.degrees(math.pi/2)
90.0
>>> math.degrees(math.pi/4)
45.0

radians(x)

Converts angle x from degrees to radians.

>>> import math
>>> math.radians(math.pi)
0.05483113556160755
>>> math.radians(180)
3.141592653589793
>>> math.radians(360)
6.283185307179586

hypot(x, y)

Return the Euclidean norm, sqrt(x*x + y*y)

>>> import math
>>> math.hypot(3, 4)
5.0
>>> math.hypot(-3, 3)
4.242640687119285
>>> math.hypot(0, 2)
2.0

sin(x)

Return the sine of x radians

>>> import math
>>> math.sin(math.pi/4)
0.7071067811865475
>>> math.sin(math.pi/2)
1.0
>>> math.sin(3)
0.1411200080598672

cos(x)

Return the cosine of x radians.

>>> import math
>>> math.cos(math.pi/4)
0.7071067811865476
>>> math.cos(math.pi)
-1.0

tan(x)

Return the tangent of x radians.

>>> import math
>>> math.tan(math.pi/4)
0.9999999999999999
>>> math.tan(math.pi/2)
1.633123935319537e+16
>>> math.tan(math.pi)
-1.2246467991473532e-16
>>> math.tan(3*math.pi/4)
-1.0000000000000002

asin(x)

Return the arc sine of x, in radians.

>>> import math
>>> math.asin(1)
1.5707963267948966

acos(x)

Return the arc cosine of x, in radians.

>>> import math
>>> math.acos(1)
0.0
>>> math.acos(0)
1.5707963267948966

atan(x)

Return the arc tangent of x, in radians.

>>> import math
>>> math.atan(1)
0.7853981633974483
>>> math.atan(3)
1.2490457723982544

atan2(y, x)

Return atan(y / x), in radians

>>> import math
>>> math.atan2(-0.50,-0.50)
-2.356194490192345
>>> math.atan2(0.50,0.50)
0.7853981633974483
>>> math.atan2(5,5)
0.7853981633974483
>>> math.atan2(-10,10)
-0.7853981633974483
>>> math.atan2(10,20)
0.4636476090008061

Mathematical Constants

pi

The mathematical constant pi

>>> import math
>>> math.pi
3.141592653589793

e

The mathematical constant e

>>> import math
>>> math.e
2.718281828459045

Others

a**b

Exponent − Performs exponential (power) calculation on operators.

>>> a=2
>>> b=5
>>> a**b
32

a//b

Floor Division − The division of operands where the result is the quotient in which the digits after the decimal point are removed.

>>> 15//4
3

x is y

Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y).



We are done



Like this post

1   Like


Share this post on

Google Plus LinkedIn


About the author




Join the discussion

Nothing to preview

Post Comment



Comments