Class 12 : Review Of Python Basics

Home Page class 12 @ Python

Class 12 : Python

Python Basics
Data Types
String
List
Tuple
Dictionary
Flow of Execution
Mutable and Immutable Types
Type Casting

Class 12 : Python Revision MCQs

Revision Tour MCQs

Class 12 | Revision Tour 7

By: Archana Shukla and Rajesh Shukla

Lists Contd…

Loop through list

l1=[10,20,30,40,50]
for i in l1:
print (i)

check if the item is in the list

l1=[10,20,30,40,50]
if 10 in l1:
print(“yes it is in the list”)

Built-in list functions

FunctionDescriptionExample
len(list)Returns total no of items in the listlst=[10,20,30,40]
print(len(lst))
4
max(list)Returns the item with largest value in the list
min(list)Returns the item with smallest value in the list
list(seq)Converts a tuple or string into a list
sum(list)Adds all the numeric values present in the list

List Methods

MethodDescriptionExample
append()Adds an element at the end of the list>>>flower=[‘rose’,’lily’,’lotus’]
>>>flower.append(‘jasmine’)
>>>flower
[‘rose’, ‘lily’, ‘lotus’, ‘jasmine’]
index()Returns the index of the first element with the specified value>>>flower.index(‘lily’)
1
insert()Adds an element at the specified position>>>flower.insert(2,’sunflower’)
>>>flower
[‘rose’, ‘lily’, ‘sunflower’, ‘lotus’, ‘jasmine’]
sort()Sorts the list>>>flower.sort()
>>>flower
[‘jasmine’, ‘lily’, ‘lotus’, ‘rose’, ‘sunflower’]
remove()Removes the item with the specified value>>>flower.remove(‘lotus’)
>>>flower
[‘jasmine’, ‘lily’, ‘rose’, ‘sunflower’]
reverse()Reverses the order of the list>>>flower.reverse()
>>>flower
[‘sunflower’, ‘rose’, ‘lily’, ‘jasmine’]
pop()Removes the element at the specified position or the last item if the index is not specified>>>flower.pop(1)
‘rose’
>>>flower
[‘sunflower’, ‘lily’, ‘jasmine’]
clear()Empties the list or Removes all the elements from the list
count()Returns the number of elements with the specified value>>>l1=[1,2,3,1,4,5,1,6,1,8]
>>>l1.count(1)
4
extend()Add the elements of a list (or any iterable), to the end of the current list>>>lst=[10,20,30,40]
>>>lst1=[50,60,70,80]
>>>lst.extend(lst1)
>>>lst
[10, 20, 30, 40, 50, 60, 70, 80]
copy()Returns a copy of the list>>>l1=[‘a’,’b’,’c’,’d’,’e’]
>>>l2=l1.copy()
>>>l2
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
del keywordRemoves the specified index
Or removes the complete list if the index is not specified
>>>l1=[‘a’,’b’,’c’,’d’,’e’]
>>>del l1[1]
>>>l1
[‘a’, ‘c’, ‘d’, ‘e’]

Joining two lists

We can use + operator to combine two lists. This is also called concatenation

n=[1,2,3,4,5]
n1=[6,7,8,9,10]
print("list n")
print(n)
print("list n1")
print(n1)
print("n+n1")
print(n+n1)
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(n+[10,20,30])

Output

list n
[1, 2, 3, 4, 5]
list n1
[6, 7, 8, 9, 10]
n+n1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 10, 20, 30]

Nested list

list within a list

n=["computer",[10,20,30,40]]
print(n[0][0])
print(n[0][1])
print(n[0][2])
print(n[0][3])
print(n[0][4])

print(n[1][0])
print(n[1][1])
print(n[1][2])
print(n[1][3])

Output

c
o
m
p
u
10
20
30
40

Number Of Visitors

0270461