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

Ch-3 Working With Functions

The document discusses different types of functions in Python including built-in functions, user-defined functions, functions with parameters, default arguments, variable length arguments, scope of variables, and passing strings and lists to functions.

Uploaded by

King Rajdeep
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 views18 pages

Ch-3 Working With Functions

The document discusses different types of functions in Python including built-in functions, user-defined functions, functions with parameters, default arguments, variable length arguments, scope of variables, and passing strings and lists to functions.

Uploaded by

King Rajdeep
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/ 18

Page No.

21
Date

Functiohs'ti

Functioht

Tt is a set of Thstucion qiyeh by the


to perfortm specifte tosh.
Benefits

Reuse abiliiq of cade L


Redsce lines of code 2 2

Easy opdototiogb
Tupes cf Eonctiohs' o

Tn tulhon eate dealina uith 3tupes of Furctish


DRuilt in Functior-This function tahes Onta value
ahd cohuert it into ih.
Ea'- intC'12d)
23

Int cli23.45)
123
DOMS Page No.
Date

loat)- It corvert Inteaer and


into floaihapotkl.
Eq- loGt22)
122.00

flact C123-4S)
123-4S

Ihput c) H isised to accept


accep vole
4he uS

evalc- his function take a


and hUMeric Ke sult.
SS

maxc) minc)'- It 1etuns moimúm and


hihimum value.
Ear max cc
cC 10,S,0])
mih CL lo, S,0)
absc) This fuhctioh retuns the absokte volye
of Sinale hcmbet.
Ea't abs (-64)

tapeCl
of
This unetioh retsuny the dto ype
Orgument.
Eg ee Clo)
Class int
3OMS Page No. 2.3
Date

la) Retun the


eas="Hetla t lengih ot the obiet..
p t ClehCs))
Youhd ()- 1t rounds off he volve.
Eg round
KOund CI24S16)yhgn
kouhdcl2 452 2)
Krouhd(12 S8-S6t,-2)’ 130oo

nhe' This function is used to def he a seies cf ho


KTist,tuple is osefal for locp.
ig lisi(Ramge (s)
tuple(kangcC,d)
list Crange (s))
0WAPtocreáie a wSer define fiunc hon nume as F
Lhich printhe Fäbonati series úpl h nombers.
def F:

bl
pint(a,b)
hs int Cinpt(" Eiexh

print Cc, endi)


DMS Page
Date

Uesdag
tineieh
Write a funtioh in
the ho, of
and dispoy
lawecae.
def F
S= inpt(Erter StYíng
ftr n
ifi.

if i,isup per)
else:
|= 4 1
pint C" 1otal ho, of upper cose leiie s
prin Total ho. of IoleYcase letters is

Random Hodole.
To Kond om functíon inpt a libraty robe
as Yondom.

\ondomCli Tt aenetes
aeneratej olye
alye from0 tol.
pichs tan dom Volle Uhich is qteaterthan
eajual to 0 but less than l.
DO15 Page No. 2S
Date

Far Input rondom


print Cron do ra, randomC))
1Ondon. Yandint C)- This unction aertl tloo
pataheteIS a and b as a lGest and highet
MulnbEx, etums a value incluaibq tange Cá b).
humber,
h oh bolh are includina.
Eqr ondit (ab)

print Cranåom, ranaint,1Þ)


tond om. rordrange () This fonction acept the ianae
of value and defite displou the alye tkiy Ahe
Yuhaet con pich aha tandom humbex.
Eg- impart random
a= Komdom, randlarge)
prirt Ca)
xandom. urifotm() - This functior returhs a rahdon
loatlha poikt humber in betueen wo hunbers,

ais the lower límitotd b is the pper


lIrit af Kandom f loat, Tt Yeton flocting paibl
Nomber areoter than a or eaual to a
bt lessthan b ai
DOMS
Date Page No.s

Fidag
Usey Funcion
betined

kewold is Usedto (rediea uiert


Def
functioh..
Fonetidn whkh do not pass para meter ahd d
hot retun v a l b e t i

satemens

Example
Ldefiforitbt
no = int CinptcExtev h o : ) . d i
for i in Yange Cno, Q,):
iad fact= fat *i
t ' h prít C f á c i ) c ig o t i

23 Funtin rametey but do hot ttuk


hich pass putamete
Value.

def function nome(voriable ):


DOMS Page No. 24
Date

det son Ca,b):

printC)
(Jmto, 10)

-it Cinpt CErter: '))

dshs

volye

tlax

def -functish C) :
Stotement
retun value
pirt (Eunclloh C)

print (a)
92

for i in Iange
S=Sti
Yetun S
ptint CfCo)
DOMS
Date Pags No.

print CP)

Function Lhich paSs poraneter and xetutn


Nol e
def fonction rumc ( porameter)
stotemet
Yetun ylue
prikt (frcti ön Corguner))
P= functioh Cargumevt)
prirt CP)

Exompe
def s Ca,b)
teturn Cc)
I= Iht Cinput C" Enter: " )
s= 1ntcinput (" Erter: 1))
print (P)
DOMSPage No. 24
Date

Rolurn Multiple Values'

To Ketun muliple volue n fom a functior the


ctotement nside afUncon boda shoula be of the
form.
yetutn Voluel Value 2

Tt retun value in the fortm ot tuple.


def s)i

return
f=SCO

Scope of Voriable

def fc):

prml Cx)

print (x)

Local Scop
A Local Variabe Is Voiable defihe Gthih a
carnst be
fundioh,e cannst be accessed
accessed otside he fuclia
Date Page No. 0

print Co)

printcc) ne

Ghloal Voable

Variable Lohich is def ihe outsie


function ond con þe 0ccessed anuhere i
the Code

Global Vatable connot pe updoted ibside {ho


ike
fnction

Tuesdag Postiona Araumets

when the funetiun call stotemert aut hoth


he humbex
Chd adeY cf atqurert or dcfthed
in the fondion definotionis called tle
Positi onal Atqument,
DOM5Page No. 3\
Date

Aef iecord Chollna., Nore ):


pint Roll. ho
, nollno)
pritt ("Name =",hamc)
hamc)
xollho int Cinpt ( "Eter Rollho'")
name ihpt Erley Morei "9
reCOYd Ctollno, hamne)

KabgId Aauber

Keutagd Ataunentare the med arqrert


ith assianed vlyes belny possed in he
function Calledsttenertt
xetod Chome tall ho

Oefarlt Axaumcht

A poramcer haiha defaut alue ib o fottisr


as Defout Aiquinant.
AdvartagesT
Theucan beused to add hew paratneter
to he exictiry fontian
Can be usedto combine similat EUAdiGhs
DOMS
Date
Page No. 2)

deff Cx2E330):
priht(p)i

Welnedy
Keuuord Aaubent

Ifheteiya fuhction Lilh on parahe


ie luartAo specitaohl omet
ahd Valueforsok
them in stor call hch
provided by usih thir
patameters Can be theirpositlnctdc
hame instead af

deffoll ho, none)


piat ("cll -no-"toll no)
pint ( Name'-" Name)

Adyortages
5Ucina the fUNioh ith heacd atqomenti
ROS0eras e do hot to emenberhe
Otder cf he aroument.

default
Lhich Luart to Qs olhers have
aYgurert alues.
DOMS Page No. 3
Date

Vaviable lenalh Araubmeri

As he hone sugacsts, in certoh sitosttórs tue


Cah pass aiiable oxquments to a ohclíon.
def Sum (*h):

St
pint ( s ) b e
Surm (36)

Sum C1Q,10, 30)

Hot to hahe local variab\e alob


AA The qlcbal hetaaYd is Used to mahe a locol
Vaviable qlobal

print (o)

pxint (a)
DOMS
Date Page No. U

fohction
assiha ist to a

ae uloble. 1 f u00 nahe On


Lit inside ke fuhclhit Lil relle
in ist

def fi):
for i in tahqe(0,lenC):
prikt c)
1=eal CippC"Enler list:" ) ) i a

priht ci1)

Ditiahatq o a fhciioh
Posita
Keas ole immuhable, Values ote hulable.

def cd):
-for i in d
prirt&
Ca)
dl= eval Cinpt"Enler the Dictiohang: ))
prit Ca)
pitCa)
DOM5Page No. S
Date
slelze

Pasihg Shing Tunctlohi

def f Csting):
fox i in sitihg:

s= irpt (Erter string:


f6)

Pasina List to a Functioh

def flL, 0hdex):


e= int Cihpstc Enter elemer: ))
for i lh Yange CO, len()):
if /--Indexi

l1= eval Cinpst Enler 1ist: )


i=in CinpiC'Ene T: ")
pist (i1)
pirtCIa)
DOMS
Date Page No. s

Passiha Tople to o fuctio

def c):
foy i in t:

pint Cs)
A= eval Cinpt C"Enter 1ople Elemenls "))

Nestcd Funcioh

A Function Liihin a fohcion is called Nested bdi.


defco:
print C"Hello')ol
def fic):
prirt("Wclcomel')

defiCab):
pikt ("a-", a, "'b-", b)
def Sumlacb)
pist Cat b)
def sob Ca,b)
pxrikt (a-b)
Sum CI0,4)
sub (10,4)
fCio, 4)
DOMS Page No. 34
Date

Usina moih() as a function

Le can add oin C) fondlon 1o oa picotam.

def hello):
prit " Hello" WorAM)
def maih :
pit (This is the main forctior)
helo)
maihC)

Oulput- Bsed Oueslion


A

heqtuotd
Global 4he
fonction allots
iatside furclon fsrdias
Frut
Di{feentigte Pprit
CCnol) ho=
Use Explairthe Fonctiohtelorn Ngn- defcn he
qloba ful int
lool ot Fruitful Iht Cn)lpE
oihIoCpCo3)
hae XE st ihitnum
is 1he Lith functión Cinput Estr(n)
value, qlobal
cortext,
heby A
proatom betcGECh C"
he fehloh vale
Ouide fondich Enter
defaulH
is
ittenhction is eywgId.h
hnguad Inhelp Fxitfel The
mer trfcl of
fohcioh :)
he variabledec to ot The lenc)): ht
local sultable
rcioh,ibside
USuse fonttioh fohchs humb
\hsidealobalby Puthon,
hat
vorableaalobul example
alobal the laied Date
DOMS
Global
Keyuatd ho letutn and
defauh. ke
keqLwodfuhction decihslde a do hoh-ftut Pagp
No. 3
Voioble a
kte. aia hot
ho to olue
u

You might also like