Adobe Scan 15-Dec-2023
Adobe Scan 15-Dec-2023
', \ I 11 , ), J, 4, S, b, 7, 8, 9]
l { I 1 1 :l, ,, I
111 '\\' l i ·l I l l t t •1 , ,, , (\ I t I ~
! 'l i I\ I I n t> W I l !, t )
Using * Qperator-with-l-ist-- I
l ht' " t'Pt't ,1h"H b usNi to n'plic,1te n list by n specific nurnber of times. With * operator, one operand has to be a ;
h~t ,rnd tlw otlw, should only be nn intege,~ otherwise it will give an error. For example,
Commands Output
A 11, 2 , J) [1, 2, 3, 1, 2, 3, 1, 2, 3]
pri nt(H )
X [S, 5, S]
A [X] " 3
p r i n t (A)
Commands Output
n a me " Amit " ('Amit', 'Amit', 'Amit', 'Amit']
Ll [na me ) " 4
prin t (Ll )
'''''
a as
1 ct J . . I a
n,•,1 • n
41fds- \ti
print([l, 2, b) !• 11 , 2 , 5) ) #Teue
pi-int { [l, 2, G) ' [l , 2, 5) ) #True
slicing a List
, 1. a list means_accessing a spe:ific portion of a list. This can be done by using a range of values with start
19
e\ number, end index number with colon in between. We can use both forward index and backward indeY t o
c::cify the range of slicing a list. Some important points to consider to do slicing in a list are:
, Elements from beginning to a range - list[: index] or [ : -index]
, Elernents from specific Index till the end - list[index:]
, Elernents within a range - list[start index: end index]
, Elements within range using step value - list[start index : end index: step value]
, Whole list in forward order- list[: ]
, Whole list in reverse order - list[:: -1].
For example,
word = ['E ' , 'D' , 'U' , 'C' , 'A' , 'T ' , ' I ' , '0 ' , 'N' l
0 1 2 3 4 5 6 7 8
E D u C A T I 0 N
-9 -8 -7 -6 -5 -4 -3 -2 -1~ •ckward - ~
,~c
I
Commands Output
Examples
word [ : J ['E', 'D', 'U', 'C', 'A', 'T', 'I', 'O', 'N ']
To display the whole list
or
word [ : : ]
or
word [ 0 : ]
or
word(-9:]
Displays the alternate elements print (word [: : 2 ) ['E', 'U', 'A', 'I', 'N']
or
word[-9 : -6)
Ol
word [: -6 J
Introduction to Python
__I 313 __.;:..:,.;._--
•~· ' T'j frc-.m tne .'C · ~•. T j
To a'spla, the ''st;., reverse oroer ,.a:::-~. : : - 11 ['N'. ·o·. ·r. ,·. 'A ', ·c·. ·u·. ·o·. ' E1
a Task
!f word = ['E', ' D 'U 'C' 'A' T 'I' 'O' 'N')
What will be the output of the fo!lo'lt 1ng !'1Structions·
• print(word[:1:-2])
• print(word[-1:H
• print(word[:-11}
---~-~---------~----~~- -------------=-~
Adding New Elements to a List
There are three different functions used to add elements in an existing list which are appe:;d < },
ex.:.e.:1d ( and i.!!serr. , J • Let us learn about them in detail.
••
"'ll'
314 I Touchpad Artificial Intelligence-IX
J
I (f I " iI j )
f'' I fl
, .. ,,,,, 'Ill l II' "1 r,, rl" I 11
t;f1l 1 Rf 1; l''tA ir11ll", •fl,1<1", "J l< •11tl , 111 H / 1111dnl J
ei,r:' , l:l l•l't 11 r.f ("ll1,11 r.ln" J
I
"' fl,,,
ij( ' "
,t1 II' I( II II 'I I l '' ( I ' 1 If )
rr j II I ( r.• 1 f fi )
Command• Output
Tl [ 1, 2 , , 4I (1, 2, 3, 4, SJ
m [ ]
n. X\ n (m>
print (n)
I d(W ct)
~r , (p
l \\i111plex:
o,,,1"•\
l, I l \) I • () I ll (I HI '\ I \\II
I, , ( 11 ('\ I l (~ f
I I l i II t ( 1,}
a 11 ~ \' II
l'lilll\L)
Exnmptr:
Commands Output
11•118 1 '0,40, ri()J JlO '0. 40 100)
lL[q •l OO
p1i11t(ll}
l l I l: ~J l " ab..;'',"~) "l / (10 ·i,bc' ·~y1.' 1001
p1int(ll) V
Removing Elements from a List
l he1e me two difter('nt tunctions u-.ect t\) rern1.J~~ d~n,ent, in cm t,:~htln1.J 11-.t t m ~, , ( ~ ctl\d \ • )l' ( ) lt,.t ' '
lcc11 n nbout thes(' function, m dt't,1il.
,,
,)
e>(1st5 : Thi
' clearo• f .
-L,_unct1on removes a 11 t he e Iements of the list in..one-go. It empties the list but the empty hst still
in Python which can be used later to fill the values. For examp 1e,
City"' [ " Delhi ", " Mumba i", "Ko lkata", " Ch e nnai " J
City
· Clear ( )
Pt ·
J.nt (c ity)
' OlJtplJt Will be· []
te" .
1 erseo: This function reverses the content of the list "in place". For example,
~ Ph r -
Cl "' [ I
<1lI-ih a ' , ,b , , , c , , , d , , , e , J
•
<l • tev
e.tsp ()
/
prin t (alpha)
, ' 'd' 'c' 'b' 'a']
Output will be: [ e' ' ' ' h' · d ... th I' t ·t If" d
. sorts t he rist ·in ascending or descending order. T 1s 1s one m e 1s I se an works for
• sort(): This function
the list with values of the same data types. For example,
_________________ -
Output will be: ["Mumbai", "Kolkata ", "Delhi", "Chennai"]
• indexO: It....____
returns the index number of the value given in t he function. For example,
city=[ " Delhi ", "Mumbai ", "Kolkata " , "Chennai"]
city . index ( "Kolkata " )
Output will be: 2
city.i ndex( "Pune ") c9'
Output will be: ValueError
• countO: This function counts the number of occurrences of the specified value in the given list. If the value
doesn't exist, then the function returns 0. For example,
9 Ato Glonccz
• A program is written in any programming language which the computer can understand and execute.
• Control structures are a set of instructions that controls the flow of instructions in a program.
• An algorithm is a step-by-step approach to identify and solve a problem in a finite time.
• A flowchart is a graphical representation of an algorithm.
• Python is a general-purpose, object oriented, easy to learn and high-level programming language.
• Instructions written in a source code that are executed by a Python interpreter are called statements.
• Tokens are the smallest meaningful unit of a program.
• Identifiers are the user defined names of variables, list, dictionary, tuples, classes etc.
• Variable is a name given to a memory location to hold a specific value.
• List is a collection of heterogeneous data arranged in a sequence.
• The elements of a list can be accessed by using its index number starts with o (zero).
• The + operator is used to concatenate the list with another list
• The * operator is used to replicate a list specific number of times.
• The comparison of the lists is done using comparison operators>, <, >=, <=, !=and==.
• The append( ) Function appends a single element with the given value(any datatype) at the end of the list
••
~~'
318 I Touchpad Artificial Intelligence-IX
,,,,,