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

Lecture3_py774

The document provides an overview of lists and tuples in Python, highlighting their characteristics, methods, and examples. It explains how lists can store different data types and includes operations like slicing, appending, sorting, and removing elements. Additionally, it introduces tuples as immutable sequences and presents methods for indexing and counting elements.

Uploaded by

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

Lecture3_py774

The document provides an overview of lists and tuples in Python, highlighting their characteristics, methods, and examples. It explains how lists can store different data types and includes operations like slicing, appending, sorting, and removing elements. Additionally, it introduces tuples as immutable sequences and presents methods for indexing and counting elements.

Uploaded by

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

Lists in Python

A built-in data type that stores set of values

It can store elements of different types (integer, float, string, etc.)

Apna
marks = [87, 64, 33, 95, 76] #marks[0], marks[1]..

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

student[0] = “Arjun” #allowed in

python len(student) #returns

length
List Slicing
Similar to String Slicing

list_name[ starting_idx : ending_idx ] #ending idx is not

Apna
included

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

College
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]

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

College
list.sort( ) #sorts in ascending order [1, 2, 3]

list.sort( reverse=True ) #sorts in descending order[3, 2, 1]

list.reverse( ) #reverses list [3, 1, 2]

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


List Methods
list = [2, 1, 3, 1]

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)

[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”]

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

You might also like