Python Ch-4_Notes
Python Ch-4_Notes
Example:
def my_function(): # function header
print("Hello from a function")
# calling function from main code
my_function()
O/P:
Hello from a function
Example:
def my_function(fname):
print(Hello", fname)
my_function("ABC")
my_function("XYZ")
my_function("DEF")
O/P:
Hello ABC
Hello XYZ
Hello DEF
Arguments
Information can be passed into functions as arguments.
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 2
Python Programming (UNIT-4) | 4311601
Arguments are specified after the function name, inside the parentheses. You can add as
many arguments as you want, just separate them with a comma.
Parameters vs. Arguments
Sometimes, parameters and arguments are used interchangeably. It’s important to
distinguish between the parameters and arguments of a function.
A parameter is a piece of information that a function needs. And you specify the
parameter in the function definition. For example, the greet() function has a parameter
called name.
An argument is a piece of data that you pass into the function. For example, the text
string 'John' or the variable jane is the function argument.
Example:
def greet(name): #parameter
print('Hi')
greet(“John”) #argument
Types of Function Arguments
You can call a function by using the following types of formal arguments −
1. Required arguments
2. Keyword arguments
3. Default arguments
4. Variable-length arguments/ Arbitrary Arguments
1. Required arguments
Required arguments are the arguments passed to a function in correct positional order.
Here, the number of arguments in the function call should match exactly with the function
definition.
Example:
# Defining a function
def function( n1, n2 ):
print("number 1 is: ", n1)
print("number 2 is: ", n2)
#function call
function( 30, 20 )
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 3
Python Programming (UNIT-4) | 4311601
O/P:
number 1 is: 30
number 2 is: 20
2. Keyword arguments
Keyword arguments are related to the function calls. When you use keyword arguments
in a function call, the caller identifies the arguments by the parameter name.
You can also send arguments with the key = value syntax.
This allows you to skip arguments or place them out of order because the Python
interpreter is able to use the keywords provided to match the values with parameters.
Example:
# Function definition is here
def printinfo( name, age ):
print ("Name: ", name)
print ("Age ", age)
These functions are created by users These functions are not created by
1 as per their own requirements. users as their own.
3
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 6
Python Programming (UNIT-4) | 4311601
There is no such kind of requirement If the user wants to use a particular
to add a particular module/ packages. library function then the user has to
add the module/ packages
UDF is the part of program which is Library functions are the part of
5 compiled at run time program which is called at run time
Scope of a Variable
A variable is only available from inside the region it is created. This is called scope.
The location where we can find a variable and also access it if required is called the scope
of a variable.
In Python, we can declare variables in two different scopes: local scope, global scope.
Based on the scope, we can classify Python variables into two types:
1. Local Variables
2. Global Variables
1. Local Variables
When we declare variables inside a function, these variables will have a local scope
(within the function). We cannot access them outside the function.
These types of variables are called local variables.
Example:
def myfunc():
x = 300
print(x)
print(x) #cannot access outside the function
myfunc()
O/P:
300
2. Global Scope
Module
A Python module is a file containing Python definitions and statements. A module can
define functions, classes, and variables.
A module can also include runnable code. Grouping related code into a module makes the
code easier to understand and use. It also makes the code logically organized.
Modules are provide the facility of reusability .
In python two types of modules are their:
1. Built in Module (Standard Library Modules): A module which is already created in
standard library of python is known as Built in Module.
2. User Define Module: A module which is created by user is known as User Define
Module.
Math Module:
Python has a built-in module that you can use for mathematical tasks.
The math module has a set of methods and constants.
Sr Function Use Example
.
Math Constants
Sr.
Constant Description
No
1 math.e() Returns Euler's number (2.7182...)
2 math.pi() Returns PI (3.1415...)
Random Module
Python Random module is an in-built module of Python that is used to generate random
numbers in Python.
These numbers occur randomly and does not follow any rules or instructuctions.
We can therefore use this module to generate random numbers, display a random item for a
list or string, and so on.
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 13
Python Programming (UNIT-4) | 4311601
Sr.
Function Use Example
No
1 random.random( Generate random floating numbers random.random()=
) between 0 to 1 0.4585545764414376
2 random.randint() Returns a random integer within the random.randint(1,50)=
range 22
Example:
import random
print(random.random())
print(random.random())
print(random.randint(5,50))
O/P:
0.050357716811645026
0.8258126187037218
49
Statistic Module
Python has a built-in module that you can use to calculate mathematical statistics of numeric
data.
Sr.
Function Use Example
No
1 statistics.mean() Calculates the mean statistics.mean([1,2,3,4,5,6])=
(average) of the given data 3.5
2 statistics.mode() return the mode (most repeated statistics.mode([1,2,3,2,5,5])=
value) of the given data 2
3 statistics.median( Calculates the median (middle statistics.median([1,2,3,4,5,6])
) value) of the given data =
3.5
4 statistics.stdev() Calculates the standard statistics.stdev([1,2,3,4,5,6])=
deviation from a sample of 1.8708286933869707
data
INFORMATION TECHNOLOGY [Mrs. Aesha K. Virani] 14
Python Programming (UNIT-4) | 4311601
Example:
import statistics
print(statistics.mean([1,2,3,4,5,6]))
print(statistics.mode([1,2,3,2,5,5]))
print(statistics.median([1,2,3,4,5,6]))
print(statistics.stdev([1,2,3,4,5,6]))
O/P:
3.5
2
3.5
1.8708286933869707
**********