Half Yearly Programs
Half Yearly Programs
Program 1
Write a python program to find the given number is a perfect number or not.
Output:
Enter any number: 6
The number is a Perfect number!
Program 2
Write a python program to search a particular character from the string.
Output:
enter string: i hate school
enter character to find: h
2
Program 3
Write a python program to search a particular element from list of integer
numbers and display its position.
Output:
Enter list of integers: [1,2,3,4,5]
Enter your integer: 4
3
Program 4
Write a python program to find LCM and GCD of the given 2 numbers.
Program 5
Write a python program to print the given pattern.
CEGIK
DFHJ
EGI
FH
G
cnt=9
for i in range(67,72,1):
for j in range(i,i+cnt, 2):
print(chr(j),end='')
print()
cnt-=2
Output:
CEGIK
DFHJ
EGI
FH
G
Program 6
Write a python program to count the number of two letter words and three
letter words from the string.
Output:
Enter string: God we made it
The no of 2 letter words: 2
The no of 3 letter words: 1
Program 7
Write a python program to count the number of upper case and lowercase
letters from the string.
Output:
Enter string: my DeAr ComE NeaR
The no of uppercase letters are: 6
The no of lowercase letters are: 8
Program 8
Write a python program to check whether a given string is palindrome or not.
st=input("Enter string: ")
revst=st[::-1]
if st==revst:
print("The string ",st,"is a palindrome")
else:
print("The string ",st,"is not a palindrome")
Output:
Enter string: noon
The string noon is a palindrome
Program 9
Write a python program to find the prime factors of a given number.
Output:
Enter a number: 1523
1523 is a prime number.
Output:
54 is the sum of boundary
18 is the sum of non boundary
Program 11
Write a python program to find the maximum and minimum number from the
list without using any built in functions.
l=[]
n=int(input('Enter the number of elements:'))
for k in range(n):
a=int(input('Enter a number:'))
l.append(a)
max=0
for i in l:
if i>max:
max=i
min=max
for j in l:
if j<min:
min=j
print('The maximum value is:',max,'and minimum value is:',min)
Output:
Enter the number of elements:5
Enter a number:2
Enter a number:3
Enter a number:4
Enter a number:1
Enter a number:6
The maximum value is: 6 and minimum value is: 1
Program 12
Write a python program to find the second largest number of a list of
numbers.
l=[]
n=int(input('Enter the number of elements:'))
for k in range(n):
a=int(input('Enter a number:'))
l.append(a)
max=0
for i in l:
if i>max:
max=i
_2ndmax=0
for j in l:
if j>_2ndmax:
if j==max:
continue
else:
_2ndmax=j
if _2ndmax==0 and max==0:
_2ndmax=max
print('The 2nd largest number is:',_2ndmax)
Output:
Enter the number of elements:3
Enter a number:12
Enter a number:42
Enter a number:2
The 2nd largest number is: 12
Program 13
Write a python program that reads a string that capitalizes every other letter
in the string eg:computer becomes cOmPuTeR
a=input('Enter a string:')
z=''
l=len(a)
for i in range(l):
if i%2!=0:
z+=a[i].upper()
else:
z+=a[i]
print(z)
Output:
Enter a string:hello world
hElLo wOrLd
Program 14
Write a python program to find the no of vowels and no of consonants in the
given string.
s=input('Enter a string:')
v=0
c=0
for k in s:
if k.lower() in ['a','e','i','o','u']:
v+=1
else:
c+=1
print('No of vowels:',v,'No of consonants:',c)
Output:
Enter a string:i love python
No of vowels: 4 No of consonants: 9
Program 15
Write a python program to find the given n digit number is a digit palindrome
or not.
Output:
Enter a number: 636
636 is a palindrome.
Program 16
Write a python program to create a dictionary with the student details, roll no
as a key and name,Avg as list of values .delete the student details those who
have scored less than 75 from the dictionary .
student_details = {}
while True:
roll_no = input("Enter roll number (or 'done' to finish): ")
if roll_no.lower() == 'done':
break
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
student_details[roll_no] = [name, marks]
Program 17
Write a python program to accept a list of strings and display the number of
palindrome words present in it.
Output:
Enter a list of words separated by spaces: hello bob
There are 1 palindrome words in the list.
Program 18
Write a python program to print the following pattern
1
21
321
4321
54321
Program 19
Write a python program to print the given pattern
54321
5432
543
54
5
Output:
54321
5432
543
54
5
Program 20
Write a python program to check whether the given character is digit ,
lowercase alphabet ,uppercase alphabet or special character or not.
Program 21
Write a python program to find the sum of given n digit.
Output:
Enter the numbers:1234
10
Program 22
Write a python program to print the Fibonacci series of first 20 elements.
a=0
b=1
c=0
while c < 20:
print(a, end=" ")
n=a+b
a=b
b=n
c += 1
Output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Program 23
Write a python program to find the sum of the following series x+x2/2! –
x3/3!+x4/4!-………. upto n terms.
import math
n = int(input("Enter the no. of terms:"))
x = int(input("Enter the value of x:"))
sum = 0
for i in range(1, n+1):
if i % 2 != 0:
sum -= (x**i) / math.factorial(i)
else:
sum += (x**i) / math.factorial(i)
print(sum)
Output:
Enter the no. of terms:3
Enter the value of x:2
-1.3333333333333333
Program 24
Write a python program to print the prime and composite numbers from the
list of integer numbers.
Output:
Enter the list of numbers: 3,4,5,6
The prime numbers are: [3, 5]
The composite numbers are: [4, 6]
Program 25
Write a python program to count the frequency of list elements and store in a
dictionary
l = input("Enter the elements:").split()
d = {}
for i in l:
b = l.count(i)
d[i] = b
print(d)
Output:
Enter the elements:hey hello
{'hey': 1, 'hello': 1}
Program 26
Write a python program to create a dictionary from a string with every word
of the string as the key and length of the string as value.
d={}
a=str(input("ENTER A STRING: "))
l=a.split()
for i in l:
d[i]=len(i)
print(d)
Output:
ENTER A STRING: HI HELLO FRIENDS
{'HI': 2, 'HELLO': 5, 'FRIENDS': 7}
Program 27
Write a python program to find the sum of the series x-x3/3!+x5/5!-x7/7!+.... up
to n terms.
import math
x=int(input("ENTER THE VALUE OF X: "))
n=int(input("ENTER NUMBER OF TERMS: "))
s=0
c=1
for i in range(0,n):
s+=(-1)**i*((x**c)/(math.factorial(c)))
c+=2
print(s)
Output:
ENTER THE VALUE OF X: 2
ENTER NUMBER OF TERMS: 2
0.6666666666666667
Program 28
Write a python program to replace the repeating elements in a list with zero.
Output:
ENTER THE NUMBERS: 2,4,5,4,2,3
[2, 4, 5, 0, 0, 3]
Program 29
Write a python program to display the sum of alternate elements in the list.
Output:
ENTER THE NUMBERS: 2,5,7,3,4
13
Program 30
Write a python program to sum of all the elements in the list which ends with
25.
Output:
ENTER THE NUMBERS: 25,225,125,44352,752,256
375
Program 31
Write a python program to calculate the mean of a tuple with numeric values.
Output:
Enter a numeric tuple:(1,2,3)
The mean of the tuple is:2.0
Program 32
Write a python program to display the sum of left diagonal elements and right
diagonal elements of the two dimensional list.
Output:
Enter any matrix:[[1,2,3],[2,6,4],[15,65,8]]
the left diagonal is :15
the right diagonal is :24
Program 33
Write a python program to accept a list of elements and exchange the first
half with the second half of the list.
L=list(eval(input("Enter a list:")))
m=len(L)//2
if len(L)%2==0:
L=L[m:]+L[:m]
else:
L=L[m+1:]+[L[m]]+L[:m]
print(“the final list is :”,L)
Output:
Enter a list:[1,2,3,4,5]
the final list is :[4,5,3,1,2]
Program 34
Write a program to count the number of times a character appears in a string.
s=input("Enter string:")
c=input("Enter the char to count:")
count=0
for i in s:
if i==c:
count+=1
print("The no of time the character",c,"appeared in the string is ",count)
Output:
Enter string:happy
Enter the char to count:p
The no of time the character p appeared in the string is 2
Program 35
Write a python program to find the sum of the series 1-x+x2-x3+x4-…..xn .
Output:
Enter value of x:2
Enter no of terms:5
the sum of series is:-21.0
Program 36
Write a python program to display the matrix addition and subraction of the
two dimensional lists.
x = [[1,2,3],[4,5,6],[7,8,9]]
y = [[11,12,13],[14,15,16],[17,18,19]]
result = [[0,0,0],[0,0,0],[0,0,0]]
sub = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(x)):
for j in range(len(x[0])):
result[i][j] = x[i][j] + y[i][j]
sub[i][j] = x[i][j] - y[i][j]
print('Addition of Matrix : ')
for r in result:
print(r)
print('Subtraction of Matrix : ')
for r in sub:
print(r)
Output:
Addition of Matrix :
[12, 14, 16]
[18, 20, 22]
[24, 26, 28]
Subtraction of Matrix :
[-10, -10, -10]
[-10, -10, -10]
[-10, -10, -10]
Program 37
Write a python program to display the matrix Multiplication of the two
dimensional lists.
Output:
[19, 22]
[43, 50]
Program 38
Write a python program to accept the details of Employees such as Emp no,
Emp name, Basic, HRA,DA from user and Calculate Annual Salary of Every
Employee using Dictionaries.
employees = {}
num_employees = int(input("Enter the number of employees: "))
for _ in range(num_employees):
emp_no = input("Enter Employee Number: ")
emp_name = input("Enter Employee Name: ")
basic = float(input("Enter Basic Salary: "))
hra = float(input("Enter HRA: "))
da = float(input("Enter DA: "))
annual_salary = (basic + hra + da) * 12
employees[emp_name] = annual_salary
print("\nEmployee Name and Annual Salary Dictionary:")
print(employees)
print("\nEmployee Details and Annual Salary:")
for emp_name, annual_salary in employees.items():
print(f"Employee Name: {emp_name}")
print(f"Annual Salary: {annual_salary}")
Output:
Enter the number of employees: 2
Enter Employee Number: 1
Enter Employee Name: kumar
Enter Basic Salary: 40000
Enter HRA: 18000
Enter DA: 18000
Enter Employee Number: 2
Enter Employee Name: suresh
Enter Basic Salary: 50000
Enter HRA: 22000
Enter DA: 23000
Program 39
Write a python program to create a list of strings and add a word as key in
dictionary and no. of vowels in a word as a value.
l = []
d= {}
c=0
n = int(input('Enter n : '))
for i in range(n):
a = input('Enter a string : ')
l.append(a)
for i in l :
for j in i:
if j in 'aeiouAEIOU':
c += 1
d[i] = c
c=0
print(d)
Output:
Enter n : 2
Enter a string : apple
Enter a string : banana
{'apple': 2, 'banana': 3}
Program 40
Write a python program to create a dictionary containing top players and their
runs as key value pairs of a cricket team and delete the players name where
the corresponding values is greater than 50 from the dictionary and display it.
cricket_team = {
"Virat Kohli": 75,
"Rohit Sharma": 60,
"KL Rahul": 45,
"Rishabh Pant": 30,
"Hardik Pandya": 55
}
for player, runs in list(cricket_team.items()):
if runs > 50:
del cricket_team[player]
print(cricket_team)
Output:
{'KL Rahul': 45, 'Rishabh Pant': 30}