PPS_Notes_Unit 3
PPS_Notes_Unit 3
PPS_Notes_Unit 3
AY 2022-23
Prepared By
Prof. S.B.Patil
Vision and Mission of Institute
VISION
“We are committed to produce not only good engineers but good human beings, also.”
MISSION
• We believe in and work for the holistic development of students and teachers.
• We strive to achieve this by imbibing a unique value system, transparent work culture,
excellent academic and physical environment conducive to learning, creativity and
technology transfer.
VISION
The department of Engineering Sciences is committed to support the core engineering programs
with fundamental knowledge and skills with acumen to be leaders amongst the generation of
engineers.
MISSION
The department of Engineering Sciences strives to incorporate the best pedagogical methods to
deliver basic sciences to engineering students and to guide them to be proactive learners, deep
thinkers and responsible citizens in the early stages of their engineering education.
To foster students of first year engineering for the respective programs of engineering
through expert lectures, seminars & mini projects.
To implement activity plan for overall development of students.
To develop laboratories for meaningful implementation of curriculum and then for
research.
To apply project and problem based leaning approach for first year engineering program
to create a bridge to support to core departments for guiding projects of students.
To cultivate research in the field of Engineering Sciences for the benefit of society.
POs are statements that describe what students are expected to know and be able to do upon
graduating from the program. These relate to the skills, knowledge, analytical ability attitude and
behavior that students acquire through the program.
i) Problem Analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences and engineering sciences.
ii) Design/Development of Solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
1. Get solid foundation in basic sciences along with engineering fundamentals for a
successful professional career.
2. Able to co-relate engineering issues to broad social context.
Credit: 04
End-Sem: 70 Marks
Course Objectives: Prime objective is to give students a basic introduction to programming and
problem solving with computer language Python. And to introduce students not merely to the
coding of computer programs, but to computational thinking, the methodology of computer
programming, and the principles of good program design including modularity and
encapsulation.
1) To understand problem solving, problem solving aspects, programming and to know about
various program design tools.
2) To learn problem solving with computers
3) To learn basics, features and future of Python programming.
4) To acquaint with data types, input output statements, decision making, looping and functions
in Python
5) To learn features of Object Oriented Programming using Python
6) To acquaint with the use and benefits of files handling in Python
Course Outcomes:
CO2. Choose most appropriate programming constructs and features to solve the problems in
diversified domains.
CO3. Exhibit the programming skills for the problems those require the writing of well-
documented programs including use of the logical constructs of language, Python.
CO4. Demonstrate significant experience with the Python program development environment
Contents: Need for functions, Function: definition, call, variable scope and
lifetime, the return statement. Defining functions, Lambda or anonymous function,
documentation string, good programming practices. Introduction to modules,
Introduction to packages in Python, Introduction to standard library modules.
Unit Objectives :
On completion the students will be able to :
1. Understand Function.
2. Understand Lambda.
3. Get standard library modules.
Unit outcomes:
Students are able to understand the functions and modules:
1. Understand use of functions in python program.
2. Exhibit the programming skills for the problems those require the writing of well-
documented programs including use of the logical constructs of language, Python.
Start to apply various skills in problem solving using functions and modules.
Books :
1. Reema Thareja, “Python Programming Using Problem Solving Approach”,
Oxford University Press
File1:- standardsquareroot.py
from math import sqrt
The expression
sqrt(num)
is a function invocation, also known as a function call. A function provides a service to the code
that uses it. Here, our code in Listing 6.1 (standardsquareroot.py) is the calling code, or client
code. Our code is the client that uses the service provided by the sqrt function. We say our code
calls, or invokes, sqrt passing it the value of num. The expression sqrt(num) evaluates to the
square root of the value of the variable num. Unlike the other functions we have used earlier, the
interpreter is not automatically aware of the sqrt function. The sqrt
functionisnotpartofthesmallcollectionoffunctions(like type, int,and str)always available to Python
programs. The sqrt function is part of separate module within the standard library. A module is a
sqrt(num)
num is the information the function needs to do its work. We say num is the argument, or
parameter, passed to the function. We also can say “we are passing num to the sqrt function.”
The function uses the variable num’s value to perform the computation. Parameters enable
callers to communicate information to a function during the function’s execution. The program
could call the sqrt function in many other ways, as Listing 6.2 (usingsqrt.py) illustrates.
File2: usingsqrt.py
# This program shows the various ways the
x = 16
print(sqrt(16.0))
print(sqrt(x))
# Pass an expression
print(sqrt(2 * x - 5))
y = sqrt(x)
y = 2 * sqrt(x + 16) - 4
print(y)
y = sqrt(sqrt(256.0))
print(y)
print(sqrt(int('45')))
>>> print('Hi')
Hi
>>> __builtins__.print('Hi')
Hi
>>> __builtins__.print
>>> id(print)
9506056
>>> id(__builtins__.print)
Function Basics
There are two aspects to every Python function:
• Function definition. The definition of a function contains the code that determines the
function’s behavior.
Every function has exactly one definition but may have many invocations. An ordinary function
definition consists of four parts:
• Name—The name is an identifier (see Section 2.3). As with variable names, the name chosen
for a functionshouldaccuratelyportrayitsintendedpurposeordescribeitsfunctionality.
(Pythonprovides forspecializedanonymousfunctionscalled lambda expressions, butwedefertheir
introduction until Section 8.7.)
• Body—every function definition has a block of indented statements that constitute the
function’s body. The body contains the code to execute when callers invoke the function. The
code within the body is responsible for producing the result, if any, to return to the caller.
File3 shows the general form of a function definition. A function usually accepts some values
from its caller and returns a result back to its caller. (doublenumber.py) provides a very simple
function definition to illustrate the process.
File3: doublenumber.py
def double(n):
# Call the function with the value 3 and print its result
x = double(3)
print(x)
Defining functions:
1. When new function is to be used we need to first define it.
2. Local variables or objects cannot be accessed outside a function.
3. Note : Function name cannot contain spaces or special characters
except underscore (–).
Syntax :
For example:
Write a program to add two numbers using a function.
#Simple add function
def add(a,b):
c=a+b
return c
c=add(90,78)
print(‘Addition is’,c)
Example:
def count_to_n(n):
for i in range(1, n + 1):
print(i, end=' ')
print()
Call to a function:
1.A function can be called from any place in python script.
2.Place or line of call of function should be after place or line of
declaration of function.
Example:
• In following code, add function is called on line 6.
• Add() function definition start on line 2 and ends on line 4.
Program:
Example:
# Compute the greastest common factor of two integers
largest_factor = 1
return largest_factor
2. Global variables or objects are created in python script outside any function.
3. Global objects are available after “global” keyword defined in the script.
Example :
Program:
• def add_gv(a,b):
• c=a+b+gv
• gv=100
• add_gv(10,20)
Example :
• Modifications made in the function (after using “global”) will stay after the function as
well.
Program:
#Modification in global variable is made
def add_gv(a,b):
global gv
gv=150
print(gv)
c=a+b+gv
return c
gv=100
print(gv)
x=add_gv(10,20)
print(x)
print(gv)
2. Arguments or Parameters to a function are treated as local variable for that function.
Types of Arguments:
1. Positional Arguments
2. Default Arguments
3. Unlimited-Positional Arguments
4. Keyword Arguments
Positional Arguments :
3. Example. In following example add functional takes two positional arguments a and
b.
4. When function is called add(90,78) then arguments are assigned by their position.
Default Arguments:
• One of the argument to a function may have its default value.
• For example laptop has default built-in speakers. So if no speaker is connected it will
play default speaker.
• Now if value for this argument is not passed by the user then function will consider that
arguments default value.
• Calling functions with very large number of arguments can be made easy by default
values
• So, even if value of b is not passed, then default value of b will be 10.
#Default arguments
#A default argument is an argument that assumes a default value if a value is not provided
return;
default_Argument( name=nm)
• Example is print().
• So, programmer can also create such a function taking unlimited arguments.
Keyword Arguments:
• These are another special category of arguments supported in python.
return;
result = n1+n2+n3
return result
print("Sum =",sum)
Return Statement:
• It is statement to return from a function to its previous
function who called this function.
• After return control goes out of the current function.
• All local variables which were allocated memory in current
function will be destroyed.
• Return statement is optional in python.
• Any function can return multiple arguments.
Example:
• return #This returns none value
• return None #This returns none value
• return a, b #This returns two values
• return a #This returns single value
• These all are valid examples of a return statement.
#Driver code
num1 = getData()
num2 = getData()
Documentation String:
• In python, programmer can write a documentation for every
function.
• This documentation can be accessed by other functions.
def func( ):
"""Welcome to Coulomb"""
return
print(func.__doc__)
def sum(numbers):
total = 0
for x in numbers:
total += x
return total
L=[2,4,1,5,6]
print(sum(L))
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
if (n==1):
return False
elif (n==2):
return True;
else:
for x in range(2,n):
if(n % x==0):
return False
return True
a=int(input("Enter a number:"))
print(test_prime(a))
def is_even_num(l):
enum = []
for n in l:
if n % 2 == 0:
enum.append(n)
return enum
def fibo(n):
if(n<2):
return n
return(fibo(n-1)+fibo(n-2))
print("fibonacci series:")
for i in range(nterms):
print(fibo(i))
A. Important points:
1. Need of function
2. Call function
3. Arguments in function
4. Local and Global variable
5. Documentation string
6. Good programming practices
7. Introduction to modules and packages
8. Standard library in python
9 What is global variable and local variable? Explain along with suitable example. 6
16 Define Function, Function Call and Function Parameters with a Python program(s) 6
that demonstrate mismatch conditions between function parameters and
arguments.
17 What are function parameters? Define a function that take two parameters and 6
does their addition and display it.
19 What is documentation string in python? Expalin its advantages and rules to write 6
docstrig?
26 What is lambda function? Write a program to find smaller of two numbers using lambda 6
function.
28 Write a program that uses a lambda function to find the sum of 1st 10 natural 6
numbers?
30 The return statement is optional. Justify this statement with the help of an 6
example.
34 Write a python program using function to compute area of triangle, square, circle 6
and rectangle.
35 Write python program using function to find greatest of three numbers by passing 6
numbers as argument.
36 Write a python program using function to find whether number is odd or even. 6
37 Write a Python function that takes a number as a parameter and check the 6
number is prime or not.
38 What is the result of the below lines of code?Here is the example code.def fast 6
(items= []): items.append (1) return itemsprint fast ()print fast ()
46 Write a program that prints absolute value, square root and cube of a number. 6
50 Write a program to display the date and time using the time module. 6