0% found this document useful (0 votes)
29 views21 pages

Half Yearly Programs

Uploaded by

tktk5965
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views21 pages

Half Yearly Programs

Uploaded by

tktk5965
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

HALF YEARLY PROGRAMS

Program 1
Write a python program to find the given number is a perfect number or not.

n = int(input("Enter any number: "))


sum1 = 0
for i in range(1, n):
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
print("The number is a Perfect number!")
else:
print("The number is not a Perfect number!")

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.

s=input("enter string: ")


x=input("enter character to find: ")
if x in s:
print(s.find(x))
else:
print("not found")

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.

L=list(eval(input("Enter list of integers: ")))


n=int(input("Enter your integer: "))
print(L.index(n))

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.

n1=int(input("Enter n1: "))


n2=int(input("Enter n2: "))
if n1>n2:
greater=n1
else:
greater=n2
while True:
if ((greater%n1==0) and (greater%n2==0)):
lcm=greater
break
greater=greater+1
print("lcm is" , lcm)
for i in range(greater,0,-1):
if n1%i==0 and n2%i==0:
gcd=i
break
print("GCD is", gcd)
Output:
Enter n1: 6
Enter n2: 8
lcm is 24
GCD is 2

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.

st=input("Enter string: ")


l=st.split()
count=0
count2=0
count3=0
for i in l:
count=0
for j in i:
count+=1
if count==2:
count2+=1
elif count==3:
count3+=1
print("The no of 2 letter words: ",count2)
print("The no of 3 letter words: ",count3)

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.

st=input("Enter string: ")


Ucount=0
Lcount=0
for i in st:
if i.isupper():
Ucount+=1
elif i.islower():
Lcount+=1
print("The no of uppercase letters are: ",Ucount)
print("The no of lowercase letters are: ",Lcount)

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

Enter string: diary


The string diary is not a palindrome

Program 9
Write a python program to find the prime factors of a given number.

n=int(input("Enter a non Zero number: "))


if n==1:
print("1 is neither prime nor composite")
else:
a=True
for i in range(2,n):
if n%i==0:
a=False
break
else:
continue
if a==True:
print(n," is a prime number.")
else:
print(n," is a composite number.")

Output:
Enter a number: 1523
1523 is a prime number.

Enter a number: 1523


1523 is a prime number.
Program 10
Write a python program to display the sum of boundary elements and non -
boundary elements of the Two-dimensional list.

a = [[1, 2, 3, 4], [5, 6, 7, 8],


[1, 2, 3, 4], [5, 6, 7, 8]]
r=len(a)
c=len(a[0])
#only if all the sub lists have the same no of elements
sumb=0
sumnb=0
for i in range(r):
for j in range(c):
if i==0 or j==0 or i==(r-1) or j==(c-1):
sumb+= a[i][j]
elif not i==0 or j==0 or i==(r-1) or j==(c-1):
sumnb+= a[i][j]
print (sumb,"is the sum of boundary")
print (sumnb,"is the sum of non boundary")

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.

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


on=n
rn = 0
while n > 0:
rn = rn * 10 + (n % 10)
n //= 10
if on == rn:
print(on,' is a palindrome.')
else:
print(on,'is not a palindrome.')

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]

for roll_no, details in list(student_details.items()):


if details[1] < 75:
del student_details[roll_no]
print(student_details)
Output:
Enter roll number (or 'done' to finish): 1
Enter student name: akash
Enter marks: 56
Enter roll number (or 'done' to finish): 2
Enter student name: amit
Enter marks: 87
Enter roll number (or 'done' to finish): done
{'2': ['amit', 87]}

Program 17
Write a python program to accept a list of strings and display the number of
palindrome words present in it.

words = input("Enter a list of words separated by spaces: ").split()


count = 0
for word in words:
if word == word[::-1]:
count += 1
print("There are",count,"palindrome words in the list.")

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

for i in range(1, 6):


for j in range(i, 0, -1):
print(j, end=" ")
print()
Output:
1
21
321
4321
54321

Program 19
Write a python program to print the given pattern
54321
5432
543
54
5

for i in range(5, 0, -1):


for j in range(5, 5 - i, -1):
print(j, end=" ")
print()

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.

char = input("Enter a character: ")


if char.isdigit():
print("'" + char + "' is a digit.")
elif char.islower():
print("'" + char + "' is a lowercase alphabet.")
elif char.isupper():
print("'" + char + "' is an uppercase alphabet.")
else:
print("'" + char + "' is a special character.")
Output:
Enter a character: 7
'7' is a digit.

Program 21
Write a python program to find the sum of given n digit.

n = int(input("Enter the numbers:"))


a = str(n)
Sum = 0
for i in a:
Sum += int(i)
print(Sum)

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.

l = list(eval(input("Enter the list of numbers: ")))


p = []
c = []
for num in l:
if num < 2:
continue
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
c.append(num)
break
else:
p.append(num)

print("The prime numbers are:", p)


print("The composite numbers are:", c)

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.

l=list(eval(input("ENTER THE NUMBERS: ")))


r=[]
for i in range(len(l)):
if l[i] in r:
l[i] = 0
else:
r.append(l[i])
print(l)

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.

l=list(eval(input("ENTER THE NUMBERS: ")))


s=0
for i in range(0,len(l),2):
s+=l[i]
print(s)

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.

l=list(eval(input("ENTER THE NUMBERS: ")))


s=0
for i in l:
a=str(i)
if a[-2]=='2':
if a[-1]=='5':
s+=i
print(s)

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.

t=tuple(eval(input("Enter a numeric tuple:")))


b=len(t)
sum=0
for i in t:
sum+=i
m=sum/b
print("The mean of the tuple is:",m)

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.

L=list(eval(input("Enter any matrix:")))


a=0
b=0
for i in range(len(L)):
a+=L[i][i]
b+=L[i][len(L)-1-i]
print(“the left diagonal is :”,a)
print(“the right diagonal is :”,b)

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 .

x=float(input("Enter value of x:"))


n=int(input("Enter no of terms:"))
sum=0
for i in range(n+1):
t=x**i
if i%2==0:
t=-t
sum+=t
print(“the sum of series is:”, sum)

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.

A = [[1, 2],[3, 4]]


B = [[5, 6],[7, 8]]
rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])
if cols_A != rows_B:
print("Multiplication not possible")
else:
result = [[0,0],[0,0]]
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += A[i][k] * B[k][j]
for row in result:
print(row)

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

Employee Name and Annual Salary Dictionary:


{'kumar': 912000.0, 'suresh': 1140000.0}

Employee Details and Annual Salary:


Employee Name: kumar
Annual Salary: 912000.0
Employee Name: suresh
Annual Salary: 1140000.0

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}

You might also like