Subject
Introduction to Python
Subject Expert
Prof. M. V. Tiwari
(B. E. in Electronics & Telecommunication Engineering, H.V.P.M., C.O.E.T., Amravati, 2009.)
(M. Tech. in Electronic System & Communication, An Autonomous Institute of GCOE,
Amravati, 2013.)
(Pursuing Ph.D. from P.G. Department of Applied Electronics, S.G.B.A.U. under the guidance
of Dr. S. V. Dudul, Head PGDAE.)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Function
➢Function is a group of related statements that perform related task.
➢Function Makes the code organized, reusable and manageable.
Types of Functions
1. Built in Functions : Functions that are built into python (BIF)
Eg : print, str, int, range, in, and, etc.
2. User Defined Functions : Functions defined by users (UDF)
Eg : - It can be anything depending upon user.
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Syntax : - Without return statement
def Function Name (parameters):
----------------
Statements(s)
------------
Note : Take care of indentation
Function Name(parameters)
e.g. : def sum (a, b):
c=a+b
print(‘sum =’, c)
sum (10,32)
sum(1.5, 3)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Return Statement
We can return a result from a function using return statement.
Functions in python can return multiple values also.
e.g. :
def sum (a, b):
c=a+b
return c # It is returning single value
a1 = sum(90, 30)
print(a1) # result = 120
eg : Returning Multiple values
def sum_sub(a,b):
c=a+b
d=a–b
return c, d
a1, a2 = sum_sub(10,20)
print (a1, ‘ ‘, a2) # result = 30, -10
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Parameter Types
1. Formal Parameters
These are the parameters that appear inside parenthesis ( ) of the function
definition.
They are useful for receiving the value from outside function.
2. Actual Parameters
These are the parameters that appear in the function call.
Note : Parameters are also called arguments.
Parameter Passing Techniques
1. Position Parameters
In this the parameters passed to a function correspond to their positions. The
number and positions of Actual and Formal Parameters should match.
2. Keyword Parameters
In this the parameters passed to a function correspond to their names. The
positions of the arguments can be changed.
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
eg:
Formal Parameter
def sum (a,b):
c=a+b Parameter Types
return c
a1 = sum(30,40) Actual Parameter
print (a1)
a2 = sum (b=30, a = 20) Position Parameter
print(a2) Passing Types
Keyword Parameter
Note:
➢Keyword arguments must follow positional arguments
➢Having positional argument after keyword arguments will result into errors.
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Default Parameters
1. With formal parameters we can specify a default value which would be used if that
parameter is not passed.
2. Note:
➢ Any number of arguments in a function can have default values.
➢ All arguments to the right of default arguments must have default values.
➢ Non – default arguments cannot follow default arguments.
def greet(name, msg = “Good Day”):
print(“Hello”, name, msg)
greet(“Mayur”) Hello Mayur Good Day
greet(“Vijay”, “Good Evening”)
Hello Vijay Good Evening
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Nested Function
Function can be defined within functions in Python.
Usage:
➢ Encapsulation : To hide inner functions from outside world.
➢ Modularity : To divide a giant function into small functions.
➢e.g. :
def comb(r, n):
def fact(a):
f=1
for i in range (1, a+1):
f = f*i
return f
c = fact(n)/(fact(r)*fact(n-r))
print(c)
n = int (input(‘enter total number of flavours:’))
r = int (input(‘enter scoops u can add:’))
comb(r, n)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Parameter Passing :
1. In Python numbers, string, tuples, list or dictionary are sent to functions
by their references.
2. If we pass integers, floats, strings and tuples to a functions and try to
change their value a new object is created in the function with the
modified value because they are immutable.
3. If we are passing list to a function and try to make some change in the
list then the same list is modified because they are mutable.
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Passing Integers :
def f1(n): def f2(n):
n = n*2 n = n*2
return n
n = 20 n = 20
print(n) print(n)
f1(n) f2(n)
print(n) print(n)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Passing List
def f3(plist):
plist[1] = 40
list = [10, 20, 30]
print(list)
f3(list)
print(list)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Local and Global Variables :
➢ Variables that are declared outside the functions are called global
variables.
➢ Variables that are declared inside the functions are called local
variables.
➢ global keyword used for accessing the global variable from the
function.
➢ nonlocal keyword used for accessing the variable in outer function.
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Lambda Function :
Syntax:
lambda <Arguments>: expression
Eg:
s = lambda n : n*n
result = s(4)
print(result)
O/p : 16
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Recursive Function
Function calls itself
Eg:
def fact(n):
if n == 0:
return result = 1
else:
return result = return n * (n-1)
num = int(input(“Enter the number : ”))
Print(“The factorial of ”, num, “ is : ”, fact(num))
O/p :- for n= 5 => 120
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
EXAMPLES
Q1) What is the result ?
define welcome( ):
print(‘Welcome All’)
welcome()
Options :
A) Welcome All B) All
C) Welcome D) Error
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q2) You are developing a Python application for an online game. You need to create a
function that meets the following criteria :
➢ The function is named update_score
➢ The function receives the current score and a value
➢ The function adds the value to the current score
➢ The function returns the new score
How should you complete the code ?
Option1 Option2
Current += Value
Option 3
Option 1 :
A) update_score B) def update_score C) return update_score
Option 2 :
A) (Current, value): B) ( ): C) (current, value) D) ( )
Option 3 :
A) Pass current B) return current C) return D) pass
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q3) The ABC company is creating a program that allows customers to log the number of miles biked. The
program will send messages based on how many miles the customer logs. You create the following code.
01.
2. name = input(“What is your name ? ”)
3. return name
4.
5. calories = miles * calories_per_mile
6. return calories
7. distance = int(input(‘how many miles did you biked this week? ’))
8. burn_rate = 50
9. biker = get_name()
10.calories_burned = calc_calories(distance, burn_rate)
11.print(biker, ‘you burned about ’, calories_burned, ‘calories’)
Which code segments should you use for line 01 and line 04. Select two
Options:
A)01. def get_name() B) 01. def get_name(name):
c)04. def calc_calories(): D) 04. def calc_calories(miles, calories_per_mile)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q 4) What is the result ?
def sum(a, b):
c=a+b
return c
print(c)
res = sum (10, 20)
print(res)
Options :
A. 30 B. 0
30 30
C. Error D. 30
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q 5) What is the result ?
def welcome(msg = ‘Hello’, name):
print(msg, name)
welcome(name= ‘Amit’, msg = ‘Hi’)
Options :
A)Hello Amit B) Hi Amit
C) Amit D) Error
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q 6) What is the result ?
def msg(msg, times = 2):
print(msg * times)
msg(times = 4, ‘hello’)
Options:
A) Hellohello C) hellohellohellohello
B) Error D) hello4
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q 7 ) What is the result ?
def fun(a, b = 5, c = 10):
print(a, b, c, end = ‘ ‘)
fun(c = 20, a = 5)
Options :
A)5 10 B) 5 5 10
C) 5 5 20 D) Error
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q8) You are writing a function that increments the player score in a game. The function has the following
requirements:
• If no value is specified for points, then points start at one.
• If bonus IS True, then points must be doubled.
You write the following code. Line numbers are included for reference only.
01 def increment_score(score, bonus, points):
02 if bonus == True:
03 points = points * 2
04 score = score * points
05 return score
06 points = 5
07 score = 10
08 new_score = increment_score(score, True, points)
For each of the following statements, select Yes if the statement is true. Otherwise, select No.
Q I) To meet the requirements, line 01 must be changed to the following:
def increment_score(score, bonus, points = 1):
A) Yes B) No
QII) Once any parameter is defined with a default value, any parameters to the right must also be defined with
default values.
A) Yes B) No
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q III) If the function is called with only two parameters, the value of
the third parameter will be None.
A) Yes B) No
Q 9) What is the result ?
def f1(a): Options :
print(a * 10) A) 300
def f1(b): B) Error
print(b + 20) C) 50
f1(30) D) 3020
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q 10) What is the result ?
def mod(m):
m = m+1 Options :
print(m) A) 10 10
retun m B) 11 10
n = 10 C) 10 11
mod(n) D) 11 11
print(n)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q 11) What is the result ?
x = 10 Options :
def f1(x): A) 10 2 10
x=2 B) 10 2 2
print(x, end = ‘ ‘) C) 2 10 10
print(x, end = ‘ ‘) D) 2 2 2
f1(x)
print(x, end = ‘ ‘)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
THANK YOU
Email id :-
[email protected] Mobile number :- 9766791922/9405435264
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera