Python Programs-From VTU QP
Python Programs-From VTU QP
list=[12,34,28,19,29]
count=0
sum=0
avg=0.0
for i in list:
count=count+1
sum=sum+i
avg=sum/count
print("the number of elements are:",count)
print("sum of the elements is:",sum)
print("the average of elements is:",avg)
3. Python program to i) find largest of 3 numbers ii) check whether the given
year is leap year or not with functions.
def largest(a,b,c):
if a>b and a>c:
return a
elif b>a and b>c:
return b
else:
return c
def leap_year(year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
print("enter 3 numbers")
a=int(input("1st number:"))
b=int(input("2nd number:"))
c=int(input("3rd number:"))
print("the largest among three is:",largest(a,b,c))
print("******************************");
print("enter a year");
y=int(input())
leap_year(y)
4. Python program to create a user defined function to find length of a
string, find max and min letter in string.
def str_len(s):
count=0
for i in s:
count+=1
return count
print("enter a string");
s=input()
d={}
for i in s:
if i in d:
d[i]=d[i]+1
else:
d[i]=1
max_occurs=max(d,key=d.get)
min_occurs=min(d,key=d.get)
print("letter with maximum occurence is ",max_occurs)
print("letter with minimum occurence is ",min_occurs)
5. Python program to concatenate and compare two strings. Read the strings
from user.
s1=input("enter the 1st string S1:")
s2=input("enter the 2nd string S2:")
if s1==s2:
print("strings are same")
else:
print("Strings are different")
print("The concatenation two strings is:",s1+s2)