Python 4
Python 4
to store collectiorm of
"A list
in Python i5a way
items
te a list you ase the sguare byacke t C]
"To cYea
Outtome
Eg 1# Greate a playlist
2 playlist= [ vay'shubamulu
3" Vayishubaulu' Love i th the
4' Love wth the shape of you' Shape oE You'isey
5Hey Jude
Happy evening Ju de
let it Be Respected
8Joining &as
9 Dk my dear b Sis3
lo Respected si
11
q6t
prine (playlist)
ist can soievarious
s data types, mixed
data tyPes
"A list can Contain
a nestd is t
anotheslistThi s is (alled
Code 0utput:
1nunbers_list:
2print(numbers li'st)
9Splt( Method:e
"To transform a strin into a ist in Py thon, use the
stsing splht method This method divides the string base d
On
speciied sepera tor an dthen pu t S these
SmalleY par ts into tist.
"For in stonce consrdex the
string ,2, 3 To convey t
it into a ist 33 si mply use thepi4 me tho d
I, 2,3 spli t()
Thìs method sepesa tes the
String at
ever y comma.
Code!
output'
2: num beys s t = num bess_str.spli
t()
3' pri( numbers_ l's t)
Indexing Syntax
. To get
Speefe item "honn a tis
ti6t, you
two thing ,the home of the (is t
and the'
jndet number
the lis t e ach item
numbe Star tng trom
isassianed an index
" The inde
number ts
itens less than the
positi'on in the ists2
Code:
Output!
1.myist=fa,
2-prlnt ( my
isto)
" An ndez ErToY 0cCurS when
acceSS Qn inde number that is
ls t's rangc the
Negative Indexing:
"Lt is usetul hen you wan t to get i temg from
the end the is!
’ Thelast ite m i5 accessed
using list _iname
second last item a ccessed using
is tname 2), and So
Output:
APPend method:
Code!
Dutput!
my-lista
2 my- U's t
app ent
3 print (myis)
Insert method!
Code!
Output.!
2my'st. insert(1, b)
3 prin
(my ist)
I you in ser t ar i te m at an index beyond
the
The ite ist's
m 5i2e, Py thon won an ero
was adde d to Bhe e nd f the li's &
Extend method!
Code! butput!
Imylist d, }
2 myist. extend
(CU)
Remouing Items!
Pop Method
Code!
Outpu t!
1mylrst'a, b4,
2- my-listo pop(2)
3· prìnt(my. list)
the index i5 Out of
thoow an Indez Error Yanger Python o wil
Remove Method:
Code! Output
1-my-li'st a;]
2my-1ist. remove ('b)
B" prin t(my lis)
Sheuld
heeave tuoo pvimary
Code : Ou tpu t!
2- myList.emove (2)
opelnt Cmy.List)
Clear Method!
Codeico
2 my-is t clear()
3 print (m9-list)
Updotin9 ist
. To reaplae an item
in alist ,
a ne value to t 4ou a ssgn
at its specific index. Simply
Syntax
mylist Cinde 'nevalee' Otputi
Sode
2. my
3 print
we put index that doesn't exist t
Qn index eroY
Loops:
.When tasks in code are repetitive, i t's of ben
a sign that loopS Can be used to Sinnplify and
optimise the code
"There mainlg tuso types oP löDps in Python:
or loops Commonly used sith list s and used
when the repetition count s koon
’While lbops Conmonly used when
Yepetitionis
bosed a condit on and
anndwe
don't know thne exact repetition count
For lodp
.A or lDop
in Python goes
throug h each item
a list one by one, perfoY ming an
each then. actono7th
Code:
1· letters Outputi
2- to b,c.J
le tter tn lettxs
3:
printyette)
" Hexe
letteracts temporory variable
for each item in list
Indentation or Space betore we wri E Code
insl de the foY btd Ck is Crut al os indica tes the
s Cope in
Adding G'st Numbers:
Code!
11tems 6utput
t, 22, 33)
The sum of al|.
2item-sum= 0
ite ms is 66.
3for item in i tems
tem Sum : item sum
+item.
4 print( the sum of at
item sis iem_süm3)
Alternative method! Sum(J- Funiton
Py thon proides a buil tin tunction
findthe Sum called sum() to
all tem in a
list
Code: à
1 iterns ",2, 33) Output!
^ The Sum of,al tems S
item.Sum= Sum (items) 66
3 print (4"The sum -offall iterm s is $i )
tem
While ops
The Ioop storts si th the
" The cond iti on
keyood hile"
ensures the code in side the
*uns epeatedly as loeng as a specific hile loop
isnt
"Jp the Condi bon,
Conditon i5 met
me t the lbop Stops
Indentatisn(using y spaces) is impor t an t
We con use the wshile loop to print sequon ce of numbers
Code! Otput:
1number o
2 shile number < 9
3 print (number)
number= number +!
1numbes9
2. while numbeY >o
diyprint (number)
Ynumbers number-I
(wspaa)
GAessing a nunber singi Pyth onKáame)!
1" 5eret-number This is the secret numbes which the
user has to guess to win the geme.
Correct!
The cshile loop en 3ures tontinuously, only brea king shtn
the coYYet conditior is satisied
(Project
E-Commer ce Hnal Bill Calaula tes
Eenter pries
Seperated by comas ; 10,
The tinal bill:12.0 22, 4S