0% found this document useful (0 votes)
5 views14 pages

CS ch-2

The document discusses user-defined functions in Python, explaining their purpose and syntax. It provides examples of various functions, including calculating the area of a rectangle, determining if a number is odd or even, and finding the factorial of a number. The document emphasizes the importance of functions in making programs more readable and manageable.

Uploaded by

qwertyuiopouw
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)
5 views14 pages

CS ch-2

The document discusses user-defined functions in Python, explaining their purpose and syntax. It provides examples of various functions, including calculating the area of a rectangle, determining if a number is odd or even, and finding the factorial of a number. The document emphasizes the importance of functions in making programs more readable and manageable.

Uploaded by

qwertyuiopouw
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/ 14

L

Ch-2
Hser Defined Function in Bython
FunctionA funetiovn is a'Sub Progra m thot
COm be

anonedi uut of a
gau frarom statements and it.
Tis uut othe
Rort tte kroyom The oast ineAntaut
reasom
homdlisg eaieri a ioly a mall
tho Brogom isded witlata tim
Anote reasonito aseuntign is to
Yeduee hnogromga
Functis mose a pvogr9m Yeadable
amo wndestonolabletoat piog na m mad
Aerebg mabing pnagram mamage neuk
Syntax det fumction- name largumeata)
Statement
Ex
=2* 2**2

2) det Qbe U:
print ('Hello)

Caling/Irsaking
To Haat hoo besm
defined eorlier yau meacd te conite
a fnetim call
be invaeed olld
folloiae., tby int
oargumuts sehorated by Comm as

SymtoxKFumction-yisme>(Kvalye to be fasned to ang


gCalesO me ting(5)
)det Sum (x,y)):
add = ty
hri wtSum too nos. QAe add)

Sum too nos. le 36


QOAP to fim tha sum

def Sum (2y):


um :', odd

b=jut(uhut Eute seCona no )


Sum (9, b)
o. 2
Outus: Ente first
Sum
WAP to tinc
fid the anea of rectonqle by
Fumetiou

def areaot-rectongle (uy)

retongk:))
b nt (inut ( Eate breodth recdayla)
Entu bresdlh
Entu brsdh etengla t
Area rectongle s: 200
det qreatee ky2):

elset
hyiatlz)' is qratea )
X

22

uthut 1st
(.)AP for odd ov eVeM

det ooddorevem (n)


n)

hait ('evem)
else

n= int (intut Fute te ino.


Odd_or evenn)
Qutul Eute
Enter tt mo
odd

WAP to tind te grode ofot a studeuti


det qrode of studt (he)
hriut (Sade A+)
elif h>=80ncl fan< 90!
þriut (4rade A)
priat Grade R)
elsd
hrit ('FAIL I1)
her = flaat(input ('Eten Iag?)
qrode of studeut (hu)
Onthut Ende
FAIL L
for
JAP for n

tor a in ronge , m+1)!

matiralmo (on)

Outfut Ente tu mo. lo


23

Sum otm matural no.S


det moturalou)

hyiut lm Sum,Sum
)

|2 3
det Loble ca)
for b
heiat ( a * , , = ' , arb)
Heo Wo.
mO. s! )
(Toble
Toble (a)

for Su odo

def odd (a):


fote

Dtut Eutera
Sum = 26
WAP for Factoria ot Givem mo.
det fact ():
fat
for a (ne,-)
hriat n e , foct)

fact (m)
Etee a
6!= 120

def matyal (n):


Q=1

oie (aSM)

a:n i t i u (Eutes
t a
natu nal )
Qutut 10
12 3 6 6 7 8 9
Sum 65

wAP Sum rst m


det even a)

Sum O

Sumt

Qt 2
Sum

= jnt ufat (c Exde: a no. )


even (m)
6
Quthut
2

Sum 20
QWAP to fiual Table of
>>det Tablen)

iut CisAut('Ente
Table (n)

Output Easte the no.

Table ot 6

6 30
36

10
oAP to fiuo foctovial giveu
a stve
def taet (n)
mul =I
Loile (n >o)!

briut (sd Foctotial =,ml


Ma jutnBute Euser te no. ))

Outu Ente
Fctorial = |20

nested If

det for s):


in IyoMge
for
hriat( e m = ) )
hyint ()
riongla (j
det ti()

foy (0,a):
hriat (
hrint )

2.

3
|2 3

det notri 2
for a (O,5):

hriut ()
22 2
3313

def Rehetri ):
for
for b
hrint a, end= )
hriat ()
Rela t

3
6

de coumt- tri
tor
foyb
hriut (sum, en - )

Cat tri)
# Retuasly Valuetrou Function
det
steteuut

amme avguwts)
Syta Ye tuvn value

def abc (mn


retun mty
d= abc (3,4)
peut (d)
7

fetumy wadie fraa


det Sum m,n):
retuyy

add = Sum Q, b)
Gdd

You might also like