Lecture3_py774
Lecture3_py774
Apna
marks = [87, 64, 33, 95, 76] #marks[0], marks[1]..
College
student = [”Karan”, 85, “Delhi”] #student[0], student[1]..
length
List Slicing
Similar to String Slicing
Apna
included
College
marks[ 1 : 4 ] is [64, 33, 95]
Apna
list.append(4) #adds one element at [2, 1, 3, 4]
the end
College
list.sort( ) #sorts in ascending order [1, 2, 3]
Apna
list.remove(1) #removes first occurrence of element [2, 3, 1]
College
list.pop( idx ) #removes element at idx
Tuples in Python
A built-in data type that lets us create immutable sequences of values.
Apna
tup = (87, 64, 33, 95, 76) #tup[0], tup[1]..
College
tup[0] = 43 #NOT allowed in python
tup1 = ( )
tup2 = ( 1,
tup3 = ( 1, 2, 3 )
Tuple Methods
tup = (2, 1, 3, 1)
Apna
tup.index( el ) #returns index of first occurrencetup.index(1) is 1
College
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.
Apna
College
WAP to check if a list contains a palindrome of elements. (Hint: use copy( )
method)
Apna
College
Store the above values in a list & sort them from “A” to “D”.