Notes On Function and Modules
Notes On Function and Modules
fun cki en
#
Function's
Need ot fun ctien
what is fun ien:
Funcion i a blo ck of adt Ihat perlorm
spu lie task
)tollosing
AII Ingaly alatd stakem unt can be getuprd
tngthur in sing inity
un derstand and dabug
J) Repratiien a trquantly Heuird nde ian b.
araidad by usina untisn
9 Dividing Lonq progmm ino funcien alow us to
dubua he partb o program enu at a time.sR
funci on Deinitien
det mtssaqe
print ("HLtn")
orint This is Statkumund
print"Proqram inished."
print(Rent o tde.")
DATE
*3). de agtl):
ded deloaratien ):
print Programis tiniuhed")
print
rest o he cod
>funhion Ca i
fun cion cal is a Ath statemant that invekes
hen A unti i calltd, the gutier Controt
aet ee utd, altr teuti the CGntA goes back
print ('LuLky)
name
Erry ouwtur punhion cont acuss inn r fun cion data - memb un
det mt).age
print (1x )
Aotal waniable
uncien can change more S e r thLL
he values sl qlebal vaniable caunot b
yari cuble Chanad by ataida
# hlobd
b: 20 # (alo bal b 20
qlobal a
print la)
print l6) print a)
f1) print Lb)
a 20
OlP enor -variable "
b20.
torterentod btore
Enampl:
def 20:
print (ab) b=20O Low
+ Lora print (b)
printlo
riw100)
prit(a)
print la)
Retarn Statemont
more thas Zro
3ero Vle D th alin a
part of function
T4 is op tiÛn alit
al it atuuys inditate theend
alao td do ranser the controt
bark t6 th ealling pad
Syntax: return ValR
:it there cre multiple walu t hetur
ol tuple.
DATE
Example ) del
41) deihulot
rurn msa Catb
print (hillo priNt Sum is . )
rebulte n*tauln-1)
Loteteurn rswlt
print (ac5)
Arquument o} Function:
Argument he varioble d in functien
det un (arn):
print ( a r g ) l
un(Lu cky")
TE
Syntaai
orgumut.
Example d! add Cab) _de add Co. b, c)
deatbtC
printld
add. (5, 6) add 5y6)
*EOr -missing one
tuirtà posihia nal ar
boan 2> Dau Artåi ment
func CUalval3,)
DATE
3 Ktysord Argumenti
ktuyword aagment ClHealb0 relkered s Named
prink (tun.--doc)
Example: det tuni)
this is cdoc-string.
itis mnultlin
Print ("hello You'")
det unename a),
nom:
program
Evtny ilt with .Py entension ib a module.
the e nam in Used 0 impert pntiular
modul.
|Name Apace
nam
alasSmate
Date
Page
RAH)
hdm.
oucan inaunL
ueu nam wrd hor that ohects.
n Pythn eath nmodnle a namL,hs namspa
mD o it bintien and variablt.
(lobalLocal and Buit in Namespaei
qlohal ymamespate contain the odnte Lohith
Qunntly ene cuting
Ths Jocal namespace ç a namusp a on daining
the nomes
return nmhniteo
elst:
rturn 0
print("Eter Some nimber") hulin namespae
num= int input) # qabal nampau
itlevennum (num)= 0):
print ("Thi sauat Yoot of",numis" math. sart{num
cASSMAte
Date
Page
Private Nariable;
he varinblesAhat beon with Ao ndrstore
as priuoate voiobles
dttadd (anb):
aa=253
--bb = 342
Saue the file as Mo dule-1
mtiple e packoQes
packoges and modulta
A\onq uitt sub patkaaes ond modules, it
Contoios ohir python Sile andinit-py
For pe a holder, to ben packaq e, conpuborryt
houl be One -ini-Py yilele inn
packag and nib-padeag
My- Pacikage/
Hinit--py
subpacieoqtl/
int-py
-nodutel-py
-module2py
=module-5. py
-module-py
module-2py
=motrttpy
module-2.y
alAsSMAte
Date
Page
mport Hy-Packae.modlel
rom My.Packa a import modult-2
imparh My-Patkaat.. Subpackaqtl
Whon oe im port Moin Package thun thts
o to Create a Packagei
* (reating a Calulator or pr orming basic
opnationS
Step-l Crea a older namad My Hoth s. in
your ioorkina oldsr
p-2: Ihside ytaths directory create
gubdireories and initp le
mulla, b)osno
de mulaib):
print(Sa3bs =i )
classMate
Date
Page
inikp
initpy
Lsubta ctinn subla.b)
-Mul
--init- py
multiplcation pumwlaib)s
-Div
--init--py
division. py 1 diylas b)
Step-B NoCheate a cdirwer pogoum that
mport,HyMath Addaddiion
3kep g: impor MHath Suh subirackin
import MyMathsNulmuliplitatisn
impor MyMoths Div.diuision
MyMah. Add ad ditiomdd (20,to0)
MyHatths. Sub. Subtracion. Subl80,40)
MyMath. Mulnldipmul(20,s)
MyMathnsDi.divisiondiv (80, 20)
20* S 100
Ro/0 4
pragram inished
classMate
Date
Page
time
Sys
math
mai
yandom
shring
re
Socket