12 CS Practical 1 To 5 Oct2022
12 CS Practical 1 To 5 Oct2022
AIM : To write a program to calculate the factorial of the given number using for loop
EX 1 A - CODE :
EX 1 A - OUTPUT :
Enter a Number: 4
Factorial of 4 is 24
EX 1 A - RESULT :
The factorial of given number is found using for loop
EX 1 B - CODE :
Page 1 of 6
12 Computer Science / Python Programs / Practical
AIM: To write a program using functions to check whether a number is even or odd
EX 2 A - CODE :
def oddeven(a):
if (a%2==0):
print("The given number is Even")
else:
print("The given number is Odd")
EX 2 A - OUTPUT :
Enter a number: 7
Enter a number: 6
EX 2 A - RESULT:
The program is checked for Odd and Even input values
AIM: To write a program to create a mirror of the given string. For example, “wel” = “lew“.
EX 2 B - CODE :
import string
def rev(str1):
str2=""
i=len(str1)-1
while i>=0:
str2+=str1[i]
i-=1
return str2
word=input("\n Enter a String: ")
print("\n The Mirror image of the given string is: ",
rev(word))
Page 2 of 6
12 Computer Science / Python Programs / Practical
EX 2 B - OUTPUT :
Enter a String: school
EX 2 B - RESULT :
The program is checked with input string and obtained the reverse of the string
======================================================== ============f
AIM: To write a program to generate values from 1 to 10 and then remove all the odd numbers from the
list
EX 3 - CODE :
num1=[]
for i in range(1,11):
num1.append(i)
print("Numbers from 1 to 10.....\n",num1)
for j, i in enumerate(num1):
if(i%2==1):
del num1[j]
print("The values after removing odd numbers.....\n",num1)
EX 3 - OUTPUT :
Numbers from 1 to 10.....
[10 ,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1]
[10 ,8 ,6 ,4 ,2]
EX 3- RESULT
EX 3 – RESULT :
The program generated values from 1 to 10 and removed the odd numbers
Page 3 of 6
12 Computer Science / Python Programs / Practical
AIM: To write a Program that generate a set of prime numbers and another set of odd numbers. Display
the result of union, intersection, difference and symmetric difference operations
EX 4 - CODE :
EX 4 - OUTPUT :
Odd Numbers: {1, 3, 5, 7, 9}
Union: {1, 2, 3, 5, 7, 9}
Intersection: {3, 5, 7}
Difference: {1, 9}
EX 4 - RESULT:
Set of Prime numbers and Odd numbers are generated and the result of Set Operators obtained.
Page 4 of 6
12 Computer Science / Python Programs / Practical
AIM: To Write a program to accept a string and print the number of uppercase, lowercase, vowels,
Consonants and spaces in the given string using Class
EX 5 - CODE :
class String:
def __init__(self):
self.uppercase=0
self.lowercase=0
self.vowels=0
self.consonants=0
self.spaces=0
self.string=""
def getstr(self):
self.string=str(input("Enter a String: "))
def count_upper(self):
for ch in self.string:
if (ch.isupper()):
self.uppercase+=1
def count_lower(self):
for ch in self.string:
if (ch.islower()):
self.lowercase+=1
def count_vowels(self):
for ch in self.string:
if (ch in ('A', 'a', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U')):
self.vowels+=1
def count_consonants(self):
for ch in self.string:
if (ch not in ('A', 'a', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U')):
self.consonants+=1
def count_space(self):
for ch in self.string:
if (ch==" "):
self.spaces+=1
def execute(self):
self.count_upper()
self.count_lower()
self.count_vowels()
self.count_consonants()
self.count_space()
Page 5 of 6
12 Computer Science / Python Programs / Practical
def display(self):
print("The given string contains...")
print("%d Uppercase letters"%self.uppercase)
print("%d Lowercase letters"%self.lowercase)
print("%d Vowels"%self.vowels)
self.consonants=self.consonants-self.spaces
print("%d Consonants"%self.consonants)
print("%d Spaces"%self.spaces)
# creating objects
S = String()
S.getstr()
S.execute()
S.display()
EX 5 - OUTPUT :
Enter a String: Welcome to Computer Science
3 Uppercase letters
21 Lowercase letters
10 Vowels
14 Consonants
3 Spaces
EX 5 - RESULT :
The program is checked with an input string and obtained the number of upper case, lower case, vowels,
consonants and spaces
Page 6 of 6