0% found this document useful (0 votes)
12 views

Functions Defined in Modules

The document provides an overview of functions in Python, explaining their purpose, advantages, and types, including built-in functions and those defined in modules. It details the math, statistics, and random modules, highlighting specific functions and their usage with examples. Additionally, it includes activities to identify errors and predict outputs related to random number generation and list manipulation.

Uploaded by

kashisajitv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Functions Defined in Modules

The document provides an overview of functions in Python, explaining their purpose, advantages, and types, including built-in functions and those defined in modules. It details the math, statistics, and random modules, highlighting specific functions and their usage with examples. Additionally, it includes activities to identify errors and predict outputs related to random number generation and list manipulation.

Uploaded by

kashisajitv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

FUNCTIONS IN PYTHON

Introduction
Gallery Message Watsapp

Myfiles Google Calculator

• A function is a collection of statements to perform a particular task.


• Large programs are broken down into smaller units/modules known as functions.
• A function is a named unit of a group of program statements. To execute a function, we have to call it
in the program as and when requires.
• Advantage of using functions is that it makes the program handling easier as only a small part of the
program is dealt with at a time.
• Another advantage is that it reduces the program size. Once written, a function can be used in other
programs as library functions.
Types of Functions

Types of Functions

Functions User
Built-in
defined in Defined
Functions
Modules Functions
Built-in Functions
• These are pre-defined functions that are always available for use.
• We can directly use it in our program.
• There is no need to import any packages to use this built-in functions.
• Examples: print(), input(), len(), type() …..
Functions Defined in Modules
• In Python, a library is a collection of modules and packages that together cater to a
specific type of applications or requirements.
• A python module is a file containing variables, statements and functions related to
a particular task.
• Functions defined in modules are pre-defined functions in particular modules.
• These functions can be used in our programs only after importing the
corresponding module using import statement.
• Examples: math module, random module, statistics module
Math Modules
• It contains different types of mathematical functions.
• Most of the functions in this module returns float values.
• It can be used in the program by using the following statement:
import math
• Some of the functions in math module are:
sqrt(x): It returns the square root of x. ‘x’ can be a positive integer or floating
point number.
import math
math.sqrt(169) returns 13.0
math.sqrt(-68) returns value Error : math domain error
math.sqrt(0) returns 0.0
ceil(x) : It returns the smallest integer not less than x. ‘x’ can be positive integer or
negative integer or floating point number.

import math
math.ceil(2.1) returns 3
math.ceil(2.8) returns 3
math.ceil(2) returns 2
math.ceil(-2.6) returns -2
floor(x): It returns the largest integer not greater than x. ‘x’ can be a negative or positive
integer or floating point number.
import math
math.floor(6.5) returns 6
math.floor(6) returns 6
math.floor(-6.5) returns -7
pow(x,y): It returns the value x raised to power y in float type. x & y can be an integer or
floating point numbers.
• Built-in function pow returns the value not as a float type. And also it can take maximum 3
prameters. Example : pow(2,3,5) returns the value of (2^3)%5 ( 3rd parameter is
optional).
import math
math.pow(2,3) returns 8.0
math.pow(2.5,1.5) returns 3.9528
pow(2,3) returns 8
fabs(x): It returns the absolute value of float x. x can be integer or any floating point
number.

import math
math.fabs(45) returns 45.0
math.fabs(-45) returns 45.0
math.fabs(-45.6) returns 45.6

pi : Returns the value of pi.


math.pi returns 3.141592653589793

sin()/cos()/tan() : Returns the sin/cos/tan values of angles measured in


radians.
Statistics Module
• This module provides functions for calculating statistics data.
mean(): Returns the mean/average of the given data.
Syntax: statistics.mean(<sequence of values>)
Example: import statistics
statistics.mean([2,5,4,8])) returns 4.75
median(): Returns the middle value of the given data.
syntax: statistics.median(<sequence of values>)
* if the number of elements in the sequence is odd, median() returns middle value. If it is
even, then median() returns the mean of two central numbers.
import statistics
statistics.median( [10,20,30,40,50])) returns 30.0
statistics.median( [10,20,30,40,50,60])) returns 35.0
mode(): Returns the most repeated value in the given sequence of data.
syntax: statistics.mode(<sequence of values>)
import statistics
statistics.mode( [10,20,30,10,40,20,10])) returns 10.
• If there are multiple numbers with same frequency, then mode returns the first on
encountered.
statistics.mode([10,20,30,10,20,10,20]) returns 10
• If we are not passing any data into the statistics functions, then it returns Statistics
Error.
Random Module
• The programming concept involved situations where we aware of the definite
output to be obtained when a particular task to be executed.
• There are certain situations where we require some numbers which are not known
earlier. For example,
*Pseudo random numbers on Lottery scratch cards
*Captcha in login forms
* Computer games involving throwing of dice, picking a number or flipping a
coin.
*Shuffling deck of playing cards
• In the above situations we need random numbers.
• In order to generate random numbers, we have to import random module.
import random
Types of functions in random module
random()
• This function generates floating value between 0 & 1 including 0 and excluding 1.
• random() takes no arguments.
• Example: import random
random.random() returns 0.33651376240554043
random.random() returns 0.9690849208188809
random.random() returns 0.32623498836182896
• Each time random() generates a different number.
• Example: >>>random.random()*5 generates random number between 0 &5.
• >>>random.random()*5+10 generates random number between 10 & 15.
>>>random.random()*900+100 generates random number between 100 & 1000.
randrange()
• This method always returns the integer between lower and upper argument
excluding upper argument.
• Syntax: randrange (start, stop, step)
• Example: To generate random numbers from 0 to 30
>>>import random
>>>random.randrange(30) returns 27
>>>random.randrange(30) returns 18
• To generate random numbers between 10 to 100 with the multiple of 5.
import random
x=random.randrange(10,100,5)
print(x) returns 30
randint()
• It takes 2 arguments a & b in which ‘a’ is lower & ‘b’ is the upper limit.
• This function returns any integer between ‘a’ and ‘b’ including both.
• Syntax: random. randint(a,b)
• Example: import random
x=random.randint(0,5)
The above program generates an integer between 0 and 5 i.e, either 0,1,2,3,4 or 5.
uniform()
• random. uniform(x, y) It generates a random floating point number which is
greater than x and less than y.
• Example: random.uniform(1,100) returns 39.56970658693995
choice()
• This method is used for making a random selection from a sequence like list, tuple or string.
• Syntax: random.choice(sequence)
• Example: import random
x=random.choice([1,2,3,4,5]
print(x) returns an element 1,2,3,4 or 5
shuffle()
• This method is used to shuffle the contents of a list.
• Syntax: random.shuffle(list)
• Example: import random
fruits=[‘Apple’,’Mango’,’Orange’,’Grapes’]
random.shuffle(fruits)
print(fruits) returns ['Mango', 'Orange', 'Apple', 'Grapes']
Another way of importing a module into the program

• from <module name> import <function name>


• Example: from math import pi
from random import randrange
• Example: from math import pi
print(pi) returns 3.141592653589793
from math import floor
print(floor(4.5)) returns 4
Activity
• Find errors:
1) import math
math.power(4,2)

2) import random
random.randint(4,16,4)
3) import random
random.random(2,10)
4) import math
math.sqrt(-256)
Activity
• Find the output and what is the minimum and maximum values for the variables end
and begin?
import random
colors=[“Violet”,”Indigo”,”Blue”,”Green”,”Yellow”,”Orange”,”Red”]
end=random.randrange(2)+5
begin=random.randint(1,end)+1
for i in range(begin,end):
print(colors[i],end=“&”)

a) Blue & Green &


b) Green & Yellow & Orange &
c) Indigo & Blue & Green &
d)Yellow & Orange & Red &
Activity
• Find the output and specify the maximum and minimum value that can be
assigned to pos variable.
import random
languages=[‘Python’, ‘Java’, ‘PHP’, ‘HTML’, ‘C++’]
pos=random.randint(1,4)
for c in range(0, pos+1):
print(languages[c],end=“&”)

i) Python&Java&PHP&HTML&C++&
ii) Java&PHP&HTML&C++&
iii) Python&Java&
iv) Python&Java&PHP&

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