Chapter: Problem Solving and Python Programming : Data, Expressions, Statements

Modules - Python

A module is a file containing Python definitions ,functions, statements and instructions.

MODULES:

 

Ø   A module is a file containing Python definitions ,functions, statements and instructions.

Ø   Standard library of Python is extended as modules.

Ø   To use these modules in a program, programmer needs to import the module.

Ø   Once we import a module, we can reference or use to any of its functions or variables in our code.

o There is large number of standard modules also available in python.

o Standard modules can be imported the same way as we import our user-defined modules.

o Every module contains many function.

o To access one of the function , you have to specify the name of the module and the name of the function separated by dot . This format is called dot notation.

 

Syntax:

import module_name

module_name.function_name(variable)


Importing Builtin Module:

import math

x=math.sqrt(25)

print(x)

 

Importing User Defined Module:

import cal

x=cal.add(5,4)

print(x)

 


Built-in python modules are,

 

1.math – mathematical functions:

some of the functions in math module is,

math.ceil(x) - Return the ceiling of x, the smallest integer greater than or equal to x

math.floor(x) - Return the floor of x, the largest integer less than or equal to x.

 math.factorial(x) -Return x factorial.

 math.gcd(x,y)- Return the greatest common divisor of the integers a and b

 

  math.sqrt(x)- Return the square root of x  

  math.log(x)- return the natural logarithm of x   math.log10(x) – returns the base-10 logarithms   math.log2(x) - Return the base-2 logarithm of x.   math.sin(x) – returns sin of x radians

math.cos(x)- returns cosine of x radians

 math.tan(x)-returns tangent of x radians  

 math.pi - The mathematical constant π = 3.141592   math.e – returns The mathematical constant e = 2.718281

 

2. .random-Generate pseudo-random numbers

 random.randrange(stop)

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

 random.uniform(a, b)

 -Return a random floating point number

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Data, Expressions, Statements : Modules - Python |

Related Topics

Problem Solving and Python Programming : Data, Expressions, Statements


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2027 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.