Python Programs
Python Programs
Python Programs
ipynb - Colaboratory
#1.Design a Python program to find the average of best two marks out of three marks taken as
print("Enter marks:")
m1,m2,m3=map(int,input().split())
a1=(m1+m2)/2
a2=(m2+m3)/2
a3=(m3+m1)/2
if(a1>a2 and a1>a3):
print("Average=",a1)
elif(a2>a1 and a2>a3):
print("Average=",a2)
else:
print("Average=",a3)
Enter marks:
26 29 30
Average= 29.5
#2Write a program to read the following input from user and display. a. Name : b. USN :
print("Enter student details")
Name=input("Name:")
USN=input("USN:")
RollNO=int(input("Roll No:"))
Mobile=input("Mobile No:")
Email=input("E-mail ID:")
Marks=int(input("Percentage of Marks :"))
print("Name:",Name)
print("USN:",USN)
print("Roll No:",RollNO)
print("Mobile No:",Mobile)
print("E-mail ID:",Email)
print("Percentage of Marks:",Marks)
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 1/11
4/2/2020 passign.ipynb - Colaboratory
(a(b+c))
True
#4.A positive integer m is a sum of squares if it can be written as k + x where k > 0, x > 0
def sumofsquares(m):
if m<0:
return False
for i in range(1,int(m**0.5)+1):
for j in range(1,int(m**0.5)+1):
n=i**2+j**2
if(n==m):
return True
elif n>m:
break
return False
m=int(input("Enter value of m:"))
print(sumofsquares(m))
import math
def sumofsquares(m):
if m<0:
return False
l=[]
for i in range(m+1):
if((math.sqrt(i)-math.floor(math.sqrt(i)))==0):
l.append(i)
for i in l:
for j in l:
if(i+j==m):
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 2/11
4/2/2020 passign.ipynb - Colaboratory
if(i+j==m):
return True
return False
m=int(input("Enter value of m:"))
print(sumofsquares(m))
#5.Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, p
def computegrade(s):
if(s<0.4):
return "F"
elif(s<=0.4):
return 'E'
elif(s<=0.5):
return 'D'
elif(s<=0.6):
return 'C'
elif(s<=0.7):
return 'B'
elif(s<=0.8):
return 'A'
elif(s<=0.9):
return 'S'
else:
return "The score is out of range."
score=float(input("Enter the score:"))
print(computegrade(score))
Enter a string:hello
Reversed string is olleh
#7.Write a program to display all the palindrome words appearing in an input text.
s=input("Enter the text:")
l=s.split(" ")
for i in l:
if(i==i[::-1]):
print(i)
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 3/11
4/2/2020 passign.ipynb - Colaboratory
#8.Write a python program to display the ASCII code of the given character.
c=input("Enter the character")
print("ASCII code:",ord(c))
#9.Write a python program to compute percentage score of a student by reading his scores in 6
name=input("Enter name:")
d={}
for i in range(6):
s=input("Enter subject:")
marks=int(input("Enter marks:"))
d[s]=marks
sum=0
m1=0
m2=100
for i in d.keys():
sum=sum+d[i]
if(m1<d[i]):
m1=d[i]
k1=i
if(m2>d[i]):
m2=d[i]
k2=i
print("Name=",name)
print("Average=",sum/6)
print("Less scored in subject {0} and score is {1}".format(k2,m2))
print("Highest scored in subject {0} and score is {1}".format(k1,m1))
Enter name:Shrithi
Enter subject:ME
Enter marks:75
Enter subject:CN
Enter marks:81
Enter subject:DBMS
Enter marks:81
Enter subject:Java
Enter marks:80
Enter subject:DotNet
Enter marks:84
Enter subject:ATC
Enter marks:82
Name= Shrithi
Average= 80.5
Less scored in subject ME and score is 75
Highest scored in subject DotNet and score is 84
#10.Write a python program to prompt the user for hours and rate per hour to compute gross pa
hours=int(input("Enter hour:"))
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 4/11
4/2/2020 passign.ipynb - Colaboratory
Enter hour:3
Enter Rate per hour:22
Gross pay: 66.0
#11.Write a program which prompts the user for a Celsius temperature, covert the temperature
c=int(input("Enter temperature in Celsius:"))
f=(c*9/5)+32
print("Temperature in Fahrenheit:",f)
#12.Write a python program to read a number A which contains only digits 0's and 1's. Check i
b=input()
c=b.count('0')
c1=b.count('1')
if(c==1 or c1==1):
print("YES")
else:
print("NO")
0011
NO
#13.Write a program to list out all prime numbers between two integers m and n.
print("Enter m and n:")
m,n=map(int,input().split())
f=0
for i in range(m+1,n):
if(i>1):
for j in range(2,i):
if(i%j==0):
f=1
break
if(f==0):
print(i)
f=0
Enter m and n:
3 15
5
7
11
13
#15.Write a Python function to get a string made of the first 2 and the last 2 chars from a g
def string(s):
if(len(s)<2):
return ""
elif(len(s)==2):
return s
else:
return (s[0:2]+s[-2]+s[-1])
s=input("Enter the string:")
print(string(s))
#16.Consider a string s=“Hello how are you?”. Write a program to display this string without
s="Hello how are you?"
l=s.split(" ")
s="".join(l)
print(s)
Hellohowareyou?
#17.Write a Python program to remove the characters which have odd index values of a given st
s=input("Enter the string:")
print(s[0:len(s):2])
#18.Write a Python program to change a given string to a new string where the first and last
s=input("Enter the string:")
l=list(s)
l[0]=s[-1]
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 6/11
4/2/2020 passign.ipynb - Colaboratory
l[len(s)-1]=s[0]
s="".join(l)
print(s)
#19.Write a Python program to add 'ing' at the end of a given string (length should be at lea
s=input("Enter the string:")
if(len(s)<3):
print(s)
elif((s[-3:-1]+s[-1])=="ing"):
print(s+"ly")
else:
print(s+"ing")
#20.Write a python program to generate first n Fibonacci number and factorial of n using func
def Fibonacci(n):
fib=[]
fib.append(0)
fib.append(1)
for i in range(2,n+1):
fib.append(fib[i-1]+fib[i-2])
print("Fibonacci numbers are")
for i in fib:
print(i,end=' ')
print()
def factorial(n):
fact=1
for i in range(n,1,-1):
fact=fact*i
return fact
n=int(input("Enter n:"))
Fibonacci(n)
print("Factorial of {0} is:{1}".format(n,factorial(n)))
Enter n:3
Fibonacci numbers are
0 1 1 2
Factorial of 3 is:6
Enter 2 numbers
54 24
The HCF is 6
The LCM is 216
#22.Write a python program to illustrate reverse, compare, copy and concatenate operations on
s1="Indian "
s2="Army"
#Reverse
print("Reversed string of {0} is {1}".format(s1,s1[::-1]))
#Compare
if(s1==s2):
print("{0} and {1} are equal".format(s1,s2))
else:
print("{0} and {1} are not equal".format(s1,s2))
#copy
s3=s1[:]
print("The copied string is ",s3)
#concatenation
s4=s1+s2
print("Concacatenation of {0} and {1} is {2}".format(s1,s2,s4))
#length of the string
c=0
for i in s2:
c=c+1
print("Length of the string {0} is {1}".format(s2,c))
#23.Write a python program to evaluate the polynomial f'(x): a4x4 + a3x3 + a2x2 + a1x1 + a0,
def horner(poly,n,x):
result=poly[0]
for i in range(1,n):
result=result*x+poly[i]
return result
l=[]
print("Enter a coefficients value")
( )
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 8/11
4/2/2020 passign.ipynb - Colaboratory
for i in range(4):
l.append(int(input()))
x=int(input("Enter a X value:"))
n=4
print("Value of polynomial is ",horner(l,n,x))
#25.Write a python function isprime(num) that accepts an integer argument and returns 1 if th
def isprime(num):
if(num>1):
for i in range(2,num):
if(num%i==0):
return 0
return 1
print("Enter range of numbers:")
x,y=map(int,input().split())
for i in range(x+1,y):
print("{0}={1}".format(i,isprime(i)))
#26.Write a python program to replace each constant in a string with the next one except lett
s=input("Enter the string")
l=list(s)
for i in range(len(l)):
if(l[i]!='z' and l[i]!='Z' and l[i]!='a' and l[i]!='A' and l[i]!=' '):
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 9/11
4/2/2020 passign.ipynb - Colaboratory
if(l[i]!= z and l[i]!= Z and l[i]!= a and l[i]!= A and l[i]!= ):
k=ord(l[i])
k=k+1
k=chr(k)
l[i]=k
print("".join(l))
#27.Write a program to read through a file and print the contents of the file (line by line)
try:
fname=input("Enter filename:")
fp=open(fname,"r")
c=0
l=fp.readlines()
for i in l:
print(i.upper())
c=c+len(i.split(" "))
fp.seek(0)
print("No of characters={0}",len(fp.read()))
print("No of words={0}",c)
print("No of lines={0}",len(l))
finally:
fp.close()
#28.Write a program to prompt for a file name, and then read through the file and look for li
fname=input("Enter filename:")
fp=open(fname,"r")
c=0
cgpa=0
l=fp.readlines()
for i in l:
k=i.find("CGPA")
if(k!=-1):
c=c+1
k=k+5
j=i[k:(k+4)]
k=float(j)
cgpa=cgpa+k
print("Average CGPA is ",cgpa/c)
fp.close()
f
#29.Write a program to search and print all the lines starting with a specific word (taken as
fname=input("Enter filename:")
fp=open(fname,"r")
l=fp.readlines()
s="Hello"
for i in l:
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 10/11
4/2/2020 passign.ipynb - Colaboratory
if(i.startswith(s)):
print(i,end="")
fp.close()
#30.Given two university information files “studentname.txt” and “usn.txt” that contains stud
fname=input("Enter filename:")
fp=open(fname,"w")
fp1=open("studentname.txt","r")
fp2=open("usn.txt","r")
l1=fp1.readlines()
l2=fp2.readlines()
l=" Name : USN\n"
fp.write(l)
for i in range(len(l1)):
l=l1[i].strip()+":"+l2[i]
fp.write(l)
fp=open(fname,"r")
print(fp.read())
fp.close()
fp1.close()
fp2.close()
https://colab.research.google.com/drive/1M784wMAJao2UeSgFo78hFPXHjW7G72LE#scrollTo=6JWTh9dvgzBs&printMode=true 11/11