Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4K views

+2 Computer Science: Python Programs For Practical

This document contains Python programs for +2 Computer Science practicals, including programs to calculate the factorial of a number, sum a series, check if a number is odd or even using functions, create a mirror of a string, generate and remove odd numbers from a list, perform set operations on odd and prime numbers, and analyze a string using a class. The programs demonstrate fundamental Python concepts like loops, functions, classes, and sets.

Uploaded by

Priya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views

+2 Computer Science: Python Programs For Practical

This document contains Python programs for +2 Computer Science practicals, including programs to calculate the factorial of a number, sum a series, check if a number is odd or even using functions, create a mirror of a string, generate and remove odd numbers from a list, perform set operations on odd and prime numbers, and analyze a string using a class. The programs demonstrate fundamental Python concepts like loops, functions, classes, and sets.

Uploaded by

Priya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

+2 Computer Science

Python Programs for Practical

Video Lessons for


+2 Computer Science
+2 Computer Applications
+1 Computer Science
@
https://www.youtube.com/c/alphakarumpalagai
Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 1
Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 2
1
Write a program to calculate the factorial of the given number using loop

num = int(input("Enter a Number:"))


res = 1
for i in range(1,num+1):
res = res * i
print("Factorial of", num, "is", res)

Output
Enter a Number:12
Factorial of 12 is 479001600

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 3


2
Write a program to sum sum the series
𝟏 𝟐𝟐 𝟑𝟑 𝟒𝟒 𝒏𝒏
+ + + +⋯
𝟏 𝟐 𝟑 𝟒 𝒏

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)

Output
Enter a value of n:4
The sum of the series is 76.0

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 4


3A
Write a program using functions to check whether a number is odd or even
Using if … else …

def oddeven(a):
if (a%2==0):
return 1
else:
return 0
num = int(input("Enter a number:"))
if(oddeven(num)==1):
print("The given number is Even")
else:
print("The given number is Odd")

Output
Enter a number:7
The given number is Odd

Enter a number:6
The given number is Even

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 5


3B
Write a program using functions to check whether a number is odd or even
Using if … elif …

def oddeven(a):
if (a%2==0):
return 1
else:
return 0

num = int(input("Enter a number:"))


if(oddeven(num)==1):
print("The given number is Even")
elif(oddeven(num==0)):
print("The given number is Odd")

Output
Enter a number:7
The given number is Odd

Enter a number:6
The given number is Even

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 6


4
Write a program to create a mirror of the given string.
For example "wel" = "lew"

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

Output
Enter a String:school
The mirror image of given string is :loohcs

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 7


5
Write a program to generate values from 1 to 10 and then remove all the odd
numbers from the list.

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 removed odd numbers .....\n",num1)

Output
Numbers from 1 to 10 .....
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The values after removed odd numbers .....
[2, 4, 6, 8, 10]

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 8


6
Write a program that generates a set of prime numbers and another set of
odd numbers. Display the result of union, intersection, difference and
symmetric difference operations.

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))

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 9


Output
Odd Numbers: {1, 3, 9, 5, 7}
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}

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 10


7
Write a program to accept a string and print the number of uppercase,
lowercase, vowels, consonants and spaces in the given string using class.

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 = 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

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 11


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()
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)
print("%d Consonants" %self.consonants)
print("%d Spaces" %self.spaces)
S = String()
S.getstr()
S.execute()
S.display()

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 12


Output
Enter a string:Welcome to Computer Science
The given string contains ....
3 Uppercase letters
21 Lowercase letters
10 Vowels
17 Consonants
3 Spaces

Alpha Karumpalagai +2 Computer Science Python Programs for Practical Page 13

You might also like