You are on page 1of 8

Python EE Unit-1 By Dr.

Judith Leo , HITS 19-02-2018

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

• from math import *


sqrt(4)
Python EE Unit-1 By Dr.Judith Leo , HITS 19-02-2018

Function Description Example

Returns the smallest integer greater >>> ceil(15.5)


ceil(x)
than or equal to x. 16.0
>>> copysign(4,-5)
copysign(x, y) Returns x with the sign of y
-4.0
>>> fabs(-3)
fabs(x) Returns the absolute value of x
3.0
>>> factorial(7)
factorial(x) Returns the factorial of x
5040
Returns the largest integer less >>> floor(15.5)
floor(x)
than or equal to x 15.0
Returns the remainder when x is >>> fmod(40,3)
fmod(x, y)
divided by y 1.0
Returns an accurate floating point >>> fsum([1,2,3])
fsum(iterable)
sum of values in the iterable 6.0

Returns the fractional and >>> modf(4.5)


modf(x)
integer parts of x (0.5, 4.0)

Returns the truncated integer >>> trunc(4.5)


trunc(x)
value of x 4

>>> exp(3)
exp(x) Returns e**x
20.085536923187668

>>> expm1(3)
expm1(x) Returns e**x - 1
19.085536923187668

Returns x raised to the power >>> pow(6,2)


pow(x, y)
y 36.0

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

Returns the Euclidean norm, >>> hypot(3,4)


hypot(x, y)
sqrt(x*x + y*y) 5.0

>>> sin(90)
sin(x) Returns the sine of x
0.8939966636005579

>>> tan(85)
tan(x) Returns the tangent of x
0.17887017243876716

Returns the inverse hyperbolic cosine of >>> acosh(3.4)


acosh(x)
x 1.8945590126722978

>>> asinh(3.4)
asinh(x) Returns the inverse hyperbolic sine of x
1.9378792776645006

Returns the inverse hyperbolic tangent >>> atanh(0.4)


atanh(x)
of x 0.42364893019360184

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

Returns the natural logarithm of the


>>> lgamma(3)
lgamma(x) absolute value of the Gamma function at
0.693147180559945
x
Python EE Unit-1 By Dr.Judith Leo , HITS 19-02-2018

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

Converts angle x from degrees to >>> radians(180)


radians(x)
radians 3.141592653589793
>>> pi
pi Mathematical constant, (3.14159...)
3.141592653589793

>>> e
e mathematical constant e (2.71828...)
2.718281828459045

Random Number Functions


• To use random functions, import random module using

• import random
random.randint(0,14)

OR

• import random as ra
ra.randint(0,14)

OR

• from random import *


randint(0,14)
Python EE Unit-1 By Dr.Judith Leo , HITS 19-02-2018

(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)

• Return a random integer N such that a <= N <= 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)

• Return a random floating point number N such that a <= N <= b

Example
>>>import random as ra
>>> ra.uniform(50,120)
64.42687624976512
>>> ra.uniform(50,120)
106.9324875950124

(iv) randrange(stop) (or) randrange(start, stop)


(or)
randrange(start, stop[, step])
Returns a randomly selected element from range(start, stop, step).

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)

• Shuffle the sequence x in place.

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)

• sets the integer value x in generating random numbers.


• Given the same seed, it will generate the same number sequence every time.

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

number2 = random.randint(0, 90)


# Prompt the user to enter an answer
answer = eval(input("What is " +str(number1) + " + "+ str(number2)+“\n" ))
# Display result
print(number1, "+", number2, "=", answer, "is", number1 + number2 == answer)

You might also like