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

Revision Tour - II

Uploaded by

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

Revision Tour - II

Uploaded by

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

About Us Website: https://www.learnpython4cbse.

com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

Revision Tour - II

SuperNova-LearnPython,, a YouTube channel dedicated to helping students to learn Python


and computer science concepts.
The channel covers various topics related to computer science, including Python
networking, SQL and many more.
programming, data file handling, computer networking
If you’re looking for video descriptions, notes, assignments, and previous years’ question
papers related
ed to Python and computer science for class 11 and 12, I recommend checking
out the SuperNova-LearnPython
LearnPython channel on YouTube.

You can find the channel here1.


Happy learning! .
Please like, Subscribe and share the Channel

BY: AMJAD KHAN Page 1 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

Python: String

What is String?

A string is a sequence of characters.. Strings are basically just a bunch of words.

Strings are immutable. It means that the contents of the string cannot be changed after it is
created.

A literal/constant value to a string can be assigned using a single quotes, double quotes or triple
quotes.

Declaring a string in python

>>>myString = “String Manipulation”

>>>mystring

Output: String Manipulation

Traversing a string:-

Traversing refers to iterating through the elements of a string, one character at a time. A string
can bee traversed using: for loop or while loop. For Example:
myname =”Amjad”

for ch in myname:

print (ch, end=‘-‘)

The above code will print:

A-m-j-a-d-

Access String with subscripts:

Let us understand with the help of an example:

BY: AMJAD KHAN Page 2 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

myString = “PYTHON”

OUTPUT:

Important points about accessing elements in the strings using subscripts


subscripts:

Positive Index 0 1 2 3 4 5
myString ‘P’ ‘Y’ ‘T’ ‘H’ ‘O’ ‘N’
Negative Index -6 -5 -4 -3 -2 -1

Positive subscript helps in accessing the string from the beginning

BY: AMJAD KHAN Page 3 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

Negative subscript helps in accessing the string from the end.


Subscript 0 or negative n (where n is length of the string) displays the first element.
Example : A[0] or A[-6]
6] will display ‘P’
Subscript 1 or –ve (n-1)
1) displays the second element.

More on string Slicing:-


• The general form is: strName [start : end]
• start and end must both be integers
o - The substring begins at index start
o - The substring ends before index end
The letter at index end is not included
Let us understand with the help of an example:
Positive Index 0 1 2 3 4 5 6 7 8 9 10
String A P y t h o n P r o g
Negative Index -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

Lets we consider a string A= ’Python Prog’


S.No. Example OUTPUT Explanation
The print statement prints the substring
1 print (A [1 : 3]) yt starting from subscript 1 and ending at
subscript 2
Prints the substring starting from subscript
2 print (A [3 : ]) hon Prog
3 to till the end of the string
The print statement extract the substring
3 print (A [ : 3]) Pyt before the third index starting from the
beginning
Omitting both the indices, directs the
4 print (A [ : ]) Python Prog python interpreter to extract the entire
string starting from 0 till the last index

BY: AMJAD KHAN Page 4 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

For negative indices the python interpreter


5 print (A [-2 : ]) og counts from the right side. So the last two
letters are printed.
It extracts the substring form the
beginning. Since the negative index
6 print (A[ : -2]) Python Pr indicates slicing from the end of the string.
So the entire string except the last two
letters is printed.

Operaion Performed on String


Operators Syntax Description
+ ( Concatenation) Str1+Str2 Adds or join two or more strings

*(Repetition) Str*3 *operator creates a new string by


repeating multiple copies of the
same string.
in/not in (Membership) ‘M’ in ‘Mumbai’ ‘M’ Returns true if character exist in
not in ‘mumbai’ given the string and return false if
the given character does not exist
in the string
[:] (range(start, stop, step) Str[1:8:2] Extracts the characters from the
given range or to extract a subset
of values.
[ ] Slice[n:m] Str[ 2:7] Extracts the characters from the
given index

Traversing a String for i in range(len(str)): iterate through the elements of a


string,one character at a time.

BY: AMJAD KHAN Page 5 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

String methods & built in functions:


functions:-
Function / Description Example
Method
len(S) It returns the length of a string. >>> str1="Save
"Save Earth"
>>> print (len(str1))
(str1))
10
S.lower() Return a copy of the string S >>> str1 = 'hello WORLD!'
converted to lowercase >>> str1.lower()
()
hello world!
S.upper() Return a copy of S converted to >>> str1 = 'hello WORLD!'
uppercase >>> str1.upper()
()
HELLO WORLD!
S.capitalize() Make the first character >>> str1="welcome"
"welcome"
have upper case and the rest >>> print(str1.capitalize
capitalize())
lower case Welcome
S.title() It is used to convert the string >>> str = 'deLHi'
into the title
title-case i.e., The >>> str.title()
string deLHi will be converted to Delhi
Delhi >>> str1 = 'hello WORLD!'
>>> str1.title()
()
Hello World!
S. count It counts the number of >>> str1 = 'Hello World! Hello
(string, begin, occurrences of a substring in a Hello'
end ) >>> str1.count('Hello'
'Hello',12,25)
String between begin and end 2
index. >> str1.count('Hello'
'Hello')
If we do not give begin index 3
and end index then searching
starts from index 0 and ends at
length of the string.
S. index (str, It throws an exception if string is >>> str1 = 'Hello World! Hello
start, end) Hello'
not found. It works same as >>> str1.index('Hello'
'Hello',10,20)
13

BY: AMJAD KHAN Page 6 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

find() method. >>> str1.index('Hello'


'Hello')
0
>> str1.index('Hee'
'Hee')
ValueError: substring not found
S.islower() Return True if all cased “>>> str1 = 'save earth!'
characters in S are lowercase >>> str1.islower
islower()
True
>>> str1 = 'save 1234'
>>> str1.islower
islower()
True
>>> str1 = 'save ??'
>>> str1.islower
islower()
True
>>> str1 = '1234'
>>> str1.islower
islower()
False
S.isupper() Return True if all cased >>> str1 = 'SAVE EARTH!'
characters in s are uppercase >>> str1.isupper
isupper()
True
>>> str1 = 'SAVE 1234'
>>> str1.isupper
isupper()
True
>>> str1 = 'SAVE ??'
>>> str1.isupper
isupper()
True
>>> str1 = '1234'
>>> str1.isupper
isupper()
False
>>> str1 = 'Save Earth!'
>>> str1.isupper
isupper()
False
S.istitle() Return True if all words of string >>> “The
The Times Of
starts with Capital letters rest India”.istitle()
()
with lower True

BY: AMJAD KHAN Page 7 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

S.split() Return list of strings all >>> “The


The Times Of
separated by space or any India”.split()
specified character
[ “times”, “of”, “india”]
>>>“abc@pink@city
bc@pink@city”.split(“@”)
[“abc”, “pink”,”city”]
>>> “The
The Times Of India”.split()
India
[“tim”, “of india”]
S.join(S1) • Fills S between each str1 = ('SaveEarth!'
'SaveEarth!')
character of S1 >>> str2 = '-'
• It can also be used with list or >>> str2.join(str1)
(str1)
'S-a-v-e-E-a-r-tt-h-!'
tuple of strings
S.endswith(S1) Return True if S ends with S1 >>>“women”.endswith(
.endswith(“men”)
True
S.strip() • Removes leading and trailing >>> str1 = ' Save Earth!'
S.lstrip() spaces, if any >>> str1.strip()
()
S.rstrip() • Removes leading spaces only, 'Save Earth'
>>> str1 = ' Save Earth!'
if any
>>> str1.lstrip()
()
• Removes trailing spaces only,
'Save Earth'
if any
>>> str='Teach
'Teach India Movement'

>>> str.lstrip("T"
("T")

'each India Movement'

>>> str1 = 'Save Earth! '


>>> str1.rstrip()
()
'Save Earth'

>>> str='Teach
'Teach India Movement'

>>> str.rstrip("nt"
("nt")

'each India Moveme'


S.isdigit() Returns True if S contains only >>>str1='Save
'Save Earth'
digits >>> str1.isdigit
isdigit()
False

BY: AMJAD KHAN Page 8 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

>>> str2='1234'
>>> str2.isdigit
isdigit()
True
S.isalpha() Return True if S contains only >>> 'Click123'.isalpha
isalpha()
alphabets False
>>> 'python'.isalpha
isalpha()
True
S.isalnum() Return True if S contains >>> str1 = 'HelloWorld'
alphabet, digit or mixture of >>> str1.isalnum
isalnum()
both. True
>>> str1 = 'HelloWorld2'
HelloWorld2'
>>> str1.isalnum
isalnum()
True
>>> str1 = 'HelloWorld!!'
>>> str1.isalnum
isalnum()
False
S.find(S1) Return index number of first >>> str1 = 'Hello World! Hello
occurance of S1 otherwise -1 Hello'
>>> str1.find('Hello'
'Hello',10,20)
13
>>> str1.find('Hello'
'Hello',15,25)
19
>>> str1.find('Hello'
'Hello')
0
>> str1.find('Hee'
'Hee')
-1
S.partition ( ) Partitions the given string at the >>> str='The
'The Green Revolution'
first occurrence of the substring >>> str.partition
partition('Rev')
(separator) and returns the ('The Green ', 'Rev',
string partitioned into three 'olution')
>>> str.partition
partition('pe')
parts.
('The Green Revolution', '',
1. Substring before the
'')
separator
>>> str.partition
partition('e')
2. Separator
('Th', 'e', ' Green
3. Substring after the separator Revolution')
If the separator is not found in

BY: AMJAD KHAN Page 9 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

the string,
ring, it returns the whole
string itself and two empty
strings

ASSIGNMENT

1 Which of the following operations on a string will generate an error?


(a) "PYTHON"*3 (b) "PYTHON" + "20"
(c)"PYTHON" + 10 (d) "PYTHON" + "LANGUAGE"
2 If the following code is executed, what will be the output of the following code?
name="Computer_Science_with_Python" print (name [[-25:10])
(a) puter_S (b) hon
(c) puter_Science (d) with python
3 Which of the following functions will return the total number of characters in a
string?
count () (b) index()
(c) len() (d) all of these
4 Which of the following functions will return the last three characters of a string s ?
s[3: ] b) s[ : 3]
c) s[-3: ] d) s[ : -3]
5 Which of the following functions will raise an error if the given substring
subst is not found
in the string?
a) find() b) index()
c) replace() d) all of these
6 Which of the following functions removes all leading
ding and trailing spaces from a string
?
a) lstrip() b) rstrip()
c) strip() d) all of these
7 Find the operator which cannot be used with a string in Python from the following:
(a) + (b) not in
(c) * (d) //
8 What will be the output of above Python code?
str1= “6/4”
print(“str1”)

BY: AMJAD KHAN Page 10 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

a) 1 b) 6/4
c) str1 d (1.5)
9 Which of the following will result in an error? str1="python"
a) print(str1[2]) b) str1[1]="x"
c) print(str1[0:9]) d) Both (b) and (c)
10 Which of the following is False?
a) String is immutable.
b) capitalize ()) function in string is used to return a string by converting the whole
given string into uppercase.
c) lower() function in string is used to return a string by converting the whole given
string into lowercase.
d) None of these.
11 What will be the output of below Python code? str1="Information"
print(str1[2:8])
a) format b) formation
c) orma d) ormat
12 What will be the output of below Python code? str1="Aplication"
str2=str1.replace('a','A') print(str2)
a) application b) Application
c) ApplicAtion d) applicAtion
13 What will be the output of below Python code? str1=”power”
str1.upper( ) print(str1)
a) POWER b) Power
c) power d) power
14 Which of the following will give "Simon" as output? If str1="John,Simon,Aryan"
a) print(str1[-7:-12]) b) print(str1[-11:-7])
c) print(str1[-11:-6]) d) print(str1[-7:-11])
In the following questions(15 to 20) , a statement of assertion (A) is followed by a
statement of reason(R) . Make the correct choice as :
(a) Both A and R are true and R is the correct explanation for A
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False (or partially True)
(d) A is false( or partially True) but R is True
15 Assertion(A): The position and index of string characters are different.
Reason(R) R) : The positions for string’s characters vary from 1..n , where n is

BY: AMJAD KHAN Page 11 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

size of the string . The indices for string’s characters vary from 0 to n-1.
n
16 Assertion(A): Operators + and * can work with numbers as well as strings.
Reason(R) : Unlike numbers , for strings , + means concatenation and * means
replication.
17 Assertion(A):String slices and substrings ,both are extracted subparts of a string.
Reason(R) : String slices and substrings mean the same.
18 Assertion(A):Like numbers, ==,>,< can also compare two strings.
Reason(R) :comparison of python strings takes place in dictionary order by applying
character-by-character
character comparison rules for ASCII/Unicode.
19 Assertion(A):String slices and substrings, despite being subparts of a string , are not
the same.
Reason(R) :While the substring contains a continuous subparts of the string , the
slices may or may not contain continuous subparts of a string.
20 Assertion(A): The individual characters of a string are randomly stored in memory.
Reason(R) :Python strings are stored in memory by storing individual characters in
contiguous memory locations.

ANSWER (1 MARK
MARK- MULTIPLE CHOICE QUESTIONS (MCQ)

1 c 11 a
2 a 12 c
3 c 13 a
4 c 14 c
5 b 15 a
6 c 16 a
7 d 17 c
8 c 18 b
9 b 19 a
10 b 20 d

BY: AMJAD KHAN Page 12 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

Python: List

List is an ordered sequence, which is used to store multiple data at the same time. List
contains a sequence of heterogeneous elements. Each element of a list is assigned a number
to its position or index. The first index is 0 (zero), the second index is 1 , the third index is 2 and
so on .

Creating a List In Python,


a = [ 34 , 76 , 11,98 ]
b=['s',3,6,'t']
d=[]
Creating List From an Existing Sequence: list ( ) method is used to create list from an existing
sequence .
Syntax:
new_list_name = list ( sequence / string )
You can also create an empty list . eg .
a = list ( ) .

Similarity between List and String


• len ( ) function is used to return the number of items in both list and string .
• Membership operators as in and not in are same in list as well as string .
• Concatenation and replication operations are also same done in list and string.

Difference between String and List

BY: AMJAD KHAN Page 13 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

Strings are immutable which means the values provided to them will not change in the
program. Lists are mutable which means the values of list can be changed at any time.

Accessing Lists
To access the list's elements, index number is used.
S = [12,4,66,7,8,97,”computer”,5.5
7,8,97,”computer”,5.5]
>>> S[5]
97
>>>S[-2]
computer

Traversing a List
Traversing a list is a technique to access an individual element of that list.
1. Using for loop for loop is used when you want to traverse each element of a list.
>>> a = [‘p’,’r’,’o’,’g’,’r’,’a’,’m’]
>>> for x in a:
print(x, end = ‘ ‘)
output : p r o g r a m

2. Using for loop with range( )


>>> a = [‘p’,’r’,’o’,’g’,’r’,’a’,’m’]
>>> for x in range(len(a)):
print(a[x], end = ‘ ‘)
output : p r o g r a m

BY: AMJAD KHAN Page 14 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

List Operations
1. Concatenate Lists
List concatenation is the technique of combining two lists . The use of + operator can easily
add the whole of one list to other list .
Syntax:
list = list1 + list2
e.g.
#list1 is list of first five odd inte
integers
>>> list1 = [1,3,5,7,9]
#list2 is list of first five even integers
>>> list2 = [2,4,6,8,10]
#elements of list1 followed by list2
>>> list1 + list2
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Note: If we try to concatenate a list with elements of some other data type, TypeError occurs.
For Example:
>>> list1 = [1,2,3]
>>> str1 = "abc"
>>> list1 + str1
TypeError: can only concatenate list (not "str") to list
2. Replicating List
Elements of the list can be replicated using * operator .
Syntax:
list = list * digit
e.g.

BY: AMJAD KHAN Page 15 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

>>> list1 = [3,2,6]


#elements of list1 repeated 3 times
>>> list1 * 3
[3,2,6,3,2,6,3,2,6]

3) Membership Operator in List

in operator:

The operator displays True if the list contains the given element or the sequence of
elements.
>>> list1 = ['C.Sc.','IP'
'IP','Web App']
>>> 'IP' in list1
True

>>> 'Maths' in list1


False

not in operator:

The not in operator returns True if the element is not present in the list, else it
returns False.
>>> list1 = ['C.Sc.','IP'
'IP','Web App']
>>> 'IP' not in list1
False

>>> 'Maths' not in list1


True

4) Slicing

Like strings, the slicing operation can also be applied to lists.

BY: AMJAD KHAN Page 16 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

>>> list1 =['Maths','Phy',


,'Che','Eng','IP','C.Sc.','PE'
'PE']
>>> list1[2:6]
['Che','Eng','IP','C.Sc.']

#list1 is truncated to the end of the list


>>> list1[2:20] #second index is out of range
['Che','Eng','IP','C.Sc.','PE']

>>> list1[7:2] #first index > second index


[] #results in an empty list

#return sublist from index 0 to 4


>>> list1[:5] #first index missing
['Maths','Phy','Che','Eng','IP']

#slicing with a given step size


>>> list1[0:6:2]
['Maths', 'Che', 'IP']

#negative indexes
#elements at index -6,-5,-
-4,-3 are sliced
>>> list1[-6:-2]

5. List Manipulation Updating Elements in a List

Syntax: list_name [ start: stop : step ]

BY: AMJAD KHAN Page 17 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

>>> List1 = [ 4 , 3 , 7 , 6 , 4 , 9 ,5,0,3 , 2]


>>> S = List1[ 1 : 9 : 3 ]
>>> S
[ 3, 4, 0 ]
List can be modified after it created using slicing e.g.
>>> l1=
1= [ 2 , 4, " Try " , 54, " Again " ]
>>> l1[ 0 : 2 ] = [ 34, " Hello " ]
>>> l1
[34, ' Hello ', ' Try ', 54, ' Again ']
>>> l1[ 4 ] = [ "World " ]
>>> l1
[34, ' Hello ', ' Try ', 54, ['World ']]

Deleting Elements from a List


del keyword is used to delete the elements from the list .
Syntax:-
del list_name [ index ] # to delete individual element
del List_name [ start : stop ] # to delete elements in list slice c.g.
>>> list1 = [ 2.5 , 4 , 7 , 7 , 7 , 8 , 90 ]
>>> del list1[3]
>>> list1
[2.5, 4, 7, 7, 8, 90]
>>> del list1[2:4]
>>> list1 [2.5, 4, 8, 90]

BY: AMJAD KHAN Page 18 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

List Functions / Methods

Methods Description Example


len(object) It returns the length of the list >>> list1 = [10,20,30,40,50]
passed as the argument >>> len(list1)
5

L.append(Value) Appends a single element passed >>> list1 = [10,20,30,40]

as an argument at the end of the >>> list1.append(50)


(50)
>>> list1
list.
[10, 20, 30, 40, 50]
>>> list1 = [10,20,30,40]
>>> list1.append([50,60])
([50,60])
>>> list1
[10, 20, 30, 40, [50, 60]]
L.insert(index,value) It adds object at the given index >>> list1 = [10,20,30,40,50]

number >>> list1.insert(2,25)


(2,25)
>>> list1
[10, 20, 25, 30, 40, 50]
L1.erxtend(L2) It expand the size of L1 adding all list1 = [10,20,30]

values of L2 >>> list2 = [40,50]


>>> list1.extend(list2)
(list2)
>>> list1
[10, 20, 30, 40, 50]
L.pop() Returns the element whose index >>> list1=[10,20,30,40,50,60]
[10,20,30,40,50,60]
is passed as parameter to this >>> list1.pop(3)
(3)
function and also removes it from 40

the list. If no parameter is given, >>> list1

then it returns and removes the [10, 20, 30, 50, 60]

last element of the list. >>> list1=[10,20,30,40,50,60]


[10,20,30,40,50,60]

Return IndexError if list is empty. >>> list1.pop()


()
60

BY: AMJAD KHAN Page 19 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

>>> list1
[10, 20, 30, 40, 50]
L.remove(Value) Removes the given element from >>> list1 = [10,20,30,40,50,30]

the list. If the element is present >>> list1.remove(30)


(30)
>>> list1
multiple times, only the first
[10, 20, 40,
, 50, 30]
occurrence is removed.
>>> list1.remove(90)
(90)
If the element is not present, then
ValueError:list.remove(x):x not
ValueError is generated in list
L.clear() It removes all the values of list >>> list1 = [10,20,30,40,50,30]
>>> list1.clear()
)
>>> list1
[]
L.reverse() It reverses the order of all values of >>> list1 = [34,66,12,89,28,99]
list. >>> list1.reverse
reverse()
>>> list1
[ 99, 28, 89, 12, 66, 34]
L.sort() It can arrange the list in both list1 =[34,66,12,89,28,99]
[34,66,12,89,28,99]
>>> list1.sort(
()
ascending and descending order.
>>> list1
[12,
, 28, 34, 66, 89, 99]
99
>>> list1.sort(
(reverse = True)
>>> list1
[99,89,66,34,28,12]
L.sorted() It takes a list as parameter and list1 =[34,66,12,89,28,99]
[34,66,12,89,28,99]
>>> L=list1.sort
sorted()
creates a new list consisting of the
>>> L
same elements arranged in sorted
[12,
, 28, 34, 66, 89, 99]
99
order

L.index(object) It return index number of given >>> list1 = [10,20,30,20,40,10]

BY: AMJAD KHAN Page 20 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

object otherwise raise ValueError >>> list1.index(20)


(20)
1
>>> list1.index(90)
(90)
ValueError: 90 is not in list
L.count(object) It returns the total occurrence of >>>list1 = [10,20,30,10,40,10]
given object >>>list1.count(10)
(10)
3
>>> list1.count
count(90)
0
min(Lst) Returns minimum or smallest >>> list1 =[34,12,63,39,92,44]
[34,12,63,39,92,44]
element of the list >>> min(list1)
12
max(Lst) Returns maximum or largest >>> list1 =[34,12,63,39,92,44]
[34,12,63,39,92,44]
element of the list
>>> max(list1)
92

sum(Lst) Returns sum of the elements of the >>> list1 =[34,12,63,39,92,44]


[34,12,63,39,92,44]
list >>> sum(list1)
284

Nested List:
When a list appears as an element of another list, it is called a nested list.
>>> list1 = [1,2,'a','c',[6,7,8],4,9]
,[6,7,8],4,9]
#fifth element of list is also a list
>>> list1[4]
[6, 7, 8]

BY: AMJAD KHAN Page 21 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

To access the element of the nested list of list1, we have to specify two indices list1[i][j]. The first
index i will take us to the desired nested list and second index j will take us to the desired element
in that nested list.
>>> list1[4][1]
7

TUPLES
A tuple is an ordered sequence of elements of different data types. Tuple holds a sequence of
heterogeneous elements, it store a fixed set of elements and do not allow changes

Tuple vs List
Elements of a tuple are immutable whereas elements of a list are mutable.
Tuples are declared in parentheses ( ) while lists are declared in square brackets [ ]. Iterating
over the elements of a tuple is faster compared to iterating over a list.

Creating a Tuple
To create a tuple in Python, the elements are kept in parentheses ( ), separated by commas.
a = ( 34 , 76 , 12 , 90 )
b=('s',3,6,'a')
Create a empty tuple
T= ( )
Crerate a tuple with single element
T = 5,
Or
T = (5,)

BY: AMJAD KHAN Page 22 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

Accessing tuple elements, Traversing a tuple, Concatenation of tuples,


Replication of tuples and slicing of tuples works same as that of List

BUILT IN TUPLE METHODS

Method Description
len() Returns the length or the number of >>> tup1 = (10,20,30,40,50)
>>> len(tup1)
elements of the tuple passed as
5
Argument
tuple() Creates an empty tuple if no >>> tup1 = list()
list

argument is passed. Creates a tuple if >>> tup1


()
a sequence is passed as argument
count() Returns the number of times the >>>tup1 = (10,20,30,10,40,10)
>>>tup1.count(10)
(10)
given element appears in the tuple
3
>>> tup1.count(90)
(90)
0
index() Returns the index of the first >>> tup1 = (10,20,30,20,40,10)
>>> tup1.index(20)
(20)
occurance of a given element in the
1
tuple >>> tup1.index(90)
(90)
ValueError: 90 is not in tuple

sorted() Takes elements in the tuple and tup1 =(34,66,12,89,28,99)


>>> T=tup1.sort
sorted()
returns a new sorted list. It should be
>>> T
noted that, sorted() does not make (12,
, 28, 34, 66, 89, 99)
99

any change to the original tuple

BY: AMJAD KHAN Page 23 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

min() Returns minimum or smallest >>> tup1 =(34,12,63,39,92,44)


>>> min(tup1)
element of the tuple
12

max() Returns maximum or largest element >>> tup1 =(34,12,63,39,92,44)


>>> max(tup1)
of the tuple
92

sum() Returns sum of the elements of the >>> tup1 =(34,12,63,39,92,44)


>>> sum(tup1)
tuple
284

DICTIONARY
Dictionary is an unordered collection of data values that store the key : value pair instead of
single value as an element .
Keys of a dictionary must be unique and of immutable data types such as strings, tuples etc.
Dictionaries are also called mapping
mappings or hashes or associative arrays

Creating a Dictionary
To create a dictionary in Python, key value pair is used .
Dictionary is list in curly brackets , inside these curly brackets , keys and values are declared .
Syntax:
dictionary_name = { key1 : valuel , key2 : value2 ... }
Each key is separated from its value by a colon ( :) while each element is separated by
commas.
>>> Employees = { " Abhi " : " Manger " , " Manish " : " Project Manager " , " Aasha " : " Analyst
" , " Deepak " : " Programmer " , " Ishika " : " Tester "}

BY: AMJAD KHAN Page 24 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

Accessing elements from a Dictionary


Syntax: dictionary_name[keys]
>>> Employees[' Aasha ']
' Analyst '

Traversing a Dictionary
1. Iterate through all keys
for i in Employees:
print(i)
Output:
Abhi
Manish
Aasha
Deepak
Ishika

2. Iterate through all values


for i in Employees:
print(Employees[i])
Output:
Manger
Project Manager
Analyst
Programmer
Tester

BY: AMJAD KHAN Page 25 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

3. Iterate through key and values


for i in Employees:
print(i, " : ", Employees[i])
Output:
Abhi : Manger
Manish : Project Manager
Aasha : Analyst
Deepak : Programmer
Ishika : Tester

4. Iterate through key and values simultaneously


for a,b in Employees.items():
print("Key = ",a," and respective value = ",b)
Output:
Key = Abhi and respective value = Manger
Key = Manish and respective value = Project Manager
Key = Aasha and respective value = Analyst
Key = Deepak and respective value = Programmer
Key = Ishika and respective value = Tester

Adding elements to a Dictionary


Syntax:
dictionary_name[new_key] = value
To add an item to the dictionary (empty string), we can use square brackets for accessing and
initializing dictionary values.

BY: AMJAD KHAN Page 26 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

dict2 = {'Raman': 76, 'Suman'


'Suman': 56, 'Sahil': 34, 'Amit':
: 85}
dict2['John'] = 77 # create / Add key – value pair
print(dict2)

OUTPUT:
{'Raman': 76, 'Suman': 56, 'Sahil': 34, 'Amit': 85, 'John':77}

Updating elements in a Dictionary


Syntax:
dictionary_name[existing_key] = value
The existing dictionary can be modified by just overwriting the key-value
value pair. For Example:
dict2 = {'Raman': 76, 'Suman'
'Suman': 56, 'Sahil': 34, 'Amit':
: 85}
dict2['Sahil'] = 50 # Marks of Sahil changed to 50
print(dict2)
OUTPUT:

{'Raman': 76, 'Suman': 56, 'Sahil': 50, 'Amit': 85, 'John':77}

Membership operators in Dictionary


The membership operator in checks if the key is present in the dictionary and returns True, else it
returns False.
>>>dict1 = {'Raman': 76, 'Suman'
'Suman': 56, 'Sahil': 34, 'Amit':
'Amit' 85}
>>>'Sahil' in dict1
True
The not in operator
ator returns True if the key is not present in the dictionary, else it returns False.
>>>dict1 = {'Raman': 76, 'Suman'
'Suman': 56, 'Sahil': 34, 'Amit':
'Amit' 85}
>>>'Sahil' not in dict1

BY: AMJAD KHAN Page 27 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

False

Removing an Item from Dictionary


You can remove an item from the existing dictionary by using del command or using pop( )
method:

1) Using del Command:

It Deletes the item with the given key.


>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'
>>> del dict1['Suman']
>>> dict1
{'Raman':76,’Sahil’:55, ‘Amit’: 85}
To delete the dictionary from the memory we write:
del Dict_name
>>> del dict1
>>> dict1
NameError: name 'dict1' is not define

2) Using pop () Method:

pop () method will not only delete the item from the dictionary, but also return the deleted
value.
Syntax:
Dictionary.pop(key)
>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'
>>> dict1.pop('Sahil') # It also returns the deleted value

BY: AMJAD KHAN Page 28 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

55
>>> dict1
{'Raman':76, 'Suman': 56, ‘Amit’: 85}

Dictionary Methods and Functions

1) len():
It Returns the length or number of key: value pairs of the dictionary passed as the argument
>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'
>>> len(dict1)
4

2) dict()
It Creates a dictionary from a sequence of key-value pairs
>>>pair1 =[('Raman',76),('Suman'
'Suman',56),('Sahil',55),('Amit'
'Amit',85)]
>>> pair1
[('Raman', 76), (‘Suman’, 56), (‘Sahil’, 55), (‘Amit’, 85)]
>>> dict1 = dict(pair1)
>>> dict1
{'Raman': 76, ‘Suman’: 56, ‘Sahil’: 55, ‘Amit’: 85}

3) keys()
It Returns a list of keys in the dictionary
>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'
>>> dict1.keys()
dict_keys(['Raman', ‘Suman’, ‘Sahil’, ‘Amit’])

4) values()

BY: AMJAD KHAN Page 29 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

It Returns a list of values in the dictionary


dict1 = {'Raman': 76, 'Suman'
'Suman': 56, 'Sahil': 55, 'Amit':85}
:85}
>>> dict1.values()
dict_values([76, 56, 55, 85])

5) items()
It Returns a list of tuples (key — value) pair
dict1 = {'Raman': 76, 'Suman'
'Suman': 56, 'Sahil': 55, 'Amit':85}
:85}
>>> dict1.item()
dict_items([( 'Raman', 76), (‘Suman’, 56), (‘Sahil’, 55), (‘Amit’, 85)])

6) get()
It Returns the value corresponding to the key passed as the argument
If the key is not present in the dictionary it will return None
>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'
>>> dict1.get('Amit')
85
>>> dict1.get('Sohan')
>>>

7) update()
It appends the key-value
value pair of the dictionary passed as the argument to the key-value
key pair of
the given dictionary
>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'
>>> dict2 = {'Sohan':79,'Geeta'
'Geeta':56}
>>> dict1.update(dict2)
>>> dict1

BY: AMJAD KHAN Page 30 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

{'Raman': 76, ‘Suman’: 56, ‘Sahil’: 55, ‘Amit’: 85, 'Sohan': 79,
'Geeta': 56}
>>> dict2
{'Sohan': 79, 'Geeta': 56}

8) clear()
It Deletes or clear all the items of the dictionary
>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 34, 'Amit':85}
'Amit'
>>> dict1.clear()
>>> dict1
{ }

9) del()
It Deletes the item with the given key.
>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'
>>> del (dict1['Suman'])
>>> dict1
{'Raman':76,’Sahil’:55, ‘Amit’: 85}
To delete the dictionary from the memory we write:
del Dict_name
>>> del (dict1)
>>> dict1
NameError: name 'dict1' is not defined

10) pop()

BY: AMJAD KHAN Page 31 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

pop () method will not only delete the item from the dictionary, but also return the deleted
value.
Syntax:
Dictionary.pop(key)
>>> dict1 = {'Raman':
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'
>>> dict1.pop('Sahil') # It also returns the deleted value
55
>>> dict1
{'Raman':76, 'Suman': 56, ‘Amit’: 85}

11) popitem()
The popitem( ) method removes and returns the last inserted key:value pair from
the dictionary.

Syntax:
Dictionary.popitem()
: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
>>> dict1 = {'Raman': 'Amit'
>>> dict1.popitem() # It also returns the deleted value with key

('Amit', 85)

12) max() and min()


Following are the steps to find the maximum or minimum value from dictionary
a) create a list of the dictionary values.
b) then pass the list of dictionary value to the max()/min() function
c) max()/min() function return the maximum or minimum value from dictionary

BY: AMJAD KHAN Page 32 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

For Example:

>>> dict1 = {'Raman':


: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'

dict_value = dict1.values()
() # Create a list of dict’s values

>>> temp = min(dict_value)


(dict_value) # call min( ) function with list of dict’s
values

>>> temp # Display min value

55

>>> temp = max(dict_value)


(dict_value) # call min( ) function with list of dict’s
values

>>> temp # Display min value

85

13) sorted()

It Sorts the elements of the given dictionary in


in-place.
place. Sorting can be done by:
Sorting by keys
Sorting by values
Sorting By Keys:
You can use the keys in order to get a sorted dictionary in the ascending order.

>>> dict1 = {'Raman':


: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'

#this will print a sorted list of the keys

print(sorted(dict1.keys()))
(dict1.keys()))

#this will print the sorted list with items.

BY: AMJAD KHAN Page 33 of 34 Website: https://www.learnpython4cbse.com/


About Us Website: https://www.learnpython4cbse.com/ Feedback 8076665624

Learnpython4cbse Youtube Channel:


Inspiring Success SuperNova-learnpython
learnpython
PYTHON ONLINE CLASSES COMPUTER SC. INFORMATICS PRAC. SAMPLE PAPERS PYTHON MCQs

print(sorted(dict1.items()))
(dict1.items()))

OUTPUT:

['Amit', 'Raman', 'Sahil', 'Suman']

[('Amit', 85), ('Raman', 76), ('Sahil', 55), ('Suman', 56)]

Sorting By Values:
You can use the values as well.

>>> dict1 = {'Raman':


: 76, 'Suman': 56, 'Sahil': 55, 'Amit':85}
'Amit'

#this will print a sorted list of the values

print(sorted(dict1.values()))
(dict1.values()))

OUTPUT:

[55, 56, 76, 85]

BY: AMJAD KHAN Page 34 of 34 Website: https://www.learnpython4cbse.com/

You might also like