ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
Functions.docx
1. Function – A block of organized, reusable code that is used to perform a single, related action and is executed when we
call it.
For example we have different buttons on remote control each one of which have different functionality in the same way
function is used for different functionalities and that particular button of remote works when we press it.
Types of Python Functions
Built in functions defined in modules User Defined
(already predefined) ( used by calling particular module) (design by user )
Why Write Functions? (Advantages)
Reduces complexity of code- It helps to divide the large programs into small groups so that we can read the code, and
debug the program faster and better.
Reusability-Python Functions stop us from writing the same logic again and again. We can write the logic in one
function and then call it again and again.
Efficient maintainability-The code is usually well organized, easy to maintain.
Library functions(Built in):-pre-defined, present in standard library and no need to use import statement.
Programming code output Function used
a=int(input("enter any int value"))
b=str(20) enter any int value
Input ()– used for text input
Type conversion :- int, str, float
int() used to convert text into int
print (eval(b+”30”)) 2030 print()- displays on the screen
eval()-adds two string and returns
result as integer
li=[1,2,3,4]
print (max(li), min(li))
4
1
max()- gives maximum element in list
min()-gives minimum element in list
30
2. for i in range(0, len(li)):
print (li[i]),
1 2 3 4 range()-used in loops
x,y=20.25,-30
print(round(x,0))
20.0 round()-rounds up argument to give
no. of places
print (abs(y)) 30 abs()-gives absolute value
print (type(x)) float type()-gives data type of arguement
print (id(x)) Id()-gives identity of object
Functions defined in modules:- These are precompiled functions which are defined in particular module and in order to use
these functions we have to import the particular module(in simple terms file containing that function).
Functions of math & tring library:- We have to write import math or import string in our program before using module
Math Module String Module
Name of
Function
What it does Name of Function What it does
ceil(n) It returns the smallest integer
greater than or equal to n. capitalize() Converts the first character to
upper case
floor(n)
It returns the largest integer less
than or equal to n
count() Returns the number of times a
specified value occurs in a
string
factorial(n) It returns the factorial of value n
find() Searches the string for a
specified value and returns the
position of where it was found
3. exp(n) It returns exp isalnum() Returns True if all characters in
the string are alphanumeric
1og2(n)
It returns the base-2 logarithm
of n
isalpha() Returns True if all characters in
the string are in the alphabet
1og10(n)
It returns the base-10 logarithm
of n
isdigit() Returns True if all characters in
the string are digits
pow(n, y) It returns n raised to the power y title() Converts the first character of
each word to upper case
sqrt(n) It returns the square root of n islower() Returns True if all characters in
the string are lower case
fmod(x, y)
It returns the remainder when n
is divided by y
isnumeric() Returns True if all characters in
the string are numeric
cos(n) It returns the cosine of n lstrip() Returns a left trim version of the
string
sin(n) It returns the sine of n isspace() Returns True if all characters in
the string are whitespaces
tan(n) It returns the tangent of n istitle() Returns True if the string
follows the rules of a title
4. pi It is pi value (3 14159) isupper() Returns True if all characters in
the string are upper case
e
It is mathernaticalconstante (2
71828
lower() Converts a string into lower
case
User defined function:-
Syntax
keyword
identifier
variables
function header terminator
def functionname( parameters ):
function_statements function code
return [expression]
To define your own Python function, you use the ‘def’ keyword before its name. And its name is to be followed by
parentheses, before a colon(:)
For example def cal():
Any input parameters or arguments should be placed within these parentheses. You can also define
For example def cal(a, b): parameters inside these parentheses.
The code block within every function starts after a colon (:) and is indented.
def cal(a, b):
print (a*b)
5. The statement return [expression] exits a function, optionally passing back an expression to the caller. A return
statement with no arguments is the same as return None.
def cal(a, b):
return a*b
Rules fornaming python function (identifier)
1. It can begin with either of the following: A-Z, a-z, and underscore(_).
2. The rest of it can contain either of the following: A-Z, a-z, digits(0-9), and underscore(_).
3. A reserved keyword may not be chosen as an identifier.
(It is good practice to name a Python function according to what it does)
Execution of function output
def my_own_function():
(3) print("Hello from a function")
(1)print("hello before calling a function")
(2)my_own_function() #function calling.
(4)print("hello after calling a function")
hello before calling a function
Hello from a function
hello after calling a function
Explaination
When function call occurs, program control transfers to function block and when last statement of function block is executed, then
control is transferred again to calling block of code
How to write function
A simple program Simple function
A=int(input(“enter first variable”))
B= int(input(“enter second variable”))
def disp(A, B) : #formal parameters
print (A+B)
6. C=A+B
print ( C)
A=int(input(“enter first variable”))
B= int(input(“enter second variable”))
disp(A,B) #Actual parameters
NOTE: Formal parameters are written in function definition and actual parameters are passed at the time of function calling and name
of formal and actual parameters can be different.