You are on page 1of 24

SRI SANKARA VIDYALAYA MAT. HR. SEC.

SCHOOL,
EAST TAMBARAM
SUBJECT: COMPUTER SCIENCE STD: XII
Chapter 6
Control Structures

Hands on Experience:

1. Write a program to check the given character is a vowel or not.


Program:
ch=input("Enter a character")
if ch in ('a','A','e','E','i','I','o','O','u','U'):
print(ch,'is a vowel')
else:
print(ch,' is a consonant')
Output:
Enter a character e
e is a vowel
Enter a character R
R is a consonant

2. Using if else ..elif statement check smallest of three numbers.


Program:
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
if (a<b) and (a<c):
print(a," is smallest")
elif (b<a) and (b<c):
print(b, " is smallest")
else:
print(c, " is smallest")
Output:
Enter first number:12
Enter second number:4
Enter third number:45
4 is smallest
1
3. Write a program to check if a number is positive, negative or zero.
Program:
n=int(input("Enter a number"))
if n>0:
print("The number is positive")
elif n<0:
print("The number is negative")
else:
print("The number is zero")
Output:
Enter a number-5
The number is negative
Enter a number0
The number is zero
4. Write a program to display fibonacci series 0 1 1 2 3 5 .... n terms.
Program:
x1=-1
x2=1
n=int(input("Enter the number of terms for Fibonacci series"))
for i in range(1,n+1):
x3=x1+x2
print(x3, end='\t')
x1=x2
x2=x3

Output:
Enter the number of terms for Fibonacci series10
0 1 1 2 3 5 8 13 21 34

2
5. Write a program to display sum of natural numbers, upto n.
Program:
N=int(input(“Enter any number”))
s=0
for i in range(1,N+1):
s=s+i
print("The sum of ", N, " Natural numbers is ", s)

Output:
Enter any number10
The sum of 10 Natural numbers is 55

6. Write a program to check the given number is palindrome or not.


Program:
n=int(input("Enter a number"))
temp=n
rev=0
while(n!=0):
d=n%10
rev=rev*10 + d
n=n//10
if(temp==rev):
print(temp," is a palindrome")
else:
print(temp, " is not a palindrome")

Output:
Enter a number1441
1441 is a palindrome

Enter a number1234
1234 is not a palindrome

3
7. Write the program to print the following pattern.
* * * * *
* * * *
* * *
* *
*
Program:
for i in range(0,5):
for j in range(5, i, -1):
print('*',end=' ')
print()

Output:
* * * * *
* * * *
* * *
* *
*

8. Write a program to check if the year is leap year or not.


Program:
y=int(input("Enter a year"))
if y%4==0:
print(y, " is a leap year")
else:
print(y, " is not a leap year")
Output:
Enter a year2016
2016 is a leap year
Enter a year2010
2010 is not a leap year

4
Exercise Part III:
1. Write a program to display
A
A B
A B C
A B C D
A B C D E
Program:
for i in range(1,6):
for j in range(65,65+i):
print(chr(j), end=' ')
print()

Output:
A
A B
A B C
A B C D
A B C D E

3. Using if else elif statement write a suitable program to diaply largest of 3


numbers.
Program:
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
if (a>b) and (a>c):
print(a," is largest")
elif (b>a) and (b>c):
print(b, " is largest")
else:
print(c, " is largest")

Output:
Enter first number:5
Enter second number:23
Enter third number:34
34 is largest
5
Part IV:
3.Write a program to display all 3 digit odd numbers.
Program:
for x in range(101, 1000, 2):
print(x, end=',')
Output:
101,103,105,107,109,111,113,115,117,119,121,123,125,127,129,131,133,135,137,
139,141,143,145,147,149,151,153,155,157,159,161,163,165,167,169,171,173,175,
177,179,181,183,185,187,189,191,193,195,197,199,201,203,205,207,209,211,213,
215,217,219,221,223,225,227,229,231,233,235,237,239,241,243,245,247,249,251,
253,255,257,259,261,263,265,267,269,271,273,275,277,279,281,283,285,287,289,
291,293,295,297,299,301,303,305,307,309,311,313,315,317,319,321,323,325,327,
329,331,333,335,337,339,341,343,345,347,349,351,353,355,357,359,361,363,365,
367,369,371,373,375,377,379,381,383,385,387,389,391,393,395,397,399,401,403,
405,407,409,411,413,415,417,419,421,423,425,427,429,431,433,435,437,439,441,
443,445,447,449,451,453,455,457,459,461,463,465,467,469,471,473,475,477,479,
481,483,485,487,489,491,493,495,497,499,501,503,505,507,509,511,513,515,517,
519,521,523,525,527,529,531,533,535,537,539,541,543,545,547,549,551,553,555,
557,559,561,563,565,567,569,571,573,575,577,579,581,583,585,587,589,591,593,
595,597,599,601,603,605,607,609,611,613,615,617,619,621,623,625,627,629,631,
633,635,637,639,641,643,645,647,649,651,653,655,657,659,661,663,665,667,669,
671,673,675,677,679,681,683,685,687,689,691,693,695,697,699,701,703,705,707,
709,711,713,715,717,719,721,723,725,727,729,731,733,735,737,739,741,743,745,
747,749,751,753,755,757,759,761,763,765,767,769,771,773,775,777,779,781,783,
785,787,789,791,793,795,797,799,801,803,805,807,809,811,813,815,817,819,821,
823,825,827,829,831,833,835,837,839,841,843,845,847,849,851,853,855,857,859,
861,863,865,867,869,871,873,875,877,879,881,883,885,887,889,891,893,895,897,
899,901,903,905,907,909,911,913,915,917,919,921,923,925,927,929,931,933,935,
937,939,941,943,945,947,949,951,953,955,957,959,961,963,965,967,969,971,973,
975,977,979,981,983,985,987,989,991,993,995,997,999,
4. Write a program to display multiplication table for a given number.
Program:
num=int(input("Enter the number:"))
print("Multiplication Table of", num)
for i in range(1,11):
print(num," x ",i," = ", num*i)
6
Output:
Enter the number:16
Multiplication Table of 16
16 x 1 = 16
16 x 2 = 32
16 x 3 = 48
16 x 4 = 64
16 x 5 = 80
16 x 6 = 96
16 x 7 = 112
16 x 8 = 128
16 x 9 = 144
16 x 10 = 160

Chapter 7
PYTHON FUNCTION
Hands on Experience:
def printinfo(name, salary=3500):
print("Name:",name)
print("Salary:",salary)
return
1. Try the following code in the above program

Slno. code output


1. printinfo(“3500”) Name: 3500
Salary: 3500
2. printinfo(“3500”,”Sri”) Name: 3500
Salary: Sri
3. printinfo(name=”balu”) Name: balu
Salary: 3500
4. printinfo(“Jose”,1234) Name: Jose
Salary: 1234
5. printinfo(“ ”,salary=1234) Name:
Salary: 1234
7
2. Evaluate the following functions and write the output
Sl.no functions output
1. eval('25*2-5*4') 30
2. math.sqrt(abs(-81)) 9.0
3. math.ceil(3.5+4.6) 9
4. math.floor(3.5+4.6) 8

3. Evaluate the following functions and write the output.


Sl.no functions output
1. 1. abs(-25+12.5) 12.5
2. abs(-3.2) 3.2
2. 1. ord('2') 50
2. ord('$') 36
3. type('s') <class 'str'>
4. bin(16) '0b10000'
5. 1. chr(13) '\r'
2. print(chr(13))
6. 1. round(18.2,1) 18.2
2. round(18.2,0) 18.0
3. round(0.5100,3) 0.51
4. round(0.5120,3) 0.512
7. 1. format(66,'c') 'B'
2. format(10,'x') 'a'
3. format(10,'X') 'A'
4. format(0b110,'d') '6'
5. format(0xa,'d') '10'

8. 1. pow(2,-3) 0.125
2. pow(2,3.0) 8.0
3. pow(2,0) 1
4.pow((1+2),2) 9
5. pow(-3,2) 9
6. pow(2*2,2) 16

8
Part III
5.Write a python code to check whether a given year is leap year or not.

Program:
def leap(y):
if y%4==0:
print(y, " is a leap year")
else:
print(y, " is not a leap year")
n=int(input("Enter a year"))
leap(n)

Output:
Enter a year2016
2016 is a leap year

Part IV
4.Write a Python code to find the LCM of two numbers.

Program:
def gcd(a,b):
if b!=0:
return(gcd(b,a%b))
return a
x=int(input("Enter a number"))
y=int(input("Enter a number"))
lcm=int((x*y)/gcd(x,y))
print("The LCM of ", x ," and ", y , " is ",lcm)

Output:
Enter a number18
Enter a number24
The LCM of 18 and 24 is 72

(OR)
Program:
def lcm(x,y):
if x>y:
greater=x
9
else:
greater=y
while(True):
if(greater%x==0 and greater%y==0):
lcm=greater
break
greater+=1
return lcm
n1=int(input("Enter a number"))
n2=int(input("Enter another number"))
print("LCM of ",n1," and ",n2," is ",lcm(n1,n2))

Output:
Enter a number18
Enter another number24
LCM of 18 and 24 is 72
Chapter 8
STRINGS AND STRING MANIPULATIONS
Hands on Experience:
1. Write a program to find the length of a string.
Program:
s=input("Enter a string")
print("The length of the string is ", len(s))

Output:
Enter a stringHello
The length of the string is 5

2. Write the program to count the occurrences of each word in a given string.
Program:
def count_word(str):
ctr=dict()
words=str.split()
for word in words:
if word in ctr:
ctr[word]+=1
else:
ctr[word]=1
return ctr
s1=input("Enter a string with repeated words")
count=count_word(s1)
10
print(count)

Output:
Enter a string with repeated wordsA Batter bought some butter the butter
was so bitter so Batter bought some better butter to make bitter butter better

{'A': 1, 'Batter': 2, 'bought': 2, 'some': 2, 'butter': 4, 'the': 1, 'was': 1, 'so': 2,


'bitter': 2, 'better': 2, 'to': 1, 'make': 1}

3. Write a program to add a prefix text to all the lines in a string.


Program:
ch=input("Enter a prefix character")
lines=[]
print("Enter a Multiline Text")
while True:
line=input()
if line:
lines.append(line)
else:
break

for line in lines:


print(ch+line, end='\n')

Output:
Enter a prefix character$
Enter a Multiline Text
Python is easy and understandable.
I like Python programming.
Strings in Python is immutable.

$Python is easy and understandable.


$I like Python programming.
$Strings in Python is immutable.

4. Write a program to print integers with * on the right of specified width.


Program:
x=3
y=123
print("\n Original Number: ", x)
print("Formatted Number(right padding, width 2):"+"{:*<3d}".format(x))
11
print("\n Original Number: ", y)
print("Formatted Number(right padding, width 6):"+"{:*<6d}".format(y))

Output:
x=3
y=123
print("\n Original Number: ", x)
print("Formatted Number(right padding, width 2):"+"{:*<3d}".format(x))
print("\n Original Number: ", y)
print("Formatted Number(right padding, width 6):"+"{:*<6d}".format(y))

5. Write a program to create a mirror of the given string. Eg :”wel” =”lew”


Program:
def rev(str1):
str2=''
i=len(str1) - 1
while i>=0:
str2+=str1[i]
i-=1
return str2
word=input("Enter a string")
print("\nThe Mirror image of the given string is: ", rev(word))

Output:
Enter a string wel

The Mirror image of the given string is: lew


6. Write a program to remove all the occurrences of a given character in a
string.
Program:
def rem_char(s,c):
temp_str=''
for i in s:
if i==c:
pass
else:
temp_str+=i
print("The string after removing the character is ", temp_str)
str=input("Enter a string")
c=input("Enter a character to remove")
rem_char(str,c)
12
Output:
Enter a stringRajaRaja Cholan
Enter a character to removea
The string after removing the character is RjRj Choln

7. Write a program to append a string to another string without using +=


operator.
Program:
s1=input("Enter the first string")
s2=input("Enter the second string")
print("Concatenated string=",''.join([s1,s2]))

Output:
Enter the first stringComputer
Enter the second stringScience
Concatenated string= ComputerScience

8. Write a program to swap two strings.


Program:
s1=input("Enter first string")
s2=input("Enter second string")
print("Strings before swap... \n s1={} \n s2={}".format(s1,s2))
temp=s1
s1=s2
s2=temp
print("Strings after swap...\n s1={} \n s2={}".format(s1,s2))

Output:
Enter first stringGood
Enter second stringMorning
Strings before swap...
s1=Good
s2=Morning
Strings after swap...
s1=Morning
s2=Good
9. Write a program to replace a string with another string without using
replace()
Program:
13
s1=input("Enter a string")
s2=input("Enter a replacement string")
s1=s2
print("String after replacement ",s1)

Output:
Enter a stringChennai
Enter a replacement stringMadras
String after replacement Madras

10.Write a program to count the number of characters, words and lines in a


given string.
Program:
str=input("Enter a string")
chars=0
words=1
lines=0
for i in str:
chars=chars+1
if i==' ':
words=words+1
elif i =='.':
lines=lines+1
print("Number of Characters in a string:", chars)
print("Number of words in a string:", words)
print("Number of lines in a string:", lines)

Output:
Enter a stringI like Python. Strings in Python is immutable. Python is easy to
understand.
Number of Characters in a string: 76
Number of words in a string: 13
Number of lines in a string: 3

Part III
1. Write a program to display the given pattern.
COMPUTER
COMPUTE
COMPUT
COMPU
14
COMP
COM
CO
C

Program: Output:
str1="COMPUTER" COMPUTER
index=len(str1) COMPUTE
for i in str1: COMPUT
print(str1[:index]) COMPU
index-=1 COMP
COM
CO
C

Chapter 9.
LISTS,TUPLES, SETS, DICTIONARY
Hands on Experience:
1. Write a program to remove duplicates from the list.
Program:
lst=[2,4,6,8,8,10,6]
s=list(set(lst))
print(s)

Output:
[2, 4, 6, 8, 10]

2. Write a program that prints the maximum value in a tuple.


Program:
tpl=(200, 420, 120, 350)
print("Maximum Value in Tuple is ", max(tpl))

Output:
Maximum Value in Tuple is 420
3. Write a program to find the sum of all the numbers in a tuples using while
loop.
Program:
t1=()
lst=[]
n=int(input("Enter the no. of elements in Tuple"))
print("Enter the values for Tuple")
15
for i in range(0,n):
x=input()
lst.append(x)
t1=tuple(lst)
i=0
s=0
while i<len(t1):
s=s+int(t1[i])
i=i+1
print("The sum of the elements is ",s)

Output:
Enter the no. of elements in Tuple5
Enter the values for Tuple
1
2
3
4
5
The sum of the elements is 15

4. Write a program that finds sum of all even numbers in a list.


Program:
lst=[]
n=int(input("Enter the no. of elements in list"))
print("Enter the values for list")
for i in range(0,n):
x=input()
lst.append(x)
i=0
s=0
for i in range(n):
if int(lst[i])%2==0:
s=s+int(lst[i])
i=i+1
print("The sum of the even nos. in the list is ",s)

Output:
Enter the no. of elements in list5
Enter the values for list
16
12
13
14
11
16
The sum of the even nos. in the list is 42

5. Write a program that reverse the list using a loop.


Program:
list=[10,12,13,14,15,16]
lst=[]
for i in range(len(list)-1,-1, -1):
lst.append(list[i])
print("The reversed list is ",lst)

Output:
The reversed list is [16, 15, 14, 13, 12, 10]
6. Write a program to insert a value at the specified location.
Program:
lst=[10,20,30,40,60]
lst.insert(4,50)
print(lst)

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

7. Write a program that creates a list of numbers from 1 to 50 that are either
divisible by 3 or 6.
Program:
num=[]
for x in range(1,51):
if x%3==0 or x%6==0:
num.append(x)
print("The numbers divisible ny 3 or 6 are\n ",num)

Output:
The numbers divisible ny 3 or 6 are
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]

8. Write a program to create a list of numbers in the range 1 to 20. Then deletes
all the numbers from the list that are divisible by 3.
17
Program:
num=[]
for i in range(1,21):
num.append(i)
print("The numbers from 1 to 20 is\n", num)
for j,i in enumerate(num):
if i%3==0:
del num[j]
print("The list after removing numbers divisible ny 3 is\n",num)

Output:
The numbers from 1 to 20 is
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
The list after removing numbers divisible ny 3 is
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20]

9. Write the program that counts the number of times a value appears in the list.
Use loop to do the same.
Program:
lst=[]
n=int(input("Enter the number of elements in the list"))
for i in range(0,n):
x=input("Enter a element value")
lst.append(x)
num=input("Enter the element to be searched")
print("Number of times ", num ," appears using function is ", lst.count(num))
count=0
for i in lst:
if i==num:
count=count+1
print("Number of times ", num ," appears using loop is ", count)

Output:
Enter the number of elements in the list6
Enter a element value11
Enter a element value12
Enter a element value14
Enter a element value13
Enter a element value12
Enter a element value12
Enter the element to be searched12
18
Number of times 12 appears using function is 3
Number of times 12 appears using loop is 3

10. Write a program to prints the maximum and minimum value in a dictionary.
Program:
mydict={'x':500,'y':1000,'z':600}
val=mydict.values()
print("Max value is ",max(val))
print("Min Value is ",min(val))

Output:
Max value is 1000
Min Value is 500

Chapter 10
PYTHON CLASSES AND OBJECTS
Hands on Experience:
1. Write a program using class to store name and marks of students in list and
print total marks.
Program:
class student:
def __init__(self):
self.name=''
self.mark_lst=[]
self.total=0
def getdata(self):
self.name=input("Enter Name")
print("Enter 5 subject marks")
for i in range(5):
x=input()
self.mark_lst.append(x)
self.total+=int(x)
def display(self):
print("Name : ", self.name)
print("5 subject marks: ",self.mark_lst)
print("Total Marks: ", self.total)
stud=student()
stud.getdata()
19
stud.display()

Output:
Enter NameMala
Enter 5 subject marks
57
65
76
78
66
Name : Mala
5 subject marks: ['57', '65', '76', '78', '66']
Total Marks: 342

2. Write a program using class to accept three sides of a triangle and print its
area.
Program:
class triangle:
def __init__(self):
self.a=self.b=self.c=0
def getdata(self):
print("Enter three sides of a triangle")
self.a=float(input())
self.b=float(input())
self.c=float(input())
def areadisp(self):
s=(self.a+self.b+self.c)/2
ar=(s*(s-self.a)*(s-self.b)*(s-self.c))**0.5
print("The area of the triangle is ",ar)

t1=triangle()
t1.getdata()
t1.areadisp()

Output:
Enter three sides of a triangle
4
6
20
8
The area of the triangle is 11.61895003862225

3. Write a menu driven program to read, display, add and subtract two
distances.
Program:
class distance:
def __init__(self):
self.dist1=0
self.dist2=0
def read(self):
self.dist1=int(input("Enter distance1: "))
self.dist2=int(input("Enter distance2: "))
def disp(self):
print("Distance1: ",self.dist1)
print("Distance2: ",self.dist2)
def add(self):
print("Total Distance: ", self.dist1+self.dist2)
def sub(self):
print("Difference in Distance: ", self.dist1-self.dist2)

d=distance()
ch='y'
while(ch=='y'):
print("1.accept\n2.Display\n3.Total\n4.Subtract")
c=int(input("Enter your choice"))
if(c==1):
d.read()
elif(c==2):
d.disp()
elif(c==3):
d.add()
elif(c==4):
d.sub()
else:
print("Invalid Choice")
ch=input("Do you want to continue y/n")

Output:
1.accept
2.Display
21
3.Total
4.Subtract
Enter your choice1
Enter distance1: 120
Enter distance2: 230
Do you want to continue y/ny
1.accept
2.Display
3.Total
4.Subtract
Enter your choice2
Distance1: 120
Distance2: 230
Do you want to continue y/ny
1.accept
2.Display
3.Total
4.Subtract
Enter your choice3
Total Distance: 350
Do you want to continue y/ny
1.accept
2.Display
3.Total
4.Subtract
Enter your choice4
Difference in Distance: -110
Do you want to continue y/nn

PART IV
1. Write a menu driven program to add or delete stationary items. You should
use dictionary to store items and brand.
Program:
class stat_item:
def __init__(self):
self.stationary={}

def add_item(self):
n=int(input("Enter the no. of items to be added to the stationary shop"))
for i in range(n):
22
item=input("Enter an item")
brand=input("Enter the brand name")
self.stationary[item]=brand
print("The items in the shop is\n",self.stationary)
def remove_item(self):
remitem=input("Enter the item to be removed from the shop")
self.stationary.pop(remitem)
print("The items in the shop is after removing\n",self.stationary)

st=stat_item()
ch='y'
while(ch=='y'):
print("1.add item\n2.remove item")
c=int(input("Enter your choice"))
if(c==1):
st.add_item()
elif(c==2):
st.remove_item()
else:
print("Invalid Choice")
ch=input("Do you want to continue y/n")

Output:
1.add item
2.remove item
Enter your choice1
Enter the no. of items to be added to the stationary shop4
Enter an itemPencil
Enter the brand nameNatraj
Enter an itemPen
Enter the brand nameCello
Enter an itemEraser
Enter the brand nameCamlin
Enter an itemScale
Enter the brand nameCamlin
The items in the shop is
{'Pencil': 'Natraj', 'Pen': 'Cello', 'Eraser': 'Camlin', 'Scale': 'Camlin'}
Do you want to continue y/ny
1.add item
2.remove item
Enter your choice2
23
Enter the item to be removed from the shopScale
The items in the shop is after removing
{'Pencil': 'Natraj', 'Pen': 'Cello', 'Eraser': 'Camlin'}
Do you want to continue y/nn

24

You might also like