Python Lists Class 11
Python Lists Class 11
• What is List
• List Types
• List Creation
• Accessing the List Elements
• Traversal of List
By: A. K. Pandey
Python Lists
• List is an ordered and modifiable collection. Allows
duplicate values.
• It is a mutable sequence of python, i.e. we can
change elements of a list in place ( on the existing
memory cell reference).
• A List can contain values of mixed data type as well
as similar data type also.
• A List is a comma-separated values within square
bracket - [ ].
• List elements can be accessed using index numbers.
Python Lists
• Examples:
• [] # an empty list
• [1,2,3] # list of integers
• [2.4,4.5,6.8] # list of floating numbers
• [2.4,5,7.8,90,5] # list of integer & float
• [‘x’, ‘y’, ‘z’] # list of characters
• [‘a’,1,’ten’,5.6,20] # list of mixed values
• [‘Jan’, ‘Feb’,’Mar’,’Apr’] # list of strings
Python Lists
• List Types :
1- Empty List – List1=[] or List=list()
2- Long List- It contains several elements in multiple
lines.
e.g. List of squares of integers from 1 to 100
List2=[1,4,9,16,25,36,49,64,81,100,144,169,196,
225,256,289,324,361,400,441,484,529,576,625,…
.]
3-Nested List- A list enclosed within another list.
L1=[10,20,[30,40,50],60,70]
Python Lists
• List Creation :
1- An Empty list can be created as:
List1=[ ] or List1=list()
2- Creating a list from existing sequence (string)
L10=list(‘Computer’)
Print(L10) # [‘C’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
3- Creating List from Existing List
L1=[1,2,3,4,5,6,7]
L2=L1[:] # a copy of L1
L3=L1[1:5] # L3 contains element 1 to 4
L4=L1 # L4 is a copy of L1
Python Lists
• Accessing List Elements :
A List in Python can be accessed using its index numbers.
e.g. List=[1,5.2,96,’FACE’,’Python’,8.6]
Python Lists
• Accessing List Elements :
e.g. List=[1,5.2,96,’FACE’,’Python’,8.6]
Python Lists
• Traversing a List:
e.g. x=[10,20,30,40,50,60,70]
1- Using ‘in’ operator and for loop
Output
Python List’s Functions-I
• Built-in Functions (List
Manipulation)
• len() ,append(), index(), insert(),
reverse(), extend() functions
Python List’s Functions
len(), append() and list() functions:
Finds the length of the given List as
number of elements
OUTPUT
Python List’s Functions
index(), insert() and reverse() functions:
Returns the index of the
element.
OUTPUT
Python List’s Functions
Difference between extend() and append() functions:
append() - extends the list
by 1 element at the end.
OUTPUT
OUTPUT
remove() – deletes first
occurrence of an element.
pop() – deletes and returns
the last element
clear() - deletes all
elements
Python List’s Functions
sort() and copy() functions:
sort() - arrange the elements
in ascending or descending
order
OUTPUT
Python List’s Functions
min(), max() and count() functions:
OUTPUT
OUTPUT
Thank You