ÖðlGENT5
D
yTHON ROcRAMMING
ONE By
DAARSHANP
Reg No210924 205oI
I. Expldn în detaîl about (îsts , |?st opevators and list slices
AST:
¥Lîst Ts an ovdered Sequence of îtems . values fn he
are Called element /tems
* TE can be wYitten as a list of
Comma .Sepa atem
items (values) betueen 3uave biackets l]
* Ttems the lists Can be of diFferent data kyps
L18T OPERATORS:
Dndeing
3) Con cate nafon.
) RepetfHons.
s) upda.fing
6) Membership
) CompaviSon.
CREATE L1ST'.
n his way we can Create a
list at Compîle fme
£x: vaablename
a-
=Lvalues
[2,3,4,5,
S> pÉnt (a)
6>7 ,8,9,10
Lâ ,3,4,5t,78,4,0)
i) fNDEXING
* Acces he ?em în the
* Access he Hem n posiHon o
* Qccess he e
posion &
Pem în the last
ndeing
Ex: >s prÉnt (a[o])
element (usng negare
2
>S>pínt (aLs)
>pint Lar )
2)821CINa
e Rrifrg a pavt of he trot
[2,3,4]
>>pin t (a.[o )
L2,3:4.5 ,6.7,8.9,
3) (ONCATENATIoN
*fsf and pfnrg he itens by as l?yts
Ex: >-[2,3,4,5, 4,T,8,9,0)
t) REETITION. Í2,3,h,5,bs7,8,2,in, 20,35J
* Create a rmul ple oes of he
8arme I?sts
[20,30: 20,30,2o,30J
5) UPDATI NG
Updaig me lst wsing îndex values,
6) MEMBERSHIP
*Retun True ?f elenert 13 pre Sert în lst
false other re
>>>a- l2354)S,t,7,8,9 ,n)
Teue
) (oMhRISON
Retutn tue ff all elesnent3 in toh eknents
tetun false
fale
LisT SLICES
* LTst sIeîng a operaRon hat ertacts or Subse t
of element From an l?st and packnges hem as anohe list
SywTx: 1st name stavt: StopJ
L+stname sstav t :stop:steps)
Ex:
[q,8,7
2 what s dictonay 99 GRve an example
DÂeHonary P3 an unosdered collecton of elements.
An element n d?efonaru has a Rey: value pañr.
* All elements Tn dîcfonan are place d ?nside fe
Cuxly braces 7.e 13
Elements ?n d?c Honaries ar acce ssed Via key
ave
ave
not by her posîRons
* The Value oF aa d?coravy
oF Can be any
data type
* keys must be îmmutable data ype Cnumbes.
stngs, keple)
Opeva tons on deRonary:
* Aeessf ng an element
Update
* Add element.
* Membershfp
OPERAIONs ExAMPLE DESCRIPTIDN
>a-:"one", 2 two
Cra edfconay
atfing s one",2:
pint (a)
(reang Hhe d?ctonay
wi th elements of
d?Fferent data hype.
1:bne"> 2"tuo"
Accessîng he elements by
Pcessing
an element one?
using keys
keywotd : o
update
sS> pfint (a) Asgring a new Value to
key · 4 eplaces he old
{1ONE", 2:' tuo'y value by new Value-
add elements a[3] = thee?
s>pnt ca) Add new element ?nto he
dic
1:"ONE",2two 3thee tonavy with keys
Meherdip a-:"ONE", 2:"wo', Rekan True if he key 98
3: three °
present în d?conary .
Othexuise, re kun Fa'lse.
True
>>> 3 not na
Fal6e
to per fom lîneay and
3. write a
binay
Ry hon
Search
Progam
#Linear Seanch funcfon
def IRneav Search Carr, tayqet):
fov ? în range llenCar):
1f arrf]'z tavget:
Yehan ? # Ren
he Index of he target element
tekun-i#Rekun-i if he tavqet is not found
# Biray Beavch Func Hron
def binary-Seach CarY , taxget):
# Make Sute he arrag is Srted
arY SovtlO # Sort he array 1F Rt's not
low=o alteady Borted
hig =lenlar )-|
Nhhle buw 2= hRgh:
mid= Clow +high) / 2
Yetuunm?d #
Rekn the Rndex of he tarqet element
eltf a Lil] tagent
low =mßdtI
else:
high =rid
Yekn # Retaan - ?EFthe
he target
target i3 not Found
#Divex Cate
Yf_name =", -
moin":
aN = [3A,T,23,32 ,5, b2J # Example aray
taxget = 23 # Eample taget element
4Pex fom Rnear Seaxch
pint (" âhnear Bearch:)
result= Ifneax Seach Ca, tavget)
if result |=-|
pint CF"Element Found at ndeevesult)
e lse:
pRnt ("£lement not faund")
4+Pesfoim Binary Search
pint (°ln Bfnag Seavch: ")
Yesult -
bínay senchlam, taget)
pint (Fu Elenent Found at Snder vesult ")
else:
pnt (Element not found")
4. Discuss Sting func Rorn$ and Mefods n
func Ron3 deta?
3TRING fuNcTIONS AND METHODS
A method ?s a functon hat belongs to" an
object. dyNTAX TO AccE SS THE METHOD
Stfing na.me.mehod()
Ex: a="happy bir hday
hee,a ?s he sting name
nan
SyNTAX ExAMPLE DESCRPTION
a- Capftalizet) >>>a. (apt talizel) Capit ahze only he Frst letter
seing
a- upper C) s>a- upper)
tHapy Bi:hday' Change st?ng înto wpper Case
alower) >>>a.lower C)
thagpy bihdoy' Change
-case.
string înto lower
a- title() S>> atftlet)
Charge stng heof alltitle case ie.
fhst haxactera
Ye ap+ta ized
Ane wor
a-Suwapeaset) >a-Buapeasel) chage lowercae ehavncters to
HAPPy BIRTH DAY pperase of ice Vice verga.
a-splrt)
S>aspl?tc) texns a list of words
Ehappy','BRN hday' spavated by Spoce
a- cente (widta,
>ceter Ca Pads he
Speciied string
Fllchay" Lae
leg% is equal to 'w "
a- Coun ( Sub SS>a-count( happy) Yekn he numbet of DCCuences
sting)
aveplace (odireu) s a te place ("hapy sh Yeplace all he old
wih hew
new stfirgs sabtirng'
ish you hapgy btlhday'
ajin tb)
»b=hay Yekn a sti?ng oncatenated
>>afnCb) ilh he element of an
harp-p Pterable.(Hexe"a' fs he iterable)
aisupper C)
>al6upper C)
fa lse check wheher al he Case
basedcharacter of the
Stfr are uppercase
a9s louet) S> a.islowex)
True check wheher all he case
based charactey oF he
ings axe lawey Cose
a1s alphal) S> a?s
alphac)
False.True check wheHhes he sting
Consist s of alphabetc chanckes
only
>a.isatnum)
false check wheer he skRng
Conáist datt onby
aistihit C) check whe her he
false
Consist dhit ony
Rsspaeet)a
false
issapae) check wheer he sting
Consl uhite Space ony
afsIitle ) a.isspace () Check whe Ker he string
False Ps lttle (ased.
a. tart w?h "h) check whe he he sting
(substing Tue Starts wih Subskîng
a-end it,
>>>a.end with Cy check whehex he stvÉng
(Substerg True
end ui hh s8ubstring
a find (subtair) >> aFnd
hagy Yen Rndex of 8ubstR
?t found.othewise-I is
Yeterned.
lenca) > len (e)
Rekn the length ot ke
inca) >min(a) Rehan he fn?mum.
of he stirg . chatac.
max ca) max lo)
Rehn
fn he st
he
mamum Chanctess