CHAPTER 3 : STRINGS
Chng s data ty in Tyhou.
Singe qucki hny Hetto
2. Double quctd ttiu b= *Heito"
shirg siclog
name = ’ ugh 5
indu o! 2 3 4
-5 -4 3 2 - <Native Dndics ’ wel hen yeu've
to de sonelkuny fum nd.
The indu in striug stats v 0 to (auyth -1)
sf: nowt indx.sBart : index. end]
fut ind included evd index ncluded
s [0:3] - 1etusus
Sma -’ chuyates frot 0to 3 indices
havact fvam 1ta 3 indices
si [13] ’ etuus
CHAPTER 2: VARIABLES DATATYPES
in
qivtu fo o wtmovy Lncaion
b:Hmy"
DATA yPES:
ex: 2,-l,5
2 Clotig tout wawbtu (X 2.357, -2.5
compufer . "Python "
4. Bocleaut er: TRUE Y FALSE
5. None
40 QMagig aud tutelliqut auguage hat it
outtutiatly ldouifes the ype 4 data.
a = 57
b: 8.54 class <float>
nowe Python" ) ldeuhiu name u clar< sty>
cormmut Written line Muti 2
iua slngle 1
Tython. cowmeut
in d, tybes tuso ae 1heut
COMMENTS:
flack,
otttn:orflou),
e et
obc, 0s,
P iwalU toMed wodule Cxiena
trtheu od pre.
ininetat! wrtuto1 nBuilt
Tythou! in
wuduler tyber
of two a hevt
MODULES: 0f
TYPES
lly Python ?
- Easy to wndexttand Less develohatut tit
- Free aud oheu aowce
- Higl Lvel lauguage
-Torta ble Wovkt om inux/ Widou /Mac
CHAPTER 1 : MODULES, COMMENTS 4 P1P
MOpULES:
sOML.OME edte (usualby) tuich cau be imborted and
þYoaus uth
used in 0
- Thy OkE pre -inttallad in Pythou
PIP:
Pif d the tackage wmDuDger for pytho
tetall a modula
bih do
cqytem
Ls pit lasta!
ex: pl iustall flosk intall1 flask wodule
Sticing with skip value
wt cam pkovide a skip Valus
ike
woYd,: "aMazi
YeBuYns mzn"
kil index (it ill skip eueny next 2d indas)
stavt index
end index
othe. odvauced sicug tchmua
woYd "omazing'
"o
word [o!1] amazing
T2sord [ :7]
waYd [o:]
shring funchont 293TAAHD to
wed fuchen ferfena obevaliou
wonihulate stiugs
1. Ln) fuuchen
Ju("altsa) atus 5
enduth l) fuvch funchm telts whelhe le
2. shrig shrng.endatith v wof.
vaiahle striug euda
4"olexa heu atng enduwith ("xa wid
tYue odana' ende with 'xa.
o towwwd us data. ype
wuy funthrn!
40 auothe
str(31)
itl"1) 31tim to indgu canvuitn
Hlont( 92) 32.0
iua ud 3! is anuwmele itea
Huu 31 a stiug
input () function
is fuuchm olous thu as to taks input froe
the kuyubYd
"pythen"
MUmbu)
typrcaut it .
a: int (intut ("fnter mumher "))
oPERATORS IN PYTHON:
A0me cuwwow oheator in Tythous
1. Avith wthe OuatoY 4, ,/,%, ete
2. Actgmutut Gpouator =, t=, -*,*,/=,et
3. Conuasisau Ohouatove =*, >, <, >=,<*, !=,ete
’ and, 0Y, not
typel) fumnetion aud 1ypecarting
Ayþe fuuch m da uand t6 tind the data type a
in Tytuo.
typela)
b: "3"
type ( b)
Lhring and vice-vwa
(tF POSs |BLE)
TUPLES 1N PYTHON: 782ART
mmutiblr data tyhe tn tyon
canot be chavged daiusd
tngty utle
a (1, ) -uple wttth onty cne elewwut nedi a conamd
a.(1,7,5) - t l e ith wusve thoua Bne
Cnte datined a tufte's alemeut camot be altered m wmaupáled
TUPLE METHO0S:
a- (1,7, 2)
1 occuYtd
1) 0.couutl1) ) Thes
in tusle.
hetusu the index AtYst 0CuYMee
2) a.Index (1)
! in a
CHAPTER:4 - LISTS AND TUPLES1Y1 HE 29UT
Jists Qe containeY¢wch can atore o st
Python
mlist ['ApleAkash', 27, 3.21. falie]
int toat baul
wt Can change the
string
lomeuts 4 te t by vassou netluods
A ist cau be indexed just ike a ating
L1 ( 7,9," alexa"1
L1 [0] 7
LI[1] 9
L1 [70] Erroy
L1 fo:2] [7.9)st sicig
List Methods
conviduing allaaig
1 (1,8, 7, 2, 21,15|
1) L1.sort) > bdates the ist to [1, 2,7, 8, 15,217
1.vrge ) ’ usdatu the ust to [15,21, 2, 7, 8, 1 1
the end a List.
3) L4.o}kend 8) > adds 8 at
insert l3, 3) This i l odd 8 af index
4) L1.
w! dalete the elmut ot indur 2
5) 11. po} (2)
21 fTonm ho ust
6) 1.YewBNA (2)) )
3. shivg. cauut 1
character
aviug-cauu("k")
thing,. cout ('aie)
4. stving. cabiBatize () isunchan cpi talizes tht hst
charackv a giveu ating
5. string find ('ard) ’ hw funchim seluw tht index 4
fivt occurauee hat wod in the striut
6. shing. veblace (atduovd, nesard) he fuuchn hehla eea
he otd-wovd with new-tYd In the eutive
ESCAPE SEQUENCE CHARACTERS
\n ete
nes Line tab shace Sig sing
bock slash.
quite
CHAPTER:5 - DICTIONARY 4 SETS
Dictiouauy u a cllechim key-value pairs
ula "kuy"vatue"
Ncoding",
*uorks "i00",
a ["ku"] ’piuts "value" 0HT 228
100
a ["it] ’ piuts [1,2,5]
Prouties Bylhon pichonay
i) Tt couot cantain dulicate keys
Dictionary Methods :
Covuiduwy :eo
olsing dictonay
warkr" [92,98,97) ?
CHAPTER :6- cONDITiONAL EKPRESSIDNsorsg
f dse and elf in THON
H else and elif atafewent ane
decist m takeu by Q rogaw cuet0 cevtal n Conditin
in 0UY C0de.
Synta:
i4 (conditim 1)
indertahi Cprint ("Yes") if condiien sa true
elif (condíien 2):
pHut ( " N u ) e f tondí hin 2
elce:
pintl"Moyse") othewise
Reloti ena operatYS
Relafinal ohenatrs aat vaed to evaluate condihoL
ivsde the if else ad elit tatemuts
Relahonad oben otoys OuN!
’ greate than equl to
LOGICAL OPERA TORS:
’tue 14 both openandu one true ei fala
’ tue If at Jeast one operond se ue else jale
uwbe eif atateut.
(at elce u erected ony H all te conditon gails.
N8A
AB A- R,
R’AUB Rt A
AnB - R+R+R
AUB ’
B
An R R
la retso
tereecirn in byset veluna (8,13) intevsechon s.vi)
8,2,3,3 il,
sete botth fem its
retuus s.uuion(
16,1
)
s 1et the s.cleav() iv)
Uwwed. olemeut the
set the trom elemeut bitay a
1,1,3 s5retusn )s.YemOve
(B)
4d) the ugth 4
(tht TctuTnA Lm(s)
> i)
ollaiug
set Conuideung
susmet Operafien
on
) a. items => Athusw a list (kuy, valuu) tuples.
i) a. keyst thuus a ist covtaiving dichionasy's key
i9 a. ubdaBe (
friendNi"3) ) update the
with sufsied kuy - value pa
Hev alok ill be retuAned
SETS JN PYTHON:
Set s a collectim 4 wan-yeþititive elumet
s set)
No repthn allowed
S.addl1)
S. add(2)
Qs wathematca sets.
Poferties:
i) sets ane wuOYdeYed .
i) sets aye unindexed > cauust acces eleutb ndx.i
) sets cOMwat coutain duplicatn valme.
CHAPTER:7 - LOOPS IN YTHON
In Python
) olite Loo
While ao:
Oyutor:
ile c ondilan :
lu ulite ooh, the condihm chected irst 1 it evaluates
body the uof executed otheuwise wot
Js Conlinued until the
Conditn bttawe falsa.
ek:
wile i5: ’ huts Yes - 5 twas
priut (" Yes")
it+
for Loop: F04
kist, tule shing CIterablel
for ite in e': prints 1,7 and&
print (itw)
Rauge duachim in Python
he saue fumchm in yton ds used to gemeate
we Cau
also ecify the tst, stop aud teh-size a folous
Tauge (ctart, step, steh size )
for i tn rawge (o,7) ’ Tamge(7) Can also be wed
print ( 1) ’ pit o to 6.
Foy Loo with else
An opibual else cau be wd i h a for soos
f the code be executed wher the
execues
Outut
l: [1, 7, 8]
7
print (item) Dore
else
inabeve defvd
we funci)
ut
inhutl),
ct. ,vaye), pmntU Au),
ue the bay Dowed
Tytun frusnt
in Already funchy inBuit 9
Pythou funchion
in Tyes
ca. fuuehon the
ueCuted wich
chw set exatt coutaiuug
the pant The
Defiutien: Funetion
call.funchen called w Dhis fumci()
aethesis jol euas
bgfltwed funchim he 4
the fuucion
ut we acall to uteneve
We
Cou sFunction
rogma. the auguhe
in
catled becaufuuchibu T
("Hi) prit
Fuuchous
RECURSIONS FUNeTIONS
4 CHAPTER:
- 8
he brenk statemtut,
btak
the
fo i in vaylo,i0o)
break
The eoutiue statemeut
Couthnut
Mevahm
Coutinue with
it iwtvucs the ogLauu to "skp tu iterahm
print (" Helo")
Conthue
print (i)
Pass stateuent
pas4
walki!
l- [1,7, )
ter item in !:
pars
Functiant oth arguMuts :
wock uoit we Cam ut thet valueA In the paentheis.
A funchien can ado utuu Vaues as oou belo
def qhut (name) :
9Y "Helo" + name
s paded to gret func n name
AOk
aF greet (Alo')
wiL ho coutain Hulo Alok
a
Default Paameter Value :
hawe a value
in fuuchon.
ApecityValuenae "strangoa" he ine contaitig
pased.
x:
det gret name:"strouge') :
At fuuchen body
qrut ) ’ Nae will be "staue in fumchion bedy (aejaut)
eet ("Atek) ’ Name wll be "At:k in funcim body (pawed)
Recursion
a fnchm wtica calls
a matheuwakca foymula a
a fuuchim.
factovial (n) nx factorial (n-1)
his fmchm c be
dek fa cteial(n)
4 n== 0 6 nt ’ BaAe cond" wich daeunt
vetun 1
else :
retun n factoviat (n-1) func" callig ts
factorial (4)
4 X fottoiat (3)
4 x [34a ctevial (2)]
4 2x [2x fachial( 1)]
4X 3 2 1
we slhould Unt Ycumi ston Nheu the juch cue iniwledy
0thty mAthods to vead the lu
veod a wd.
- Asadliw () veada ene in 4 ths file.
ahiting iles in Pythan
wnle W
it in ad atpeud umk to the ftle.
he tstaou's
cau bt calld wulife iwLD
with otatemeut :
The beat way to oeu aud clot 4he jle
cotth tatuwsut
autowathially
+ Dou't mud to wnle 4.doc
ît takU
2 paaouektfileuame aud mode.
open (" thiu tt',)
iluame
ohen a a built-in fuwchen
binay kuot
in toxt wode
Reading a file in ythan:
4: ohem this.tt") ohen the file in tad only wugda
tert f.Yead () Yend ts cowtut a stores in fext vaiakle
print ( text) pmut the tert vani ab te
f.close () claseu th fe.
also spe cify the wumbes d chaacte in
funchion: tvad (2)
b reads 4vet 9 chavacters
CHAPTER: 10 - FLE I/0
its catuds
The Ramdouu Acce1 MewoYry volakle ound al
teAwinats Lu trde
ale ot ouce a brogtau
pesist he data toeve we
data etored in a tored. duice A bytuo
A Hle
Progtaiu Cau hlk to he il by Heoding the content
couttut to it
in ythan
rogyaums RAM - Volahle progy TnA in
i!
sai
RoM : uôn-valahle file qets ub dated
1yer files:
Mheu
i) Text file (.txt, cet)
") Binang Htes (g, dat , ete) y sli ihas
ha a et qumctou for eadivg updatig ond
deleting files-hbatwa sh heiriheri