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

Class 12th Python Lecture 5

Lists can store sequences of values of any data type. Lists allow mutable operations like appending, extending, and modifying elements. Tuples are immutable lists that use parentheses and cannot be modified once created.

Uploaded by

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

Class 12th Python Lecture 5

Lists can store sequences of values of any data type. Lists allow mutable operations like appending, extending, and modifying elements. Tuples are immutable lists that use parentheses and cannot be modified once created.

Uploaded by

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

Lists

List can store a sequence of values belonging to any datatype.

L: 'Hi' 3 2.6 ‘S’ ‘Two’

L = [‘Hi’, 3, 2.6, ‘S’, ‘Two’] or L = list(<sequence>)


Playing with Lists
■ Creating Lists

L = list(‘LockDown’)
print(L) [‘L’, ‘o’, ‘c’, ‘k’, ‘D’, ‘o’, ‘w’, ‘n’]

L = list( input(‘Enter elements : '))


print(L)
Takes one string, make each character as one element in List.
Playing with Lists
■ Lists vs Strings
Similarities :
Len( ), Indexing & Slicing, Membership, Concatenation & Replication

Differences :
Storage : Strings store single character at each block, Lists store references.
Mutability : Strings-Immutable Lists-Mutable
Playing with Lists
■ Appending Elements ■ Extending Elements
L=[2,5,17] L = [2,5,17]
L[3]=12 or L.append(12) L.extend( (21, ‘hi’, [3,6,7]) )
■ Updating Elements
L[1]=7

■ Deleting Elements
del L[2] deletes element at index 2.
del L[1:4] deletes slice of 1:4 i.e. 1,2 & 3 indices.
del L deletes the whole list.
Playing with Lists (other operations are same as strings)
■ Making a True Copy

Colors = [‘red’, ‘blue’, ‘green’]

L2 = Colors ❌
L2 = list(Colors) ✔

In first case, any changes in Colors will reflect in L2 as they both are
referencing the same list.
Lists Functions
Python also offers many built in functions for Lists.

● The index method gives the index of the <element>


L.index(<element>) exception error if not found.
● The extend method
L.extend(<list>) appends <sequence> into L.

append( ) & extend( ) discussed before


Lists Functions
● The insert method
L.insert(<pos>,<item>) inserts <item> at <pos>th index.

● The pop method


L.pop(<index>) deletes & returns element at <index>.
L.pop( ) deletes & returns last element.
Lists Functions
● The remove method
L.remove(<value>) removes the first occurence of <value>.
● The clear method
L.clear( ) clears the whole list,
[ ], this remains.
● The count method
L.count( <item>) counts the <item> & returns
Lists Functions
● The reverse method
L.reverse( ) reverses the whole list.

● The sort method


L.sort( ) sorts the list in ascending order.
L.sort(reverse = True) sorts the list in descending order.
Practice Time
Q1. List=[1,2,3,4,5,6,7,8,9]
print(List[ : : 3])

Q2. What will be the output of the following code snippet?


a. A = [ ]
for i in range(1,4) :
A.append(i)
print(values)
b. b=[ 1,2,3,4,5 ]
print( b[3:0:-1] )
Tuples

Tuples are basically immutable Lists.

Lists are made by square brackets [ ].


Tuples are made by curved brackets ( ).

t=(1, 'hello', 3.2, ‘w’)


t=tuple('Harsh')
Tuples vs Lists
Following things are same as before :-
● Creating
● Creating using input
● Indexing & Slicing
● Membership
● Concatenation & Replication
● Traversing
Unpacking a Tuple
T = (1, 2, ‘A’, ‘B’)
w, x, y, z = T variables on the left side must match
the number of elements in tuple.
print(w,x,y,z,sep=’*’)

OUTPUT : 1*2*A*B
Tuple Functions
● The len method (same for string & List too)
len(<tuple>) returns length of the tuple.

● The max/min methods


max(<tuple>) returns max element in the<tuple>.
min(<tuple>) returns min element in the<tuple>.
Tuple Functions
● The index method returns index number of <item>.
t.index(<item>) exception error if not found.

● The count method


t.count(<item>) returns the count of <item> in the tuple.
● The tuple method used to create tuples.
t=tuple('abc') (‘a’, ‘b’, ‘c’)
t=tuple([1,2,3]) tuple from list
t=tuple( ) empty tuple
Practice Time
Q1. t = ‘a’ , ‘b’
t2= (‘a’, ‘b’)
print(t==t2)

Q2. What is the length of this tuple?


t=( ( ( (‘a’,1 ), ‘b’, ‘c’ ), ‘d’, 2 ), ‘e’, 3 )
Q3. A= ( ‘Hello’, ‘Harsh’ , ‘How\’re’, ‘you ?’ )
( a,b,c,d ) = A
print( a, b, c, d )
A = ( a, b, c, d )
print( A[0][0]+A[1][1], A[1] )

You might also like