Professional Documents
Culture Documents
Mathematical, Trigonometric
and Random Number Functions
Mathematical Functions
• To use mathematical functions, import math module using
• import math
math.sqrt(4)
OR
• import math as ma
ma.sqrt(4)
OR
>>> exp(3)
exp(x) Returns e**x
20.085536923187668
>>> expm1(3)
expm1(x) Returns e**x - 1
19.085536923187668
>>> sqrt(66)
sqrt(x) Returns the square root of x
8.12403840463596
>>> isinf(-9)
False
Returns True if x is a positive
isinf(x) >>> isinf(float('inf’)) (or)
or negative infinity
isinf(math.inf)
True
Python EE Unit-1 By Dr.Judith Leo , HITS 19-02-2018
>>> acos(0.7)
acos(x) Returns the arc cosine of x
0.7953988301841436
>>> asin(-0.5)
asin(x) Returns the arc sine of x
-0.5235987755982989
>>> atan(0.5)
atan(x) Returns the arc tangent of x
0.4636476090008061
>>> atan2(6,4)
atan2(y, x) Returns arc tangent of (y / x)
0.982793723247329
>>> cos(90)
cos(x) Returns the cosine of x
-0.4480736161291701
>>> sin(90)
sin(x) Returns the sine of x
0.8939966636005579
>>> tan(85)
tan(x) Returns the tangent of x
0.17887017243876716
>>> asinh(3.4)
asinh(x) Returns the inverse hyperbolic sine of x
1.9378792776645006
>>> cosh(3.4)
cosh(x) Returns the hyperbolic cosine of x
14.998736658678668
>>> sinh(3.4)
sinh(x) Returns the hyperbolic sine of x
14.965363388718341
>>> tanh(3.4)
tanh(x) Returns the hyperbolic tangent of x
0.9977749279342794
>>> gamma(3)
gamma(x) Returns the Gamma function at x
2.0
>>> log(5)
Returns the logarithm of x to the base 1.6094379124341003
log(x[, base])
(defaults to e) >>> log(5,10)
0.6989700043360187
>>> log1p(5)
log1p(x) Returns the natural logarithm of 1+x
1.791759469228055
>>> log10(5)
log10(x) Returns the base-10 logarithm of x
0.6989700043360189
Converts angle x from radians to >>> degrees(1.4)
degrees(x)
degrees 80.21409131831524
>>> e
e mathematical constant e (2.71828...)
2.718281828459045
• import random
random.randint(0,14)
OR
• import random as ra
ra.randint(0,14)
OR
(i) random()
• Returns the random floating point number in the range [0.0, 1.0].
Example
>>> import random as ra
>>> ra.random()
0.4246139814723596
>>> ra.random()
0.6444914256429657
(ii) randint(a, b)
Example
>>>import random as ra
>>> ra.randint(50,120)
80
>>> ra.randint(50,120)
91
Python EE Unit-1 By Dr.Judith Leo , HITS 19-02-2018
(iii) uniform(a, b)
Example
>>>import random as ra
>>> ra.uniform(50,120)
64.42687624976512
>>> ra.uniform(50,120)
106.9324875950124
Example
>>> import random as ra
>>> ra.randrange(0,60)
33
>>> ra.randrange(0,60,5)
55
>>> ra.randrange(100, 1000, 2) # Select an even number in 100 <= number < 1000
912
Python EE Unit-1 By Dr.Judith Leo , HITS 19-02-2018
(v) choice(seq)
Returns a random element from the non-empty sequence seq (list, tuple, or string)
Example
>>> import random as ra
>>> ra.choice([10,20,30,40])
10
>>> ra.choice([10,20,30,40])
30
>>> ra.choice('Hello')
'o'
(v) shuffle(x)
Example
>>>import random as ra
>>> a=[50,'apple',4.5]
>>> ra.shuffle(a)
>>> a
[4.5, 'apple', 50]
>>> ra.shuffle(a)
>>> a
['apple', 50, 4.5]
Python EE Unit-1 By Dr.Judith Leo , HITS 19-02-2018
(vi) seed(x)
Example
>>> ra.seed(10)
>>> ra.random()
0.5714025946899135
>>> ra.random()
0.4288890546751146
>>> ra.seed(10)
>>> ra.random()
0.5714025946899135
Addition Quiz
O/P
import random What is 47 + 43 What is 1 + 61
90 65
number1 = random.randint(0, 90) 47 + 43 = 90 is True 1 + 61 = 65 is False