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

Python Lists Class 11

Uploaded by

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

Python Lists Class 11

Uploaded by

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

Python Lists

• 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

2- Using range function


Python Lists
• Copying & Nesting Lists
• Comparing Lists
• Operations on Lists
– Concatenation
– Replication
– Membership Tests
– Indexing
– Slicing
Python Lists- Copying
1- Copying a List by assigning/ aliasing
Python Lists- Copying
2- We can use list() function for copying:
Python Lists- Copying
3- Copying by slicing:
Python Lists- Copying
4- Copying by copy() function:
Python Lists- Nested Lists
When a list appears within another list, It is
said to be a nested list.
Python Lists- Comparing Lists
Each element of two lists are compared in lexicographical
( alphabetical or dictionary) order.
Python Lists- Comparing Lists
Each element of two lists are compared in lexicographical
( alphabetical or dictionary) order.
Python Lists- Operations
Python provides with following basic operations:
Python Lists- Operations
1- Concatenation: ‘+’ operator concatenates two lists
Python Lists- Operations
2- Replication: ‘*’ operator replicates a list
Python Lists- Operations
3- Membership Test: using ‘in’ operator we check whether
an element is found in the given sequence/ list or not?
Python Lists-Operation
4-List Indexing :A List in Python can be accessed using its
index numbers. From left side index begins with 0,1,2,3,….
And from right side of the list it begins with -1,-2,-3,-4,…..
e.g. List=[1,5.2,96,’FACE’,’Python’,8.6]
Python Lists-Operation
5-List Slicing : Extracting sub-parts of a given list is
known as slicing. It is done using indexing.
Syntax of slicing is: List [start: stop : step]

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

Adds new element at the end of the list.

Converts any collection type to list

OUTPUT
Python List’s Functions
index(), insert() and reverse() functions:
Returns the index of the
element.

Inserts an element at a index

Reverses the order of


elements in the list

OUTPUT
Python List’s Functions
Difference between extend() and append() functions:
append() - extends the list
by 1 element at the end.

extend() –iterates over its


parameter and adds each
element of the parameter as
a new element of the list.

OUTPUT

extending a number or non-


iterating item is not allowed.
Python List’s Functions-II
• Built-in Functions (List
Manipulation)
• remove(), pop(), clear(), sort(), copy(),
functions
• max(), min(), count() functions
• del statement
Python List’s Functions
remove(), pop() and clear() functions:

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

copy() –creates and returns a


copy of the list, but cell
address changes, but copying
using assignment does not
change the cell address.

OUTPUT
Python List’s Functions
min(), max() and count() functions:

OUTPUT

min() –returns lowest element


max() – returns highest element
count() – returns no. of times
element appears in the list
Python List ( del statement)
del Statement:
del statement: deletes the
specified list of element, slice or
complete list

OUTPUT
Thank You

By: A.K. Pandey

You might also like