0% found this document useful (0 votes)
2 views15 pages

LISTS Python Class X

The document provides an overview of lists in Python, detailing their characteristics as mutable, ordered collections that allow duplicate elements. It explains how to create lists, access their elements using indexing, and utilize various built-in functions such as append, insert, remove, and sort. Additionally, it covers list slicing, replicating lists, concatenation, and accessing elements through loops, along with example programs for counting vowels and consonants, and calculating the sum and average of numbers in a list.

Uploaded by

Sumiran Heena
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

LISTS Python Class X

The document provides an overview of lists in Python, detailing their characteristics as mutable, ordered collections that allow duplicate elements. It explains how to create lists, access their elements using indexing, and utilize various built-in functions such as append, insert, remove, and sort. Additionally, it covers list slicing, replicating lists, concatenation, and accessing elements through loops, along with example programs for counting vowels and consonants, and calculating the sum and average of numbers in a list.

Uploaded by

Sumiran Heena
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LISTS (Mutable Collection Data type) :

List is a sequence of values of any type. Values in the


list are called elements/items.

•List is enclosed in square brackets.


•List is a collection which is ordered and changeable.
• Allows duplicate members.
•List are mutable means , you can change the
elements of a list in place and it does not create a fresh
list when you make changes to an element.

Ex : lst = ['a',1,'b',3,5,'zero']
Ex. LISTS
[] #empty list
[1,2,3] # Numeric list
[ 1, 2.5 ,3.7 , 9] # mixed numbers
[‘a’,’b’,’c’,’d’] # list of characters
['a',1,'b',3,5,'zero'] # mixed list
[‘Ram’, ‘sita’, ‘shyam’] # list of strings
[ 1,2,3,[5,6],4,7 ] # Nested lists
For creating lists :
-we can give list values directly in square brackets
Or
- use list ( ) function.
Creating lists from existing sequences (ways) :
1) lst = list (‘HELLO’)
print (lst)
Output : [‘H’ ,’E’ ,’L’,’L’,’O’]
2) Using List () function :
lst = list(input(“Enter list elements”))
print(lst)
# On execution you will see:
Enter list elements : 234567
Output : [2,3,4,5,6,7]
3) Using eval ( ) function :
lst = eval(input(“Enter list values inform of list”))
print (lst) #On execution you will see:
Enter list elements : [2,3,4,5,6,7]
Output : [2,3,4,5,6,7]
How to access elements of a list ?
A list is made up of various elements which need to be
individually accessed on the basis of the application it is
used for.
There are 2 ways to access an individual element of a
list:
1) List Index 2) Negative Indexing
List Index :
•A list index is the position at which any element is
present in the list.
•Index starts from 0, so if a list has 5 elements the
index will start from 0 and go on till 4.
Negative Indexing :
•Python allows negative indexing for its sequences.
• The index of -1 refers to the last item, -2 to the
second last item and so on.
Functions used on Lists :
1) append( ) : Will add elements to existing list.
lst=[ ]
[Link] (10)
[Link] (20)
print (lst) #output is [10,20]

Appending a existing list :


lstr=[30,40,50]
[Link] (lstr)
print (lst) #output is [10,20,30,40,50]
Functions used on Lists :
2) insert( ) : Can insert elements on position specified.
lst =[30,40,50]
[Link] (0,’ Ram’)
print (lst) #output is [‘Ram’,30,40,50]
[Link](4,45.67) #output is [‘Ram’,30,40,50,45.67]

3) extend ( ) : Can add multiple elements at a time.


lst=[30,40,50]
[Link] ([60,70,”Shyam”,”AI”])

# output is [30,40,50,[60,70,”Shyam”,”AI”] ]
Functions used on Lists :
4) remove( ) : Can remove element according to value
entered.
lst =[30,40,50]
[Link] (40)
print (lst) #output is [30,50]

5) pop ( ) : Can remove elements from end of list or the


index specified.
lst=[30,40,50,60,70,80]
[Link]( )
print (lst) # output is [30,40,50,60,70]
[Link] (3) # output is [30,40,50,70]
60 is removed from 3 position
Functions used on Lists :
6) index( ) : Returns the index of the value in the list.
lst =[30,40,50,60,70,80]
[Link] (60) # output is 3
7)count( ) : Returns the count of item that given as
argument.
lst=[30,40,50,30,70,30]
[Link](30) # output is 3 times , returns 0
if elements are not there.
8) clear( ): Removes all items from the list . Creates a
empty list.
[Link]( )
print (lst) # output is [ ]
Functions used on Lists :
9) reverse( ) : Function reverses the values of list.
lst =[30,40,50,60,70,80]
[Link]( )
print (lst) #output is [80,70,60,50,40,30]
10) sort( ): function arranges the items in ascending order.
lst=[30,50,20,10,40]
[Link]( )
print (lst) # output is [10,20,30,40,50]

11) copy ( ) : Function creates a copy of the list .


lstr=lst .copy()
# Both lists will have same value
List SLICING :
Slicing means extracting a sub part of the list.
We can use index positions for slicing.
Slicing ways :

Example list : lst= [10,15,20,25,30,35,40,45,50]


-lst [start index : stop index]
Ex. lstr=lst [3:6] #output is [25,30,35,40]

-lst [ start index : ]


Ex. lstr= lst[ 6: ] # output is [ 40,45,50]

- lst [ start index :stop index : step ]


Ex. lstr= lst[ 0:8:2] # output is [ 10,20,30,40,50]
- lst [ start negative index : stop : - step]
Ex. lstr= lst[-6:-1] # output is [ 25,30,35,40,45,50 ]
- lst [ start index : stop : - step]
Ex. lstr= lst[8:0:-1] # output is a reverse list of original
Replicating lists :
- lst = [1,2,3]
>>> lst * 3 #output [1,2,3,1,2,3,1,2,3]
Joining two list (concatenation) :
- lst = [1,2,3] - lstr = [4,5,6]
>>> l=lst + lstr # output l=[ 1,2,3,4,5,6]
Comparing lists :
- lst = [1,2,3] - lstr = [4,5,6]
>>> lst = =lstr #output is FALSE
- lst = [1,2,3] - lstr = [1,2,3]
>>> lst = =lstr #output is TRUE
Accessing list using loop :
1) lst = [‘p’ , ’y ’ ,’t ’,’h ’,’o ’,’n ’] # can be numbers also
for ch in lst:
print (ch) #output : all characters on new lines

2) lst = [‘p’ , ’y ’ ,’t ’,’h ’,’o ’,’n ’]


for i in range (0,6) :
print (lst[i]) # same output but using index
3) lst = [‘p’ , ’y ’ ,’t ’,’h ’,’o ’,’n ’]
for i in range (0,len(lst)) :
print (lst[i]) # same output but using index
4) lst = [‘p’ , ’y ’ ,’t ’,’h ’,’o ’,’n ’]
L =len(lst)
for i in range (L) :
print (lst[i]) # same output but using index
Program to count vowels & consonants in a list :
vo=0
con=0
lst=[‘a’ ,’b ’,’c ’,’e ’,’u ’,’i ’ , ‘o ’,’d ’,’t ’]
for ch in lst:
if ch==‘a’ or ch==‘e’ or ch==‘i’ or ch==‘o’ or ch==‘u’:
vo=vo+1
else:
con=con+1

print (“Number of vowels = ”, vo)


print (“Number of consonants = “,con)
Program to calculate sum of digits in a list :
s=0
lst=[1,5,4,6,8,9,7,3,4,2]
for ch in lst:
s=s+ch
print (“sum of numbers in list is =“,s)
Program to calculate average of numbers in a list :
s=0
av=0
lst=[1,5,4,6,8,9,7,3,4,2]
for ch in lst:
s=s+ch
av= s/len(lst)
print (“Average of numbers in list is =“,av)

You might also like