In this chapter, I am going to discuss a Review of Python Basics CS Class 12 unsolved questions. These questions are taken from the textbook computer science with python by Preeti Arora for Class 12. Let us begin!
Review of Python Basics CS Class 12
These questions are taken from the textbook 2022 edition of Textbook for CBSE Class XII Computer Science with Python by Preeti Arora. Let’s start now!
[1] What are the advantages of the Python programming language?
[2] In how many different ways can you work in python?
[3] What are the advantages/disadvantages of working in the interactive mode in Python?
[4] Write Python statement for the following in interactive mode:
(a) To display sum of 3, 8.0, 6*12 (b) To print sum of 16, 5.0, 44.0
[5] What are operators? Give examples of some unary and binary operators.
[6] What is an expression and a statement?
[7] What all components can a Python program contain?
[8] What are variables? How are they important for a program?
[9] Write the output of the following:
(i)
for i in '123':
print("guru99", i)
(ii)
for i in [ 100, 200, 300] :
print(i)
(iii)
for j in range (10, 6, -2):
print (j * 2)
(iv)
for x in range (1, 6):
for y in range (1, x+1) :
print (x,' ', y )
(v)
for x in range (10, 20):
if (x == 15):
break
print (x)
(vi)
for x in range (10, 20) :
if (x % 2 == 0) :
continue
print (x)
[10] Write the output of the following program on execution if x = 50:
if x > 10 :
if x > 25 :
print("ok")
if x > 60 :
print("good")
elif x > 40 :
print("average")
else:
print("no output")
[11] What are the various ways of creating a list?
[12]What are the similarities between strings and lists?
[13] Why are lists called a mutable data type?
[14] What is the difference between insert() and append() methods of a list.
[15] Write a program to calculate the mean of a given list of numbers.
Ans.:
l = []
n=int(input("Enter the no. of elements:"))
for i in range(n):
val=int(input("Enter the value to insert in the list:"))
l.append(val)
s = 0
for i in l:
s = s + i
mean = s/len(l)
print("The mean is:",mean)
[16] Write a program to calculate the minimum element of a given list of numbers.
Ans.:
l = []
n=int(input("Enter the no. of elements:"))
for i in range(n):
val=int(input("Enter the value to insert in the list:"))
l.append(val)
mini = l[0]
for i in l:
if i<mini:
mini = i
print("The mean is:",mini)
[17] Write a code to calculate and display total marks and percentage of a student from a given list storing the marks of a student.
Ans.:
l = []
n=int(input("Enter the no. of subjects:"))
mm=int(input("Enter Maximum makrs for each subject:"))
for i in range(n):
val=int(input("Enter marks of subject "+str(i+1)+" out of "+str(mm)+":"))
l.append(val)
s = 0
for i in l:
s+=i
per=(s/(mm*len(l)))*100
print("The mean is:%.2f"%per)
[18] Write a Program to multiply an element by 2 if it is an odd index for a given list containing both numbers and strings.
Ans.:
l = []
n=int(input("Enter the no. of elements:"))
for i in range(n):
val=int(input("Enter value "+str(i+1)+":"))
l.append(val)
s = 0
for i in range(len(l)):
if i==0:
pass
elif i%2!=0:
l[i]*=2
print("New List is:")
for i in range(len(l)):
print("l[",i,"]:",l[i])
[19] Write a Program to count the frequency of an element in a given list.
Ans.:
l = []
n=int(input("Enter the no. of elements:"))
for i in range(n):
val=int(input("Enter value "+str(i+1)+":"))
l.append(val)
f = {}
for i in l:
if (i in f):
f[i] += 1
else:
f[i] = 1
for i, j in f.items():
print(i, "->", j)
[20] Write a Program to shift elements of a list so that the first element moves to the second index and second index moves to the third index, and so on, and the last element shifts to the first position.
Suppose the list is [10, 20, 30, 40]
After shifting, it should look like: [40, 10, 20, 30]
Ans.:
l = []
n=int(input("Enter the no. of elements:"))
for i in range(n):
val=int(input("Enter value "+str(i+1)+":"))
l.append(val)
#Using Slicing
#print ("New list =", [l[ -1] ] + l[0 : -1] )
#Using Logic
temp = l[len(l)-1]
for i in range(len(l)-1,0,-1):
l[i] = l[i-1]
l[0] = temp
print('list after shifting', l)
[21] A list Num contains the following elements:
3, 25, 13, 6, 35, 8, 14, 45
Write a program to swap the content with the next value divisible by 5 so that the resultant list will look like: 25, 3, 13, 35, 6, 8, 45, 14
Ans.:
l = []
n=int(input("Enter the no. of elements:"))
for i in range(n):
val=int(input("Enter value "+str(i+1)+":"))
l.append(val)
for i in range(len(l)) :
if l[ i] % 5 == 0 :
l [ i ], l [i-1] = l [ i - 1 ] , l [i]
print("List after swap:",l)
[22] Write a program to accept values from a user in a tuple. Add a tuple to it and display its elements one by one. Also display its maximum and minimum value.
Ans.:
t = eval(input("Enter the values for tuple"))
for i in t:
print(i)
ma=0
mi=t[0]
for i in t:
if ma>i:
ma=i
if mi<i:
mi=i
print("Maximum:",ma)
print("Minimum:",mi)
[23] Write a program to input any values for two tuples. Print it, interchange it and then compare them.
Ans.:
t1 = eval(input("Enter tuple 1:-"))
t2 = eval(input("Enter tuple 2:-"))
t1,t2 = t2,t1
print("Tuple 1 :-",t1)
print("Tuple 2:- ",t2)
print("Result of Tuple 1 < Tuple 2:",t1 < t2)
print("Result of Tupl1 > Tuple 2:",t1 > t2)
print("Tuple 1 and Tuple 2 are equal?:",t1 == t2)
[24] Write a Python program to input ‘n’ classes and names of their class teachers to store them in a dictionary and display the same. Also accept a particular class from the user and display the name of the class teacher of that class.
Ans.:
cl={}
while True:
c=input("Enter the class and section:")
tr=input("Enter name of class teacher:")
cl[c]=tr
ch=input("Enter n to terminate:")
if ch.lower()=='n':
break
search_cl_tr=input("Enter class to know the class teacher:")
print(cl[search_cl_tr])
[25] Write a program to store student names and their percentage in a dictionary and delete a particular student name from the dictionary. Also display the dictionary after deletion.
Ans.:
st={}
while True:
name=input("Enter student names:")
per=float(input("Enter percentage of student:"))
st[name]=per
ch=input("Want more records(y/n):")
if ch.lower()=='y':
continue
if ch.lower()=='n':
break
se_name=input("Enter name to delete:")
print("Dictionary Before Deletion:")
print(st)
#Method 1 using del
del st[se_name]
#method 2 using pop() function
st.pop(se_name)
print("Dictionary After Deletion:")
print(st)
[26] Write a Python program to input names of ‘n’ customers and their details like items bought, cost and phone number, etc., store them in a dictionary and display all the details in a tabular form.
Ans.:
cust={}
n=int(input("Enter no. of customers:"))
for i in range(n):
name=input("Enter name of customer "+str(i+1)+":")
bt=int(input("Enter items bought:"))
co=float(input("Cost of item:"))
ph_no=input("Enter Phone number:")
cust[name]=[bt,co,ph_no]
print(cust)
[28] Write a Python program to capitalize first and last letters of each word of a given string.
Ans.:
name=input("Enter text:")
name=res=name.title()
res=''
for i in name.split():
res+=i[:-1]+i[-1].upper()+' '
print(res)
#Method 2
name=input("Enter text:")
l=list(name)
i=0
while i<len(l):
k=i
while i<len(l) and l[i]!=' ':
i+=1
if ord(l[k])>=97 and ord(l[k])<=122:
l[k]=chr(ord(l[k])-32)
else:
l[k]=l[k]
if ord(l[i-1])>=97 and ord(l[i-1])<=122:
l[i-1]=chr(ord(l[i-1])-32)
else:
l[i-1]=l[i-1]
i+=1
print("".join(l))
[29] Write a Python program to remove duplicate characters of a given string.
Ans.:
txt=input("Enter text:")
l=[]
for i in txt:
if i not in l:
l.append(i)
for i in range(len(l)):
print(l[i],end='')
Q 30 Write a Python program to compute the sum of digits of a given string.
Ans.:
txt=input("Enter text:")
sod=0
for i in txt:
if i.isdigit():
d=int(i)
sod=sod+d
print("Sum of digit:",sod)
[31] Write a Python program to find the second most repeated word in a given string.
Ans.:
txt=input("Enter text:")
l=txt.split()
m=0
sec=0
for i in l:
if l.count(i)>=m:
m=l.count(i)
elif l.count(i)>=sec:
sec=l.count(i)
sec_most=i
print("Second most repeated word is:",sec_most)
[32] Write a Python program to change a given string to a new string where the first and last chars have been exchanged.
Ans.:
txt=input("Enter text:")
#Logic 1
print(txt[-1:]+txt[1:-1]+txt[:1])
#Logic 2 using start end variables
st=txt[0]
en=txt[-1]
swa=en + txt[1:-1]+st
print(swa)
[33] Write a Python program to multiply all the items in a list.
Ans.:
n=int(input("Enter the number of items:"))
l=[]
for i in range(n):
val=int(input("Enter the value:"))
l.append(val)
m=1
for i in l:
m*=i
print("The multiplication of number is:",m)
[34] Write a Python program to get the smallest number from a list.
Ans.:
n=int(input("Enter the number of items:"))
l=[]
for i in range(n):
val=int(input("Enter the value:"))
l.append(val)
m=l[0]
for i in l:
if i<m:
m=i
print("The multiplication of number is:",m)
[35] Write a Python program to append a list to the second list.
Ans.:
n=int(input("Enter the number of items:"))
l1=[]
l2=[]
for i in range(n):
val=int(input("Enter the value:"))
l1.append(val)
for i in l1:
l2.append(i)
print("The multiplication of number is:",l2)
[36] Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30 (both included).
Ans.:
l=[]
for i in range(1,31):
if i>=1 and i<=5:
l.append(i)
if i>=26 and i<=30:
l.append(i)
print("The multiplication of number is:",l)
[37] Write a Python program to get unique values from a list.
Ans.:
n=int(input("Enter the number of items:"))
l=[]
uni_l=[]
for i in range(n):
val=int(input("Enter the value:"))
l.append(val)
for i in l:
if i not in uni_l:
uni_l.append(i)
print("Unique numbers:",uni_l)
[38] Write a Python program to convert a string to a list.
Ans.:
txt=input("Enter the Text:")
l=list(txt)
print(l)
[39] Write a Python script to concatenate the following dictionaries to create a new one:
d1 = {‘A’: 1, ‘B’: 2, ‘C’: 3}
d2 = {‘D’: 4}
Output should be:
{‘A’: 1, ‘B’: 2, ‘C’: 3, ‘D’: 4}
Ans.:
d1={'A':1,'B':2,'C':3}
d2={'D':4}
d1.update(d2)
print(d1)
[40] Write a Python script to check if a given key already exists in a dictionary.
Ans.:
d={}
n=int(input("Enter no. of elements:"))
for i in range(n):
name=input("Enter name "+str(i+1)+":")
bt=int(input("Enter items bought:"))
co=float(input("Cost of item:"))
ph_no=input("Enter Phone number:")
d[name]=[bt,co,ph_no]
key_search=input("Enter key to search:")
for i in d:
if key_search==i:
print("Items Bought:",d[i][0])
print("Items Cost:",d[i][1])
print("Phone Number:",d[i][2])
[41] Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the values are squares of keys.
Sample Dictionary
{1:1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
Ans.:
d={}
for i in range(1,16):
d[i]=i**2
print(d)
[42] Write a Python script to merge two Python dictionaries.
Ans.:
d1={'A':1,'B':2}
d2={'C':3,'D':4}
d3=d1 | d2
print(d3)
[43] Write a Python program to sort a dictionary by key.
Ans.:
d={}
n=int(input("Enter no. of elements:"))
for i in range(n):
name=input("Enter name "+str(i+1)+":")
bt=int(input("Enter items bought:"))
co=float(input("Cost of item:"))
ph_no=input("Enter Phone number:")
d[name]=[bt,co,ph_no]
for i in sorted(d.keys()):
print(i)
[44] Write a Python program to combine two dictionary adding values for common keys.
d1 = {‘a’: 100, ‘b’: 200, ‘c’: 300}
d2 = {‘a’: 300, ‘b’: 200, ‘d’: 400}
Sample output:
Counter ({‘a’: 400, ‘b’: 400, ‘c’: 300, ‘d’: 400})
Ans.:
d1={'A':1,'B':2,'C':3}
d2={'D':4,'A':5,'B':6}
print("Before Combine")
print("Dictionary 1:",d1)
print("Dictionary 2:",d2)
for key in d1:
if key in d2:
d1[key] = d1[key] + d2[key]
else:
pass
res= d1 | d2
print("After Combine")
print("Dictionary 1:",res)
[45] Write a Python program to find the highest 3 values in a dictionary.
Ans.:
d={'Sheerya':10,'Mahek':20,'Charmi':30,'Dimple':40}
v=list(d.values())
v.sort()
print("Highest 3 values are:",v[-1:-4:-1])
[46] Write a Python program to sort a list alphabetically in a dictionary.
Ans.:
d={'Sheerya':10,'Mahek':20,'Charmi':30,'Divya':40}
v=list(d.keys())
v.sort()
print("Alphabetically Sorted keys:",v)
[47] Write a Python program to count a number of items in a dictionary value that is a list.
Ans.:
d={'Name':['Sheerya'',Mahek','Charmi','Divya'],
'Marks':[45,67,78,89],
'Grades':['A','B'],'Remark':0}
c=1
for i in d:
if type(d[i])==list:
c+= len(d[i])
print(c)
[48] and [49] not in syllabus
Not in Syllabus for 2022-23
Not in Syllabus for 2022-23
[50] Evaluate the following expressions:
Ans.:
a) 6*3+4**2//5-8 b) 10>5 and 7>12 or not 18>3
Ans.:
a) 6*3+4**2//5-8
=6*3+4**2//5-8
=6*3+16//5-8
=18+16//5-8
=18+3-8
=21-8
=13
b) 10>5 and 7>12 or not 18>3
= 10>5 and 7>12 or not 18>3
= 10> 5 and 7>12 or False
= True and False or False
= False or False
= False
[51] What is type casting?
Ans.:
- Type casting refers to the conversion of a data type from one to another in a programming language.
- Python follows dynamic type casting.
- Python also allows to declare and assign multiple variables at the same time.
- Python determines the data type of a variable automatically.
- Observe this example:
a=20
print(a)
a='Computer Science Class 12'
print(a)
The above code has variable a and the values are integer and string.
Python offers few functions to perform explicit type casting. Such functions are int(), float(), str() etc.
[52] What are the comments in python? How is a comment different from indentation?
Ans.:
- Comments are additional text that represents some information about the program, statement, expressions or descriptions.
- Comments are ignored by python interpreted.
- Comments do not perform any task in the program execution.
- There are two types of comments:
- Single Line Comments: Begins with # sign
- Multi-Line Comments: Begins and Ends with ”’
Comments are different from indentation as indentation is required in a block or suite. Comments can be used anywhere in the program. If indentation is inappropriate or missed, python shows error. If a comment is not used nothing happens to the program.