0% found this document useful (0 votes)
9 views8 pages

Lecture3 Py

Uploaded by

cookieswarna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Lecture3 Py

Uploaded by

cookieswarna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

List in Python

A buIlt-In data type that stores set of values

It can store elements of dIfferent types (Integer, float,


strIng, etc.)

marks = [87, 64, 33, #marks[0],


95, 76] marks[1]..

student = [”Karan”, 85, “Delhi”] #student[0],


student[1]..

student[0] = “Arjun” #allowed in

python len(student) #returns


List Slicing
SImIlar to String slicing

list_name[ starting_idx : ending_idx ] #ending idx is not


included

marks = [87, 64, 33, 95, 76]

marks[ 1 : 4 ] is [64, 33, 95]

marks[ : 4 ] is same as marks[ 0 : 4]

marks[ 1 : ] is same as marks[ 1 :

len(marks) ] marks[ -3 : -1 ] is [33,

95]
List
Methods
list = [2, 1,
3]
list.append(4)#adds one element at [2, 1, 3,
the end 4]
list.sort( )#sorts in ascending order [1, 2, 3]

list.sort( reverse=True ) #sorts in [3, 2,


1]
descending order list.reverse( ) #reverses

list [3, 1, 2]

list.insert( idx, el ) #insert element at index


List
methods
list = [2, 1, 3,
1]
list.remove(1) #removes first occurrence of [2, 3,
element 1]
list.pop( idx ) #removes element
at idx
Tuples
Python
A buIlt-In data type that lets us create Immutable sequences
of values.

tup = (87, 64, 33, 95, 76) #tup[0],


tup[1]..

tup[0] = 43 #NOT allowed in python

tup1 = ( )

tup2 = ( 1, )

tup3 = ( 1, 2,
3)
Tuple
Methods
tup = (2, 1, 3, 1)

tup.index( el ) #returns
index of first occurrence tup.index(1) is
1

tup.count( el ) #counts total


occurrences tup.count(1) is 2
Let‘s
PractIce
WAP to ask the user to enter names of theIr 3 favorIte movIes & store
them In a lIst.

WAP to check If a lIst contaIns a palIndrome of elements. (HInt: use


copy( ) method)

[1, 2, 3, 2, 1] [1, “abc”, “abc”, 1]


Let‘s
PractIce
WAP to count the number of students wIth the “A” grade In the
followIng tuple.
[”C”, “D”, “A”, “A”, “B”, “B”, “A”]

Store the above values In a lIst & sort them from


“A” to “D”.

You might also like