0% found this document useful (0 votes)
2 views9 pages

17 Python Modules-2!7!15

The document discusses various built-in Python modules, focusing on the Math and Random modules. The Math module provides functions for arithmetic operations such as sqrt, pow, and trigonometric functions, while the Random module offers methods like random() for generating floating-point numbers and randint() for generating integers within a specified range. Examples and explanations of these functions are provided to illustrate their usage.

Uploaded by

sohanbavaskar08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

17 Python Modules-2!7!15

The document discusses various built-in Python modules, focusing on the Math and Random modules. The Math module provides functions for arithmetic operations such as sqrt, pow, and trigonometric functions, while the Random module offers methods like random() for generating floating-point numbers and randint() for generating integers within a specified range. Examples and explanations of these functions are provided to illustrate their usage.

Uploaded by

sohanbavaskar08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

for more updates visit: www.python4csip.

com

Types of Modules
 There are various in-built module in python, we will
discuss few of them
 Math module
 Random module

 Statistical module

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Math module
 This module provides various function to perform
arithmetic operations.
 Example of functions in math modules are:
sqrt ceil floor pow
fabs sin cos tan
 Example of variables in math modules are:
 pi

e

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Math module functions


 sqrt(x) : this function returns the square root of
number(x). module name is
required before
function name here

 pow(x,y) : this function returns the (x)y


module name is not
required before
function name here

 ceil (x) : this function return the x rounded to next


integer.

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Math module functions


 floor(x) : this function returns the x rounded to previous
integer.
 fabs(x) : this function returns absolute value of float x.
absolute value means number without any sign

 sin (x) : it return sine of x (measured in radian)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Math module functions


 cos(x) : it return cosine of x (measured in radian)

 tan(x) : it return tangent of x (measured in radian)

 pi : return the constant value of pi (22/7)

 e : return the constant value of constant e


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

Using Random Module


 Python has a module namely random that provides
random – number generators. Random number
means any number generated within the given
range.
 To generate random number in Python we have to
import random module
 2 most common method to generate random number
in python are :
 random() function
 randint(a,b) function

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

random() function
 It is floating point random number generator
between 0.0 to 1.0. here lower limit is inclusive
where as upper limit is less than 1.0.
 0<=N<1
 Examples:

Output is less than 1


VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

random() function
 To generate random number between given range
of values using random(), the following format
should be used:
 Lower_range + random() * (upper_range-lower_range)
 For example to generate number between 10 to 50:
 10 + random() * (40)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: [Link]

randint() function
 Another way to generate random number is
randint() function, but it generate integer numbers.
 Both the given range values are inclusive i.e. if we
generate random number as :
 randint(20,70)
 Inabove example random number between 20 to 70 will
be taken. (including 20 and 70 also)

VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &


SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR

You might also like