0% found this document useful (0 votes)
12 views19 pages

Python Functions 2

Uploaded by

myth87654321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views19 pages

Python Functions 2

Uploaded by

myth87654321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Computer Science

CLASS-XII (Code No. 083)

DPS RKP Computer Science Department


Python Functions - 2

2021-2022
Python Functions Part - 2
Functions -
● default parameters
● positional parameters

DPS RKP Computer Science Department


Functions using libraries:
● Mathematical - math
● string

2
Default Parameter
A formal parameter/argument, whose default value is assigned in the function
definition is known as a default parameter/argument. Once we provide a default
value in a formal parameter/argument, it becomes optional during the function
call, which means it is not mandatory to pass that argument/parameter during

DPS RKP Computer Science Department


function call.

def Line(N=5): def Exec(Num=2,Times=5):


print(N*'-') print(Num*Times,end="*")
Line() Exec(3)
Line(12) Exec()
Exec(4,8)

-----
------------ 15*10*32*
3
Default Parameter - Rule 1
Default parameter(s) can not be before non-default parameters.

def Calc(M=2,N):

DPS RKP Computer Science Department


print(M*N) SyntaxError: non-default
Calc(2) argument follows default
Calc() argument

Calc(4,8)

4
Default Parameter - Rule 2
While using multiple Default parameter(s), if we provide value to one of the
default argument through a function call, it overrides the leftmost default value of
the default parameters.

DPS RKP Computer Science Department


def Exec(P=1,Q=2,R=3): 18
print(P+Q+R) 20
Exec(5,10) 6
60
Exec(15)
Exec()
Exec(10,20,30) Call 1: P=5,Q=10,R=3
Call 2: P=15,Q=2,R=3
Call 3: P=1,Q=2,R=3
Call 4: P=10,Q=20,R=30
5
Default Parameter

def Line(CH="-",N=4): To draw a line with N number of a given


character CH. Assume the default value of CH as
print(CH*N) "-" and N as 4
Line("#",3)

DPS RKP Computer Science Department


Line('*') OUTPUT
Line() ###
Line(7) ****
Line("Hello") ----
28
Line((2,7)) HelloHelloHelloHello
(2, 7, 2, 7, 2, 7, 2, 7)

6
Default Parameter
def Line(CH,N=4): To draw a line with N number of a given character
print(N*CH) CH. Assume the default value of N as 4
Line("*")

DPS RKP Computer Science Department


Line("#",8) OUTPUT

Line(5,6) ****
Line() ########
30
----------
TypeError

7
Default Parameter
To find the payment for a worker at the
def Pay(Hrs=8): rate of Rs. 50/hour upto 8 hours and Rs.
if Hrs<=8: 100/hour after 8 Hours. Assume 8 Hrs. as
return Hrs*50 default hrs. for worker
else:

DPS RKP Computer Science Department


return 400+(Hrs-8)*100
print("Worker 1 gets",Pay()) OUTPUT
print("Worker 2 gets",Pay(7))
Worker 1 gets 400
print("Worker 3 gets",Pay(9))
Worker 2 gets 350
Worker 3 gets 500

8
Default Parameter
To calculate per over average for given
def Avg(Runs,Balls=6): number of Runs and Balls. Assume the
default Balls in over as 6
return Runs/Balls
R=int(input("Runs in current over:"))

DPS RKP Computer Science Department


print("Avg Run per Over:",Avg(R))
R=int(input("Runs in current over:"))
B=int(input("Balls in Over:")) OUTPUT
print("Avg Run per Over:",Avg(R,B))
Runs in current over:7
Avg Run per Over: 1.1666
Runs in current over:16
Balls in Over:7
Avg Run per Over: 2.2857

9
Default Parameter
def Resident(Loc=['Delhi','India']):
print("Resident of",Loc[0]+","+Loc[1])
Resident()
OUTPUT

DPS RKP Computer Science Department


Resident(['New York','US'])
Resident(('London','UK')) Resident of Delhi,India
Resident of New York,US
Resident({0:'Pune',1:"India"}) Resident of London,UK
Resident("AB") Resident of Pune,India
Resident("Mumbai","India") Resident of A,B
---------
TypeError Resident() takes
from 0 to 1 positional
arguments but 2 were given
10
Default Parameter
def Loc(Place="India"):
print("Destination",Place)
Loc()

DPS RKP Computer Science Department


Loc("USA") OUTPUT
Loc(110022) Destination India
Loc(("Delhi")) Destination USA
Loc(["Pune","Mah"]) Destination 110022
Destination Delhi
Loc({'City':'Jaipur'}) Destination ['Pune', 'Mah']
Destination {'City': 'Jaipur'}

11
Default Parameter

def Do(A=1,B=2,C=3): # Do(C=3,B=2,A=1) OUTPUT


return A+B+C
6
print(Do())
15

DPS RKP Computer Science Department


print(Do(10)) 48
print(Do(15,30)) 600
print(Do(100,200,300)) AMARAKBARANTHONY
print(Do("AMAR","AKBAR","ANTHONY")) [1, 2, 3, 4, 5, 5, 6, 7, 8]
print(Do([1,2],[3,4,5],[5,6,7,8])) (12, 10, 34, 1, 2, 3)
print(Do((12,),(10,34),(1,2,3))) ----TypeError

print(Do({1:20},{12:90},{100:200})) # Type Error

12
Default Parameter
def Some(Data=[12,30]):
print(Data[0]*5,Data[1]*10)
Some() #1 60 300
Some("AMAY") #2 AAAAA MMMMMMMMMM

DPS RKP Computer Science Department


Some(['A',10,'B',20]) #3 AAAAA 100
#4 Data['Name'] <> Data[0] Key Error
Some({'Name':'Ajay','Marks':35})
Some(25) #5 Type Error
Some(2.567) #6 Type Error
Some("A") #7 Index Error
Some(True) #8 Type Error
Some(34*31) #9 Type Error

13
Positional Parameter
In all the previous examples of functions before this slide, we have been using
positional parameter(s)/argument(s), where the values of the function call are
assigned in the same sequence of the corresponding identifier(s) used in the
parameter(s)/ argument(s) of the function (i.e. from left to right).

DPS RKP Computer Science Department


def FIND(I,J,K,L): I J K L I J K L
print(I*J-K*L)
FIND(5,4,2,3) 5 4 2 3 40 30 20 10
a,b,c,d=40,30,20,10
FIND(a,b,c,d) a b c d
14
1000
14
Named/Keyword Parameter
Named/Keyword argument are used to specify name(s) of parameter/argument
with specific value(s) in the Function Call, so that it can assigned to a specific
argument(s) in the function. Its order does not matter if there is no other
positional parameter mentioned along with it.

DPS RKP Computer Science Department


def LEqn(x,y,a): x y a x y a
return x+a*y
print(LEqn(a=5,x=10,y=3)) 10 3 5 7 5 2
X1=int(input("X1:"))
25
Y1=int(input("Y1:")) X1 Y1 A1
X1:7
A1=int(input("A1:")) Y1:5
print(LEqn(X1,a=A1,y=Y1)) A1:2
17
15
Keyword/Named Parameter in Builtin
print(end="*") end and sep both are named
print("AMAR",30,end=" # ",sep=" @ ") parameters and so, which
one out of the two comes
print("CONTD!") first in sequence does not

DPS RKP Computer Science Department


A,B,C=20,"JAYA",3.25 matter
print(A,B,C,sep=" % ",end=" & ")
OUTPUT
print("OVER.")
*AMAR @ 30 # CONTD!
20 % JAYA % 3.25 & OVER.
NOTE: Named parameter will always be
after the positional parameter

16
Keyword/Named Parameter in Functions
def Amt(Price=100,Qty=1,Disc=0): Calculate discounted amount
return Price*Qty*(100-Disc)/100 for given unit price, qty and
print(200,Amt(200,5,10)) discount with default values of
print(100,Amt()) Unit Price as 100, Qty as 1 and

DPS RKP Computer Science Department


print(100,Amt(Disc=10)) Disc as 0.
print(100,Amt(Disc=5,Qty=20))
P=1000 OUTPUT
Q=5
D=10 200 900.0
print(P,Amt(Disc=D,Price=P,Qty=Q)) 100 100.0
100 90.0
100 1900.0
1000 4500.0

17
DOs and DONTs
def ReCap1(a,b,c=9): #1 Right
----
def ReCap2(a,b=90,c=9): #2 Right
----

DPS RKP Computer Science Department


def ReCap3(a=900,b=90,c=9): #3 Right
----
def ReCap4(a=900,b=90,c): #4 Wrong
----
def ReCap5(a,b=900,c): #5 Wrong
----
ReCap2(25,c=10,b=5) #6 Right
ReCap2(25,10) #7 Right
ReCap3(b=25,c=10,a=5) #8 Right
18
Happy Learning…

DPS RKP Computer Science Department


Thank you!
Department of Computer Science

19

You might also like