WORK SHEET FOR PRACTISE
CLASS -11 PYTHON
Execute in python script mode or INTERCTVE MODE
NOTE(COPY AND PASTE)
1. What is the output of the following code?
dict={}
dict[2]=1
dict[1]=[2,3,4]
print(dict[1][1]
2. What will be the output of the below code:
word = ‘aeioubcd’
print (word [:3] + word [3:])
print("xyz DEF".upper())
3. What will be the output?
i) numbers = [11, 12, 13, 14]
numbers.extend([15,16,17,18])
print(len(numbers))
ii) list1 = [11, 13]
LIST2=list1
list2 = list1.copy()
list1[0] = 14
LIST2[0]=21
print(list2)
PRINT(LIST2)
4. What Will Be The Output Of The Following Code Snippet?
i) tuple = ('Python') * 4
print(type(tuple))
ii) tuple = (21,) * 3
1|Page
tuple[0] = 2
print(tuple)
II. ANSWER THE FOLLOWING
1. What will be the output?
i) dict = {}
dict[10.0] = 76
dict['10'] = 756
dict[10]= dict[10]+5
count = 0
for i in dict:
count += dict[i]
print(count)
ii) What will be the output of the following code snippet?
dict = {}
dict[(1,8,4)] = 178
dict[(6,3,1)] = 1
dict[(4,3)] = 2
sum = 0
for k in dict:
sum += dict[k]
print (sum)
2. i)Find the output of the following code:
str= “
[email protected]”
m=""
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i].lower()
elif str[i].islower():
m=m+str[i].upper()
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+"#"
print(m)
ii) What data type is the object below:
A=1,2,3,"UIO",90
print(type(A))
2|Page
5. a)What is the output of the following snippet of code?
numbers={1:5,2:22}
letters={3:'B'}
comb={}
comb['numbers'] = numbers
comb['letters'] = letters
print(comb)
III. ANSWER THE FOLLOWING
1.a)What are the similarities between string and tuple? Refer reader
b)Create a tuple containing element 1 to 50 using for loop.
a=()
for i in range(1,51):
a=a+(i,) # concatination
print(a)
c)Write a Python script that takes input from the user then count and display total number
of uppercase and lowercase characters present in the string. Refer book or classwork
2. i) Write difference between append() and extend() methods of list. Refer book or
classwork
ii) a) Write a Python statement to convert the first character of each word in the string to
uppercase. Refer book or classwork
b)Write a Python statement to check all the characters of the string is numeric or not. Refer
book or classwork
c) Write a Python statement to search the substring is present in the string or not Refer
book or classwork .
WORKSHEET – STRING HANDLING
3|Page
1 What will be the output of following code-
str="hello"
str[:2]
Ans:
'he'
2 What will be the output of following code-
str='Hello'
res=''
for i in range(len(str)):
res=res+str[i]
print(res)
Ans:
Hello
3 What will be the output of following code-
s='Hi!'
s1='Hello'
s2=s[:2]+s1[len(s1)-2:]
print(s2)
Ans:
Hilo
4 What will be the output of following code-
'ba'+'na'*2
Ans:
'banana'
5 What will be the output of following code-
s='Welcome to python4csip.com'
print(s.find('come'))
4|Page
s.count('o')
Ans:
3
4
6 What will be the output of following program:
s='Hello'
for i in s:
print(i,end='#')
Ans:
H#e#l#l#o#
7 What will be the output of following program:
for i in 'hardik':
print(i.upper())
Ans:
H
A
R
D
I
K
8 What will be the output of following program:
s='Hello'
for i in s:
print('Welcome')
Ans:
Welcome
Welcome
Welcome
Welcome
Welcome
9 What will be the output of following program:
str='virat'
for i in str:
5|Page
print(str.upper())
Ans:
VIRAT
VIRAT
VIRAT
VIRAT
VIRAT
10 What will be the output of following program:
a='hello'
b='virat'
for i in range(len(a)):
print(a[i],b[i])
Ans:
hv
ei
lr
la
ot
11 What will be the output of following program:
a='hello'
b='virat'
for i in range(len(a)):
print(a[i].upper(),b[i])
Ans:
Hv
Ei
Lr
La
Ot
12 What will be the output of following program:
print("xyyzxyzxzxyy".count('xyy', 2, 11))
6|Page
7|Page
Ans:
13 What will be the output of following program:
print(“hello”+1+2+3)
Ans:
Error
14 What will be the output of following program:
str1 = "PYTHON4CSIP.COM"
print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
Ans:
YTH PYTHO ON4CSIP.COM PYTHON4CSIP.CO PYTHON4CSIP.CO
15 What will be the output of following program:
str = "my name is kunfu pandya";
print (str.capitalize())
Ans:
My name is kunfu pandya
16 What will be the output of following program:
str1 = 'Hello'
str2 ='World!'
print('str1 + str2 = ', str1 + str2)
print('str1 * 3 =', str1 * 3)
Ans:
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello
17 What will be the output of following program:
count = 0
for letter in 'Hello World':
if(letter == 'l'):
count += 1
print(count,'letters found')
Ans:
3 letters found
8|Page
18 What will be the output of following program:
s="python4csip"
n = len(s)
m=''
for i in range(0, n):
if (s[i] >= 'a' and s[i] <= 'm'):
m = m + s[i].upper()
elif (s[i] >= 'n' and s[i] <= 'z'):
m = m + s[i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m + '#'
print(m)
Ans:
ppyHho#CcIi
19 What will be the output of following program:
a = "Mahender Singh Dhoni"
a = a.split()
b = a[0][0]+". "+a[1][0]+". "+a[2]
print (b)
Ans:
M. S. Dhoni
20 What will be the output of following program:
s='Mahender, Singh, Dhoni'
s1=s.split()
for i in s1:
print(i)
Ans:
Mahender,
Singh,
Dhoni
21 What will be the output of following program:
"Welcome to Python4csip.com".split()
Ans:
9|Page
['Welcome', 'to', 'Python4csip.com']
22 What will be the output of the program?
line = "PYTHON IS EASY TO LEARN"
L = line.split('a')
for i in L:
print(i, end=' ')
Ans:
PYTHON IS EASY TO LEARN
24 What will be the output of following program:
s='mahender, singh, dhoni'
s1=s.split()
for i in s1:
if(i>'n'):
print(i.upper())
else:
print(i)
Ans:
mahender,
SINGH,
dhoni
10 | P a g e
25 What will be the output of following program:
my_string = 'PYTHON4CSIP'
for i in range(len(my_string)):
print (my_string)
my_string = '#'
Ans:
PYTHON4CSIP
#
#
#
#
#
#
#
#
#
#
26 What will be the output of following program:
str="Python4csip.com"
for i in range(len(str)):
if(str[i].isalpha()):
print(str[i-1],end='')
if(str[i].isdigit()):
print(str[i],end='')
Ans:
mPytho44csi.co
11 | P a g e
28 What will be the output of following program:
str="PYTHON4CSIP"
for i in range(len(str)):
if(str[i].isdigit()):
print(str[i],end='')
if(str[i]=='N'or str[i]=='Y'):
print('#',end='')
Ans:
##4
29 Write a Program to Count the Number of Vowels in a String.
Ans:
string=input("Enter string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=
='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)
30 Write a Program to Take in a String and Replace Every Blank Space with
Hyphen.
Ans:
string=input("Enter string:")
string=string.replace(' ','-')
print("Modified string:")
print(string)
31 Write a Program to Calculate the Length of a String Without Using a Library
Function
Ans:
12 | P a g e
string=input("Enter string:")
count=0
for i in string:
count=count+1
print("Length of the string is:")
print(count)
32 Write a Program to Calculate the Number of Words and the Number of
Characters Present in a String
Ans:
string=input("Enter string:")
char=0
word=1
for i in string:
char=char+1
if(i==' '):
word=word+1
print("Number of words in the string:")
print(word)
print("Number of characters in the string:")
print(char)
13 | P a g e
34 Write a Program to Count Number of Lowercase Characters in a String
Ans:
string=input("Enter string:")
count=0
for i in string:
if(i.islower()):
count=count+1
print("The number of lowercase characters is:")
print(count)
35 Write a Program to Check if a String is a Palindrome or Not
Ans:
string=input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")
36 Write a Program to Calculate the Number of Upper Case Letters and Lower Case
Letters in a String
Ans:
string=input("Enter string:")
14 | P a g e
count1=0
count2=0
for i in string:
if(i.islower()):
count1=count1+1
elif(i.isupper()):
count2=count2+1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)
37 Write a Program to Calculate the Number of Digits and Letters in a String.
Ans:
string=input("Enter string:")
count1=0
count2=0
for i in string:
if(i.isdigit()):
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)
15 | P a g e
41 Write a Python Program to check First Occurrence of a Character in a String
Ans:
string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")
flag = 0
for i in range(len(string)):
if(string[i] == char):
flag = 1
break
if(flag == 0):
print("Sorry! We haven't found the Search Character in this string ")
else:
print("The first Occurrence of ", char, " is Found at Position " , i + 1)
42 Write a Python Program to find ASCII Value of a Character
Ans:
ch = 'T'
print("The ASCII Value of %c = %d" %(ch, ord(ch)))
WORKSHEET DICTIONARY
1. The ___KEYS___ of a dictionary must be immutable types.
2. Dictionaries are also called ______UNORDERED MUTABLE DATATAYPE_
3. Dictionaries are ______UNORDERED__MUTABLE_ datatypes of python.
4. Dictionaries are ______UNORDERED____ set of elements.
5. The popitem() function will always remove the _LAST_______ entered value of the
dictionary
16
II Answer the following Questions: 5 X 2=10
6. # Trace the Output
Execute and check in python IDLE
D1={ “rno” : ”53” , “name” : “sara” , “English” : 74 }
D2={ “Csc” : 100 , “English” : 99 , “Gender” : “Male”}
n=d1.update(d2)
print(“Merged Dictionary:”, n)
7. # Trace the ERROR in which statement(s):
D={ “Pno” : 52 , “Pname” : “Virat” , “Expert” : [“Badminton”, “Tennis”] , “Score” :
(77,44) }
Print(D) #statement 1
D[“Expert”] [0] = “Cricket” #statement 2
D[“Score”][0] = 50 # statement 3
D[“Pno”] = 50 # statement 4
8. # Trace the Output
R={“Amit” : 90 , “Reshma” : 96 , “Suhail” : 92 , “John” : 95}
Print(“John” in R , 90 in R , sep=”#”)
9. # Trace the Output
D={1 : “One”, 2 : “Two” , 3: “Three”}
L=[ ]
for k , v in D.items( ):
if v[0] == ”T”:
L.append(k)
print (L)
WORKSHEER FOR LIST MANIPULATION
Q1.What will it returns?
>>>eval(‘5+8’) ____________________________
Q2. >>>a=list(‘exam’)
>>>a
Choose the correct option: WHAT IS THE VALU OF a?
17
(i) [‘exam’]
(ii) (‘exam’)
(iii) [‘E’,’x’,’a’,’m’]
(iv) [‘e’,’x’,’a’,’m’]
Q3.(a)What Option will the following returs True or false??
a=[1,2,[3,4],[5,6],7]
(i) 3 in a
(ii) [1,2,8,9]<[9,1]
(iii) [1,2,3]==[1.0,2.0,3.0]
(b)Find the given values:
(i)[a[1]]+a[3]
(ii)a[ : :4]
Q4.Find the output:
>>>L=[1,[2,3],[4,5[6,7]]]
>>>L*2
>>>[L,L]
Q5.What will it returns
>>>a=[1,2,3,4,5,6,9,8,7]
(i)a[-16:6]
(ii)a[6:-2]
Q6.Write the syntax of the function that adds single element to the end of the list.
__ANS append(_)
Q7.>>>a=[‘D’,’O’,’Y’,’O’,’U’,’R’,’B’,’E’,’S’,’T’]
>>>del a[2:-5]
>>>a ________________________________
Q8.Write the syntax of insert method and insert ‘S’ in the given list at any position by
using insert method.
L=[‘t’,’p’,’r’,’m’] ______L.insert(“S”,2)__________________________
Q9.T=[1,4,9,16,25]
18
(ii)T.pop()
>>>T
(iii)T.clear()
>>>T ________________________________
Q10.Write the syntax to sort a list in decreasing order by using reverse method .
sort(reverse=True)
Q11.The ____pop(0)____ method deletes the 1st occurrence of a given element from the
list.
Q12.Write the output
>>> a=[1,2,2,3,4]
>>> a[a[a[a[2]+2]]] ________________________________
Q13.>>>a=[1,2,3,4,5,6,7,8,9]
>>>print(a[-1:-2:-3]) ________________________________
19