1. Program to find area of square, rectangle and a circle.
Take input from
user.
def area_sqr(a):
return a*a
def area_rect(l,b):
return l*b
def area_circle(r):
pi=3.14
return pi*r*r
print("Enter the side of a square")
a=int(input())
print("the area of the square is",area_sqr(a))
print("Enter the length and breadth of the rectangle")
l=int(input("length="))
b=int(input("breadth="))
print("the area of the rectangle is",area_rect(l,b))
print("Enter the radius of a circle");
r=int(input())
print("the area of a circle is %.2f"%(area_circle(r)))
2. Program to count, summing and average of elements using loops.
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)
6. Python program to display all the prime numbers within an interval
lower = 2
upper = 50
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
7. Python program to count the occurrence of given word in a file.
file = open("C:\Python\Python37\D1.txt", "r")
#read content of file to string
data = file.read()
#get number of occurrences of the substring in the string
occurs = data.count("done")
print('Number of occurrences of the word :', occurs)
8. Python program to accept a sentence from user. Find the longest word in
the word and also print its length.
sentence = input("Enter a sentence: ")
# Finding longest word
longest = max(sentence.split(), key=len)
# Displaying longest word
print("Longest word is: ", longest)
print("And its length is: ", len(longest))
output:
Enter a sentence: welcome to jungle
Longest word is: welcome
And its length is: 7
9. Define a Python function with suitable parameters to generate first N Fibonacci
numbers. The first two Fibonacci numbers are 0 and 1 and the Fibonacci sequence
is defined as a function F as Fn = Fn-1 + Fn-2. Write a Python program which
accepts a value for N (where N >0) as input and pass this value to the function.
Display suitable error message if the condition for input value is not followed.
def fibonacci(n):
f1=0
f2=1
if n<1:
print("invalid number")
print(f1, end=" ")
for i in range(1,n):
print(f2,end=" ")
next=f1+f2
f1=f2
f2=next
print("enter a number to get the fibonacci series")
n=int(input("n="))
fibonacci(n)
10. Write a Python program that accepts a sentence and find the number
of words, digits, uppercase letters and lowercase letters.
s=input("enter a sentence:")
w=0
u=0
l=0
d=0
w=len(s.split())
for i in s:
if i.isdigit():
d+=1
elif i.isupper():
u+=1
elif i.islower():
l+=1
print("words=",w)
print("uppercase=",u)
print("lowercase=",l)
print("digits=",d)
Output:
enter a sentence:Hello welcome to Python3
words= 4
uppercase= 2
lowercase= 18
digits= 1
11. Create a class called Employee and initialize it with employee id and name. Design
methods to:
(i) setAge_to assign age to employee.
(ii) setSalary_to assign salary to the employee.
(iii) Display_to display all information of the employee.
class Employee:
def __init__(self,eid,ename):
self.eid=eid
self.ename=ename
def setSalary(self,salary):
self.salary=salary
def setAge(self,age):
self.age=age
def display(self):
print("Employee name is:",self.ename)
print("Employee ID is:",self.eid)
print("salary is:",self.salary)
print("Age is:",self.age)
print("enter the ID and name of the employee")
eid=int(input('ID:'))
name=input('Name:')
e=Employee(eid,name)
print("enter the salary")
salary=int(input())
e.setSalary(salary)
print("enter the age")
age=input()
e.setAge(age)
e.display()
Output:
enter the ID and name of the employee
ID:11
Name:Sri
enter the salary
3000
enter the age
34
Employee name is: Sri
Employee ID is: 11
salary is: 3000
Age is: 34