Indioduchon to problem
Matle 1: Compuha honal Problems
oovihm : slep by step prouduves to golve
pcblem s
Pseudocode :- High level.humanTeadable code
tike logjc
+fouchart Diaqg ammatc 7epresento' of
Sheps
* logcad Raasenng :- 0 o! bglc to daduct
Sheps 4 veriky Cotrecness
charectexshcs_ot a qood alaonith
*Input i- cors deined nputs
* Output- At least one cuput
* Dehiniheneness :- Eoch sBep must be
clecrs ey unambigausls delned.
* finkeness :- Nooihn must texmmal
altey a hnte n.o of steps
* Efechveness Each SBep must be Sirn
4 basic enough to be cariied out
manually ex bya machine.
Advantages c algoub
* Eass to urnder shand a duulop loqc
* languooe ndipendant
* Aelps in dabugjhg e4 testing
How to wrte an alooihm
*Use Sinple nghsh wth numbered
seps
x Aod Syntar ot progr amnmihg langay
E rite an alaox1thm to add two n.os
step 1:- sBazt
sBep 2:- head number A,B
SBep 3'- Sum = A+8
Step 4'- Prnt Sum
Step 5:-Stop
xcheck even os od usin Cond1hon
’1. Stast
2. Kead numberN to
3. If n"2= e0hen Print'Even
Else punt odd
4. Stop
* Wite on algonth
alqorth to hnd wehr he qven
or nagahue
N.o is poSthve ox
|.9tart
2. Redad number N
achech weherDeaaive
A>o then prnt postya
else punt pestve I Hse he Dhn
ZerO.
3.0 chech n=o hen pnnt zero
4 chech n>o Ihen print posthue clheng
prnt neoohve
5. Soppo ecoe
Flow chavt Basiceunbols
|. -siast/ slop sambol
J-inpat /oukut sqmbo!
3
4. - Decisltn Sqmbol
5. .olines
O- ConnecBes
+Daw a cbuw cha to fnd sum of two n.o
staot
Input vavable n
Sum = atb
Prnt Sum
sop
*Drau he Houwchart to hid wetheY the qven (3s
pos1hue or nogahue
(stogt
Input vaxable n
yes
hnt Poshve
Pint negahve/Print zes o/
Stop
DIaw low chart fo hrd the gluon
Stast
input vamIoble r
yes
Print
o/Pnt odd
sBop
Pseudocode
*aimple way of doxnbng an algpnlhm
using shauctesed nahvoi languaaqg
not bound by suntox rules ot any speuk
oogyamming languag
K Helps baide the qap blw the alaoaihm
algor1hn
achual proqxamming ode.
key lenhues of pseucoode
Rendable Nrltern in plain eoglsh like
\anquage
Shuctered
Not execukable
* Languqge tndopendan
* Used to planning
tormumon pseudocoda teyyosd
* shettstep sTART/sTOp
*thpat TNPUT T8AT
* dut ouTPUT/DiSPAy/ PBINT
* SET
IF /THEN/EISE
FOB - TO -po
WHILE DO
* REPEAT- UNTIL
¥ CALL
*Exanple Pseudocode to add two n.o
Algorilhm Add TawO Number
|.9TART
2. INPUT AB Num1, Num n
3. SET Sum <- Num1+ Numn g
4. D0sPIAY 3um
5. STOP
Pseudood to chech auen n.o s even or
odd
Alqorih CiUen- NumbeT istven04
|.START
2.IneNPUT dQLeGte N
3. S20 If
IF N mod 2 = THEN
DISPIAY "EVen"
ELSE
DISPIAy "odd"
4. STOP
* Pseudocode to check ita numbex is posihu
heqahue 0x zexo
START
2 NpuT N
3./1FN>0 THEN
OUTDNT
Alocvihm PosahveNeqahve
1. START
2INPUT N
3. IF N>O THEN
OUTPUT "PostiNe Number"
4- [SE NLO THEN
OUTPUT" NEqahve Number'
5. ESE PRINT "Num zero
6. END IF STOP
MODULE -2
NTHON PROGRAMING
PUthon
*most p pxoqv aming Ing Simple to use
poched wilh feahwes y ugported bya
wid xange ol lbraries Cy FrameLos ks
1s clean Syntax makes it beqirner hnd
Aequnes feur Iines of coce compared
Olher prograrmming languag
¥ High camand as it p oude many Job
ppeshunhes in sotare deuulopmn
K Used by qooqle , Nelix , NASA
Feahaes ct Rahon
*Easy to ead a rite
* *Interpreted language
* Object oNerted ay funhona!
* Dynarncaly tped
* ExBensive Libravies
fust Pyhon Rroqyam eranmple
Pint (" Hello World")
.File extension : . Py
Basic shuchure of aa phon pxogram
OComnerd (ophonal)
"Used to explan he Coda
. Start wth # bybr Snale - line Comments
# Ths proqtam greels the cuser and shows
a calulahon
Import stalumens (If needed)
" Touse extenal brares o modulas
Import mah
Tas hunthon Deknikon (ophonal)
Dehne Teusable blocss of ode
def qreef (rame)
Punt ('Helo", name)
(lonon
* WrHe pylhon tode hnd um of two
humbers.
’# To hnd Sum oF two number
Numbex 1- 2
Number2 =6
Sum - Numbes 1 t Number 2
Pant (Sum)
keyuords
and, as, assexB , breah, class , Conhnue def
del, ell, else, exapt, alse, hnals, ox,
bom, qlobal ,If,Imp08, in, lambda,None,
nonlocal, not,o, pdss, Yan,Tehurn, True,
hry, whle, llb, qjeld.
Data kupe basics
* oat- 3.14, -2.5
"hello', A
* bool - Tre, false
Nonéype -None
Use hupe (O hunchon to check
Pint Ctpe (5)) # Class 'nt >
Print (type ("uthon') # class 'str'>
Numenig hpes
. In:whole numbeys
float: o dcmal n.o's
(irnteger)
Complex: eq.
eg 3+5j
shings
* Enelosed in sngle ' , double "
hyle qoutes
* Can be ndar ed ou slicad
Aext - "Anauhcs'
Print (text (o]) # A
Prnt (text (2:6) * als
Posihon bom 2to
Booleans
t Loqical values :Tiue, false
* Usehul in Condihon a Compas iSOns
Prnt CX3) # Tue
nput output opoxahons
toaccepl npat hom usy
input("Entex your name:")
oe
age nt( input (" Ener upur
aqe"))
.fo displky output
-Print ( Hello", nama)
-Prnt ("you aye fage yeas cld')
#-Shing b formahng
to
prnt number in beheen
perators
speual sunbol ued to peskosm opeahor
on vaabla eOles
* Avrthemahe opexalos
Anhemahe operaovs
Used fos hasIC mah operaors
,I, II, (bo dnsion), Cmod.
* *(Pouer
operator Example Result
Addihon
Substoo 5-2 3
4"2
DiNiSIOn Io/2
o
Floor divi I0/3 3
modulus
Exponent g**3
Comparison
used to compaye ualues.RehuYns rus or talse
=,|=, <,,>=, <=
Equal to
Not Equal to
V
greater lo
< Less han
Greater or eaal
|Less o equal
agcal operator
wxd to combine mulhple cond
and, Or, not
Asghument qperator
used to assahn values to varables
--.
Opevatons
Assighn Valu Y=5
|Add ey ass IghnX+ = 2
Subact k X-= 3
X
assghn
mulholy y avsgh
dwde
loor divide
|madulus
Power
XNrte pyhon proqramme toochech welher the
qtven number is even or odd
> number - int(inpat °Enter a
number:))
Print ("Number is even)
else:
Number is odd")
*Nute a pglhon code to hnd qrade of perhcalar
shudunt.
Read the name of he shudant
Red the marks of the shudent
colculate qrade bagd on marks
Prnt he name and qrada ot the Shudant
marks =90, A
marks =80,BbPO
narhso= 60, D
marks 50, f ieeil
lnput ("name of the shudant")
maks =int ("Enter mark ot the Student")
If marksan v
Pint (n e got Aqrade)
elif: I mayks = 8O
Prnt n' got Bqrade)
ett: Imarks > 0
Prant (n' got 'grade
Pint (ngot O
grade)
Dint (n got Fgrade)
Wrte a hon proqramme b perom
di arihemahe operahon
x to Compute dstance b<w tuo poinls
hom taking tnput ron
hom
>num1 -int(input("Enter frst number:)
num 2 - int nput"Eney Second unber :))
Print "Acihon", a+b)
Prnt ' Sabskachon'", a-b)
Pnt CMulhplcahon', a* b)
Pint"DvISion", ab)
Prmt('Moculus', a job)
Prmi ('Io dvsÝn, alb)
Rrt ("bporent ', a* b)
>X|= int (nput (nter X1? )) (ast)+(hy)
Xa int (npat(" Ernter X,:"))
Yis Int (nput (" Enter Ya":))
ya int (nput(" Enter Yz:"))
dlstante (Xz-X)**2 + (-y)*2
PrintDstance is :", distance)
While oop teut io ro r
* We Use his loop when we dont hnOw he
number of himes to iterate betoyehand
*Suntax:
While 4est. expre ssion
Bods of while
* Wite a
Pglhon programme to hnd H.
of hyst n nahiral numbers
Sum of Nnahal number usine
while loopin
n int (tnput ("Enter the nahural nunber:")
Sum a o
Nhile i-N:
Sum t =l
Print (The sum of rst fN nahural nun