12 Computer Science / Python Programs / Practical
12 Computer Science / Python Programs / Practical
PY1(a) - Calculate Factorial
AIM : To write a program to calculate the factorial of the given number using for loop
EX 1 A - CODE :
num = int(input("Enter a Number: "))
if (num==0):
fact = 1
fact = 1
for i in range(1,num+1):
fact = fact * i
print("Factorial of ", num, " is ", fact)
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
========================== ========================== ================
PY1(b) - Sum of Series
AIM : To write a program to sum the series:1/1 + 2 ^2 /2 + 3^3 /3 + ……. n^n /n
EX 1 B - CODE :
n = int(input("Enter a value of n: "))
s=0.0
for i in range(1,n+1):
a=float(i**i)/i
s=s+a
print("The sum of the series is ", s)
EX 1 B - OUTPUT :
Enter a value of n: 4
The sum of the series is 76.0
EX 1 B - RESULT : The sum of series is found for a given number of terms
========================== ========================== ================
Page 1 of 6
12 Computer Science / Python Programs / Practical
PY2(a) - Odd or Even
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")
num = int(input("Enter a number: "))
oddeven(num)
EX 2 A - OUTPUT :
Enter a number: 7
The given number is Odd
Enter a number: 6
The given number is Even
EX 2 A - RESULT:
The program is checked for Odd and Even input values
========================== ========================== ================
PY2(b) - Reverse the String
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
The Mirror image of the given string is: loohcs
EX 2 B - RESULT :
The program is checked with input string and obtained the reverse of the string
======================================================== ============f
PY3 – Generate values and remove odd numbers
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]
The values after removed odd numbers.....
[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
PY4 – Generate Prime numbers and Set Operations
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 :
odd=set([x*2+1 for x in range(0,5)])
primes=set()
for i in range(2,10):
j=2
f=0
while j<=i/2:
if i%j==0:
f=1
j+=1
if f==0:
primes.add(i)
print("Odd Numbers: ", odd)
print("Prime Numbers: ", primes)
print("Union: ", odd.union(primes))
print("Intersection: ", odd.intersection(primes))
print("Difference: ", odd.difference(primes))
print("Symmetric Difference: ",
odd.symmetric_difference(primes))
EX 4 - OUTPUT :
Odd Numbers: {1, 3, 5, 7, 9}
Prime Numbers: {2, 3, 5, 7}
Union: {1, 2, 3, 5, 7, 9}
Intersection: {3, 5, 7}
Difference: {1, 9}
Symmetric Difference: {1, 2, 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
PY5 – Display sting elements – Using Class
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
The given string contains...
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