You Will Learn The Following Topics in This Tutorial
You Will Learn The Following Topics in This Tutorial
You Will Learn The Following Topics in This Tutorial
com
1 Introduction
3 Built-in Modules
4 Math Module
6 Statistics Module
7 More Examples.......
Page 1 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
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.
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
Page 3 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
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( ).
Page 4 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
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
math.pi
Page 5 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
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)
math.floor(x)
Page 6 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
>>> math.floor(4)
4
math.fabs(x)
math.factorial(x)
math.pow(x,y)
>>> math.pow(6.5,2)
42.25
>>> math.pow(5.5,3.2)
233.97
math.sqrt(x)
math.sin(x)
math.cos(x)
>>> math.cos(-3)
-0.9899924966
>>> math.cos(0)
1.0
math.tan(x)
This module contains functions that are used for generating random
numbers. For using this module, we have to import it
Page 9 of 13
Unit I: Computational Thinking and Programming - 1 Visit to website: learnpython4cbse.com
random.random()
0.003106178086169975
random.randint(x,y)
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
statistics module
The statistics module provides functions to mathematical statistics of
numeric data.
statistics.mean(x)
>>> 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
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)
>>> 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
Code:
import statistics
avg = statistics.mean(perc)
median_value = statistics.median(perc)
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
area = math.pi*r
CODE:
Enter Radius: 5
Page 13 of 13