Class 12 CS F 1
Class 12 CS F 1
Class 12 CS F 1
Module
A module is a file containing Python definitions (i.e., functions) and statements. Standard
library of Python is extended as module(s) to a programmer.
There are many ways to import a module in your program, the one’s which you should know
are:
i. import
ii. from Import
It is simplest and most common way to use modules in our code.
Its syntax is:
import module name
Example value= math.sqrt (25) # dot notation
From Statement
It is used to get a specific function in the code instead of the complete module file.
Syntax: from module name import function name
Example: from math import sqrt value = sqrt (25)
Built-in function
These are pre-defined functions and are always available for use. Example: len( ), type( ),
input( ) etc.
Page 1 of 6
here,
def keyword
sum function name
(x,y) parameter inside () are x,y
def sum (x,y): function header ends with colon:
return function may or may not have a return statement
Function Header
The first line of function definition that begins with keyword def and ends with a colon (:),
specifies the name of the function and its parameters.
Parameters
Variables that are listed within the parentheses of a function header.
Function Body
The body of the function gets executed only when the function is called/invoked.
The function body may or may not return any value. A function returns a value through a
return statement.
Indentation
The blank space in the beginning of a statement within a block. All statements within same
block have same indentation.
Eample1:
def sumof3multiplies1(n):
s=n*1+n*2+n*3
return s
Example 2:
def sumof3multiplies1(n):
s=n*1+n*2+n*3
print(s)
in Example 1 and 2, both these functions are doing the same thing but first one is returning the
computed value using return statement and second function is printing the computed value
using print () statement.
Page 2 of 6
sum=calcSum(num1, num2) //calling function
print("sum of two given numbers is", sum)
x=5
multiply (y, x) # function call 3
with function call 1, the variable a and b in function header will receives value 12 and y
respectively.
with function call 2, the variable a and b in function header will receives value y and y
respectively.
with function call 3, the variable a and b in function header will receives value y and x
respectively.
• Passing Parameters
Python supports three types of formal argument/parameters:
• Positional argument
• Default argument
• Keyword argument
Positional argument
When the function call statement must match the number and order of arguments as defined
in the function definition, this is called the positional argument matching.
For Example
def(a,b,c):
……
Then the possible function calls for this can be:
check (x,y,z) # 3 values (all variables) passed
check(2,x,y) # 3 values (literls+variables) passed
check (2,5,7) # 3 values (all literls) passed
Default Arguments
• A parameter having default value in the function header is known as default parameter.
• A parameter having a default value in function header becomes optional in function call.
Function call may or may not have value for it.
Page 3 of 6
• Non-default argument cannot follow default argument.
Example: function header with default values:
def interest (principle, time, rate=0.10)
function call
si_int=interest (5400, 2) #third argument missing
then the value 5400 is passed to the parameter principle, the value 2 is passed to the second
parameter time and since third argument rate is missing, its default value 0.10 is used for rate.
si_int=interest (6100, 3.0, 0.15) #no argument missing
then the parameter principle gets value 6100, time gets 3 and the parameter rate gets value
0.15.
this means the default values are considered only if no value is provided for that parameter in
the function call statement.
Keyword Argument
Keyword argument are the named arguments with assigned values being passed in the function
call statement.
Example:
interest (principle=2000, time=2, rate=0.10)
principle geta value 2000, time gets value 2 and rate as 0.10.
It is clear above function definition that values for parameters prin and cc can be provided
either as positional arguments or as keyword but these values cannot be skipped from the
function call.
Example:
def greet( );
print(“helloz”)
a=greet( )
print(a)
return
output:
helloz
None
Page 5 of 6
a. Either receive the returned values n from a tuple variable, i.e.,
def square (x,y,z)
return x*x, y*y, z*z #the return statement retuning comma separated
values (expression)
t=squared (2,3,4) # variable that receive the returned values is a tuple
print(t) # Tuple t will be prints as: (4,9,16)
b. Or you can directly unpack the received values of tuple by specifying the same
number of variables on the left-hand side of the assignment in function call
e.g.,
def square(x,y,z):
return x*x, y*y, z*z
v1,v2,v3=squared(2,3,4) #now the receiverd values are in the form of three
different variables, not as a tuple
print(“the returned values are as under: ”)
print(v1,v2,v3)
output:
the returned values are as under:
4 9 16
• Composition
Composition in general refers to using an expression as part of a larger expression, or a
statement as a part of larger statement.
Example 1:
Greater ((4+5), (3+4))
Example 2:
A logical expression
test (a or b)
HOME ASSIGNMENT:
1. Write a program that receive two numbers in a function and returns the results of all
arithmetic operations (+, -, *, /, %) on these numbers (page no. 114).
2. Write a program to add two numbers through function (page no.101).
Soln: def calcSum(x,y):
s=x+y
return s
num1=int(input("enter first no "))
num2=int(input("enter first no "))
sum=calcSum(num1, num2)
print("sm of two given numbers is", sum)
3. Program to calculate simple interest using a function interest () that can receive principal
amount, time and rate returns calculated simple interest. Do specify default values for
rate and time as 10% and 2 years respectively (page no. 110).
Page 6 of 6