Humanities/Arts Exam  >  Humanities/Arts Notes  >  Computer Science for Class 11  >  Introduction to Python Module

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts PDF Download

Module

  • A module is a grouping of functions. The program is divided into different parts under different levels, called modules.
  • Also, suppose we have created some functions in a program and we want to reuse them in another program. In that case, we can save those functions under a module and reuse them.
  • A module is created as a python (.py) file containing a collection of function definitions.

Import Statement

  • To use a module, we need to import the module. Once we import a module, we can directly use all the functions of that module.

The syntax of import statement is as follows:

  • import modulename1 [,modulename2, …]

This gives us access to all the functions in the module(s).

  • To call a function of a module, the function name should be preceded with the name of the module with a dot(.) as a separator.

The syntax is as shown below:

  • modulename.functionname()

Built-in-Module

Python library has many built-in modules that are really handy to programmers. Let us explore some commonly used modules and the frequently used functions that are found in those modules:

  • math
  • random
  • statistics

1. Module name : math

  • It contains different types of mathematical functions.
  • Most of the functions in this module return a float value.
  • In order to use the math module we need to import it using the following statement:

import math
Some of the commonly used functions in math module are given in Table.

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts

2. Module name : random

  • This module contains functions that are used for generating random numbers.
  • For using this module, we can import it using the following statement:

import random

  • Some of the commonly used functions in random module are given in Table.

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts

3. Module name : statistics

  • This module provides functions for calculating statistics of numeric (Real-valued) data. It can be included in the program by using the following statements:

import statistics

  • Some of the commonly used functions in statistics module are given in Table :-

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts

Important points regarding Import Statement:

  • import statement can be written anywhere in the program
  • Module must be imported only once.
  • In order to get a list of modules available in Python, we can use the following statement: help(“module”)
  • To view the content of a module say math, type the following: help(“math”)
  • The modules in the standard library can be found in the Lib folder of Python.

From Statement

  • Instead of loading all the functions into memory by importing a module, from statement can be used to access only the required functions from a module.
  • It loads only the specified function(s) instead of all the functions in a module.

Its syntax is :
from modulename import functionname [,functionname,…]

  • To use the function when imported using “from statement” we do not need to precede it with the module name.
  • Rather we can directly call the function as shown in the following examples:

Example-

  • >>> from random import random
  • >>> random() #Function called without the module name

Output:

  • 0.9796352504608387

Example –

  • >>> from math import ceil, sqrt
  • >>> value = ceil(624.7)
  • >>> sqrt(value)

Output:

  • 25.0

Composition

  • A programming statement wherein the functions or expressions are dependent on each other’s execution for achieving an output is termed as composition.

Example :

  • >>> sqrt(ceil(624.7))
  • >>> sqrt(trunc(625.7))

a = int(input("First number: "))
print("Square root of ",a ," = ",math.sqrt(a))
print(floor(a+(b/c)))
math.sin(float(h)/float(c))

Creating a User Define Module

  • In Python standard library, we can also create our own module consisting of our own functions.

Create a user defined module basic_math that contains the following user defined functions:

  • To add two numbers and return their sum.
  • To subtract two numbers and return their difference.
  • To multiply two numbers and return their product.
  • To divide two numbers and return their quotient and print “Division by Zero” error if the denominator is zero.
  • Also add a docstring to describe the module. After creating module, import and execute functions.

The requirement is:

  • Write a docstring describing the module.
  • Write user defined functions as per the specification.
  • Save the file.
  • Import at shell prompt and execute the functions.

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts

Output:

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts

doc variable stores the docstring. To display docstring of a module we need to import the module and type the following:
print(<modulename>._ _doc_ _) #_ _ are 2 underscore without space

The document Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts is a part of the Humanities/Arts Course Computer Science for Class 11.
All you need of Humanities/Arts at this link: Humanities/Arts
20 videos|20 docs|5 tests

Top Courses for Humanities/Arts

20 videos|20 docs|5 tests
Download as PDF
Explore Courses for Humanities/Arts exam

Top Courses for Humanities/Arts

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Objective type Questions

,

Extra Questions

,

study material

,

Exam

,

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts

,

Previous Year Questions with Solutions

,

Summary

,

Free

,

pdf

,

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts

,

Important questions

,

Viva Questions

,

mock tests for examination

,

MCQs

,

ppt

,

video lectures

,

past year papers

,

Introduction to Python Module | Computer Science for Class 11 - Humanities/Arts

,

Sample Paper

,

practice quizzes

,

shortcuts and tricks

,

Semester Notes

;