python revision short programmes
python revision short programmes
1. Write a Python program which accepts the radius of a circle from the user and
compute the area.
r = 1.1
Area = 3.8013271108436504
2. Write a Python program to get the difference between a given number and 17, if
the number is greater than 17 then print error message.
3. Write a Python program to sum of three given integers. However, if two values
are equal sum will be zero
6. Use a loop to display elements from a given list present at odd index positions
Sample Programs:
1. Write a Python program to convert a month name to number of
days. month_name = input("Input the name of Month: ")
if(month_name == "February"):
print("No. of days: 28/29 days")
elif (month_name=="April" or month_name=="June" or month_name=="September"
or month_name=="November"):
print("No. of days: 30 days")
elif (month_name=="January" or month_name=="March" or month_name=="May"
or month_name=="July" or month_name=="August" or month_name=="October"
or month_name=="December"):
print("No. of days: 31 day")
else:
print("Wrong month name")
more="Y"
count_even=0
count_odd=0
while(more=="Y"):
x=int(input("Enter a number: "))
if not x % 2:
count_even=count_even+1
else:
count_odd=count_odd+1
more=input("Do you want to continue? Y/N ")
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)
5. The program receives a number as input from user, and uses a while loop to
find the sum of digits of given number:
num=int(input("Enter a Number"))
sum = 0
while num>0:
remainder = num%10
sum = sum+remainder
num = int(num/10)
print("\nSum of Digits of Given Number: ", sum)
6. write a python program to count total digits available in a given number using
while loop.
while num:
num = int(num/10)
tot = tot+1
7. This program finds the sum of n numbers using for loop. Here the value
of n and then n numbers must be entered by user
sum = 0
n= int(input(“Enter the Value of n: "))
print("Enter " + str(n) + " Numbers: ")
for i in range(n):
num = int(input ()) # Using this block, we've received e.g 10 numbers and adds
the value and initialized to sum, one by one.
sum = sum+num
print("Sum of " + str(n) + " Numbers = " + str(sum))
using while loop
sum = 0
i=0
n=int(input("Enter the Value of n: "))
print("Enter " + str(n) + " Numbers: ")
while i<n:
num = int(input())
sum = sum+num
i = i+1
print("\nSum of " + str(n) + " Numbers = " + str(sum))
8. write a Python program to convert number of days into years, weeks and days.
num=int(input("Enter the Number of Days: "))
year = int(num/365)
week = int((num%365)/7)
days = int((num%365)%7)
10. write a Python program to swap two numbers. Both the number must be
received by user.
print("\nBefore Swap")
print("a =", a)
print("b =", b)
x=a
a=b
b=x
print("\nAfter Swap")
print("a =", a)
print("b =", b)
11. write a Python program to find factors of a number using while loop.
i=1
while i<=num:
if num%i==0:
print(i)
i = i+1
12. write a Python program to receive radius from user and print
circumference of circle.
To find average or arithmetic mean of n numbers entered by user in Python, you have
to ask from user to enter the value of n, and then n set of numbers, find and print the
average or arithmetic mean value of all those numbers as shown in the program given
below:
# the statement, num.insert(i, int(input())) states that, the value entered by user gets
inserted at ith index of num
sum = 0
for i in range(n):
sum = sum+num[i]
avg = sum/n
print("\nAverage = ", avg)
14. # Area & Perimeter of Rectangle