0% found this document useful (0 votes)
5 views

Python Lecture 3

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Lecture 3

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Click to edit Master title style

Python
Lets learn something New

1
Click to edit
Lecture 3 Master title style

LIST
TUPLE

2 2
Click to edit Master title style
List in Python
A built-in data type that stores set of values
It can store elements of different types (integer, float, string, etc.)
List is mutable…..

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


student = [”Karan”, 85, “Delhi”] #student[0], student[1]..
#marks[0], marks[1]..
student[0] = “Arjun” #allowed in python
len(student) #returns length
3 3
Click to edit Master title style
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]

4 4
Click to edit Master title style
List Methods

list = [2, 1, 3]

list.append(4) #adds one element at the end [2, 1, 3, 4]


list.insert( idx, el ) #insert element at index
list.sort( ) #sorts in ascending order [1,2,3]
list.reverse( ) #reverses list [3,1,2]
list.sort( reverse=True ) #sorts in descending order [3,2,1]
list.remove(1) #removes first occurrence of element
list.pop( idx ) #removes element at idx
5 5
Click
Tuplesto
in edit Master title style
Python

A built-in data type that lets us create immutable sequences of


values.
Tuple can slicing Like list

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


tup[0] = 43 #NOT allowed in python

6 6
Click to edit Master title style
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

7 7
Click to edit Master title style
Lets practice

1. ask the user to enter names of their 3 favorite movies & store them
in a list..
ans: m1=input(m1),m2=input(m2),m3=input(m3)
MOVIES=[]
MOVIES.APPEND()
2. check if a list contains a palindrome of elements. (Hint: use copy( )
method)
[1,2,3,2,1] OR [1,’ABC’,’ABC’,1]

8 8
Click to edit Master title style
Lets practice

1. count the number of students with the “A” grade in the following
tuple.
(”C”, “D”, “A”, “A”, “B”, “B”, “A”)

2. Store the above values in a list & sort them from “A” to “D”.

9 9

You might also like