You Will Learn The Following Topics in This Tutorial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.

com

Chapter 18 - Introduction to Python modules

You will Learn the Following Topics in this Tutorial


S. No. Topics

1 Introduction

2 How to import modules in Python?

3 Built-in Modules

4 Math Module

5 Random Module (Generating Random Numbers)

6 Statistics Module

7 More Examples.......

Page 1 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

Introduction
A function is a named sequence of statement(s) that performs a
computation. It contains line of code(s) that are executed sequentially
from top to bottom by Python interpreter.
They are the most important building blocks for any software in Python.
Functions can be categorized as belonging to
I) Modules
II) Built in
III) User Defined

Module
A Python module is a Python file containing a set of functions and
variables to be used in an application. The variables can be of any type
(arrays, dictionaries, objects, etc.). Definitions from the module can be
used within the code of a program.

Modules are usually stored in files with .py extension

Some of the advantages of using modules in your Python code are:

 they enable you to organize your code into smaller pieces that are
easier to manage.
 the code in the modules can be reloaded and rerun as many times as
needed, which enables code reuse.
Page 2 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

 modules are self-contained – you can never see a name in another


file, unless you explicitly import it. This helps you to avoid name
clashes across your programs.

How to import modules in Python?


There are two ways to import a module:
 using the import statement – imports the entire module.
 using the from…import statement – imports only individual module
attributes.
1) using the import statement
Here is how we can import a module using the import statement:
Syntax is:
import modulename1 [,modulename2, ---------]
Example
>>> import math
On execution of this statement, Python will
(i) search for the file ‘math.py’.
(ii) Create space where modules definition & variable will be created,
(iii) then execute the statements in the module.
Now the definitions of the module will become part of the code in which
the module was imported.

Page 3 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

To use/ access/invoke a function, you will specify the module name and
name of the function- separated by dot (.).
Example
>>> value= math.sqrt (25)
5
This statement invokes the sqrt ( ) function and returns the square
root of passed value to function sqrt( ).

2) using the from…import statement

If we know beforehand which function(s) ), we will be needing .We can


import specific names from a module without importing the module as a
whole.
Its syntax is
from modulename import functionname [, functionname…..]
When we import attributes or call the function using the from…import
statement, we don’t need to precede the attribute name with the name
of the module.
Here is how it can be done:
Note: You normally put all import
Example
statement(s) at the beginning of
>>> from math import sqrt
the Python file but technically
>>> value = sqrt (25)
they can be anywhere in program.
>>> print(value)

Page 4 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

Built-in Modules
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.

import math #the very first statement to be given

math.pi

Pie (π) is a mathematical constant defined in the math module, which is


defined as the ratio of the circumference to the diameter of a circle and
its value is 3.141592653589793.
>>> math.pi
3.141592653589793

Page 5 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

math.e(x)
It is another mathematical constant defined in the math module is e.
It is called Euler's number and it is a base of the natural logarithm.
Its value is 2.718281828459045.

>>> math.e
2.718281828459045

math.ceil(x)

It returns the smallest integer not less than x, where x is a numeric


expression.
>>> math.ceil(-9.7)
-9
>>> math.ceil (9.7)
10
>>> math.ceil(9)
9

math.floor(x)

It returns the largest integer not greater than x, where x is a numeric


expression.
>>> math.floor(-4.5)
-5
>>> math.floor(4.5)
4

Page 6 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

>>> math.floor(4)
4

math.fabs(x)

It returns the absolute value of x, where x is a numeric value.


>>> math.fabs(6.7)
6.7
>>> math.fabs(-6.7)
6.7
>>> math.fabs(-4)
4.0

math.factorial(x)

It returns the factorial of x, where x is a numeric value.


>>> math.factorial(5)
120

math.pow(x,y)

It returns xy (x raised to the power y), where x, y may be an integer or


floating point number
>>> math.pow(3,2)
9.0
>>> math.pow(4,2.5)
32.0
Page 7 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

>>> math.pow(6.5,2)
42.25
>>> math.pow(5.5,3.2)
233.97

math.sqrt(x)

It returns square root of x, where x may be a positive integer or floating


point number
>>> math.sqrt(144)
12.0
>>> math.sqrt(.64)
0.8

math.sin(x)

It returns the sine of x, in radians, where x must be a numeric value.


>>> math.sin(0)
0
>>> math.sin(6)
-0.279
>>> math.sin(-3)
-0.14112000806

math.cos(x)

It returns the cosine of x, in radians, where x must be a numeric value.


>>> math.cos(3)
-0.9899924966
Page 8 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

>>> math.cos(-3)
-0.9899924966
>>> math.cos(0)
1.0

math.tan(x)

It returns the tangent of x in radians, where x must be a numeric value.


>>> math.tan(3)
-0.142546543074
>>> math.tan(-3)
0.142546543074
>>> math.tan(0)
0.0

random module (Generating Random Numbers)


There are certain situations/applications that involve games or
simulations which work on a non-deterministic approach. In these types
of situations, random numbers are extensively used such as computer
games involving throwing of a dice, picking a number or Shuffling deck of
playing cards, etc.

This module contains functions that are used for generating random
numbers. For using this module, we have to import it

import random #the very first statement to be given

Page 9 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

random.random()

Generates a random float number between 0.0 to 1.0. The function


doesn't need any arguments.
>>> random.random()

0.003106178086169975

random.randint(x,y)

It returns a random integer between the specified integers such that


x <= y.
>>> random.randint(3,7)
5
>>> random.randint(-3,5)
4
>>> random.randint(-5,-3)
-5.0

random.randrange(start, stop, step)

Returns a randomly selected element from the range created by the start,
stop and step arguments. The value of start is 0 by default. Similarly, the
value of step is 1 by default.

>>>random.randrange(1,10)
2
>>>random.randrange(1,10,2)
5

Page 10 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

statistics module
The statistics module provides functions to mathematical statistics of
numeric data.

For using this module, we have to import it

import statistics #the very first statement to be given

statistics.mean(x)

The mean() method calculates the arithmetic mean of the numbers in a


list, where x is a numeric sequence.

>>> statistics.mean([2,5,8,3,10,25,67])

17.142857142857142

statistics.median(x)

The median() method returns the middle value of numeric data in a list,
where x is a numeric sequence.

>>> statistics.median([2,5,8,3,10,25,67])

>>> statistics.median([1,2,3,7,8,9])
5.0

Page 11 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

statistics.mode(x)

The mode() method returns the most repeated value in the list, where
x is a numeric sequence.

>>> statistics.mode([2,5,3,3,10,3,67,5,2,1])

statistics.sdev(x)

The stdev() method calculates the standard deviation on a given


sample in the form of a list, where x is a numeric sequence.

>>> statistics.stdev([1,1.5,2,2.5,3,3.5,4,4.5,5,5.5])

1.5138251770487459

More Examples…………..
Example: Consider the set of percentage of students in
a class:
Student Percentage
Amit 78.5
Amar 67.5
Renu 90.2
Taposh 60.0
Akram 75.2
Write a Python program to calculate the average
percentage of students in the class and median value
using Statistics module.
Page 12 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com

Chapter 18 - Introduction to Python modules

Code:
import statistics

perc = [78.5,67.5,90.2,60.0,75.2] # the percentage data

avg = statistics.mean(perc)

print("Average percentage in the class:",avg)

median_value = statistics.median(perc)

print("Median value for percentage:",median_value)

OUTPUT:
Average percentage in the class: 74.28
Median value for percentage: 75.2
Example: Write a program to calculate the area of
circle using pi (math module)
Code:
import math

r = float ( input ("Enter Radius: "))

area = math.pi*r

print("The Area of Circle is : %.2F" %area)

CODE:

Enter Radius: 5

The Area of Circle is : 15.71

Page 13 of 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy