D.A.V.
PUBLIC SCHOOL, NEW PANVEL
Plot No. 267, 268, Sector-10, New Panvel,
Navi Mumbai-410206 (Maharashtra).
Phone 022-27468211, 27451793
E-mail –
[email protected], www.davnewpanvel.com
Academic Session 2024-2024
Worksheet-1
Revision Tour
Python Programs
1) Program to find whether the year entered is leap Year or not.
Source Code:
y=int(input("Enter the year in yyyy format"))
j=y%4
k=y%400
l=y%100
if(j==0 or k==0 or l==0):
print("It is a leap year")
else:
print("It is not a leap year ")
Output:
Enter the year in yyyy format2018
It is not a leap year
2) Program to display the working of power operator in python.
Source Code:
x=int(input("Enter first no : "))
y=int(input("Enter Second no : "))
pw=x**y
print(("The ANSWER IS:,(x,y,pw))
Output:
Enter first no : 2
Enter Second no : 2
The value of 2 raised to 2 is 4
3) Program to find the greater of two nos.
Source Code:
a=int(input("Enter first no : "))
b=int(input("Enter Second no : "))
if(a>b):
print (a,’is greater’)
else:
print (b,’ is greater’)
Output:
Enter first no : 34
Enter Second no : 87
87 is greater
4) Program to find the greatest of three nos.
Source Code:
a=int(input("Enter first no : "))
b=int(input("Enter Second no : "))
c=int(input("Enter third no : "))
if(a>b and a>c):
print (a,’is greater’)
elif(b>c):
print (b,’is greater’)
else:
print (c,’is greater’)
Output:
Enter first no : 23
Enter Second no : 87
Enter third no : 43
87 is greatest
5) Program to Print total percentage and grade from given marks.
Source Code:
a=int(input("Enter first subject marks(0-100): "))
b=int(input("Enter Second subject marks(0-100): "))
c=int(input("Enter third subject marks(0-100): "))
d=int(input("Enter fourth subject marks(0-100): "))
e=int(input("Enter fifth subject marks(0-100): "))
s=a+b+c+d+e
t=s/5
if(t>=90):
g='A'
elif(t<90 and t>=60):
g='B'
elif(t<60 and t>=45):
g='C'
elif(t<45 and t>=33):
g='D'
else:
g='E'
print(("The total marks are %d out of 500")%s)
print(("The percentage is %f")%t)
print(("The grade is %c")%g)
Output:
Enter first subject marks(0-100): 78
Enter Second subject marks(0-100): 76
Enter third subject marks(0-100): 89
Enter fourth subject marks(0-100): 90
Enter fifth subject marks(0-100): 86
The total marks are 419 out of 500
The percentage is 83.800000
The grade is B
6) Program to print numbers in a given range
Source Code:
for i in range(1,11):
print(i,end=',')
Output:
1,2,3,4,5,6,7,8,9,10
7) Program to print the square of numbers in a given range
Source Code:
for i in range(1,11):
print(i*i,end=' ')
Output:
1 4 9 16 25 36 49 64 81 100
8) Program to print Fibonacci Series
Source Code:
f=0
s=1
a=int(input("Enter the no of terms :"))
print(f,s,end=' ')
for i in range(0,a):
t=f+s
f=s
s=t
print(t,end=' ')
Output:
Enter the no of terms: 8
0 1 1 2 3 5 8 13 21 34
9) Program to print the table of a number.
Source Code:
a=int(input("Enter the no whose table you want :"))
for i in range(1,11):
t=a*i
print (a, ’X’, i , ’=’,t)
Output:
Enter the no whose table you want: 15
15X1=15
15X2=30
15X3=45
15X4=60
15X5=75
15X6=90
15X7=105
15X8=120
15X9=135
15X10=150
10) Program to find the Factorial of a number.
Source Code:
a=int(input("Enter the no whose factorial you want :"))
t=1
for i in range(1,a+1):
t=t*i
print ("The factorial is:’,t)
Output:
Enter the no whose factorial you want :8
The factorial is 40320
11) Program to print Binary equivalent of a given integer
Source Code:
q=int (input ("Enter an integer"))
r= []
cnt=0
while(q>=1):
if((q%2) ==0):
x=0
r.append(x)
else:
x=1
r.append(x)
q=q//2
cnt=cnt+1
for i in range(cnt-1, -1,-1):
print(r[i], end=' ')
Output:
Enter an integer78
1001110
12) Program to print Nested for loop pattern
Source Code:
for i in range (1,6):
for j in range(1, i+1):
print (i,end= " ")
print ("")
Output:
1
22
333
4444
55555
13) Program to print the given no. in reverse order
Source Code:
no=int(input("Enter Number : "))
while no>0:
print(no % 10,end=””)
no=int(no / 10)
Output:
Enter Number : 327
723
14) Write a Python program to count the number of even and odd numbers
from a series of numbers 5 to 100.
Source Code:
count_odd = 0
count_even = 0
for x in range(1,101):
if not x % 2:
count_even+=1
else:
count_odd+=1
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)
Output is:
Number of even numbers : 50
Number of odd numbers : 50
15) Write python code to find GCD of 2 numbers.
Source code:
a=int(input('Enter no.1:'))
b=int(input('Enter no.2:'))
while a!=b:
if a>b:
a=a-b
else:
b=b-a
print('Gcd is:',a)
Output is:
enter a:12
enter b:15
GCD is: 3
16) Write python code to find LCM of 2 numbers.
Source code:
a= int(input("enter a:"))
b= int(input("enter b:"))
ta=a
tb=b
while a!=b:
if a<b:
a=ta+a
else:
b=tb+b
print('LCM is:',a)
Output is:
enter a:12
enter b:15
LCM is: 60
17) An “Armstrong number” is a number that is equal to the sum of the nth
powers of its individual digits. For example, 371 is an Armstrong number where
it has 3 digits and 33+73+13 = 371
Python Program to Check Armstrong Number
Source code:
num = int(input("enter a number: "))
length = len(str(num))
sum = 0
temp = num
while (temp != 0):
sum = sum + ((temp % 10) ** length)
temp = temp // 10
if sum == num:
print("armstrong number")
else:
print("not armstrong number")
Output is:
enter a number: 371
amstrong number
18) Program to find simple interest. Si=(p*n*r)/100
Source code:
principle=float(input("Enter the principle amount:"))
time=int(input("Enter the time(years):"))
rate=float(input("Enter the rate:"))
simple_interest=(principle*time*rate)/100
print("The simple interest is:",simple_interest)
Output is:
Enter the principle amount:200
Enter the time(years):5
Enter the rate:5.0
The simple interest is: 50.0
19) Write python program to check whether number is perfect or not.
e.g. perfect numbers are : 6,28 etc.
Divisors of 6 are 1,2,3 and if addition of divisors is equal to that number
then that number is perfect number
Source code:
n=int(input('Enter no:'))
s=0
t=n
for i in range (1,n-1):
if n%i==0:
s=s+i
if s==t:
print('number is perfect')
else:
print('number is not perfect')
Output is:
Enter no:6
number is perfect
20) Write a program to check whether number is palindrome or not.
Source code:
n = int(input("Enter any number: "))
a=0
t=n
while(n>0):
r=n%10
a=r+(a*10)
n=n//10
if a==t:
print('no. is palindrome')
else:
print('no. is not palindrome')
Output is:
Enter any number: 101
no. is palindrome
21) Program to check whether number is prime number or not.
Source code:
number = int (input("Enter any number: "))
if number > 1:
for i in range (2, number):
if (number % i) == 0:
print (number, "is not a prime number")
break
else:
print (number, "is a prime number")
else:
print (number, "is not a prime number")
Output is:
Enter any number: 1
1 is not a prime number
22) Program to find prime numbers between 2 to 50 using nested for loops.
Source code:
num = 2
for i in range(2, 50):
j= 2
while ( j <= (i/2)):
if (i % j == 0):
break
j += 1
if ( j > i/j) :
print ( i, "is a prime number")
print ("Bye Bye!!")
Output:
2 is a prime number
3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number
Bye Bye!!
23) Write python code to check whether person is eligible for vote or not.
Ans:
age = int(input("Enter your age "))
if age >= 18: # use ‘:’ to indicate end of condition.
print("Eligible to vote")
else:
print("Not Eligible to vote")
24) Write python code to Check whether a number is positive,
negative, or zero.
Ans:
number = int(input("Enter a number: ")
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")
25) Write python code to Print even numbers in the given sequence
Ans:
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,'is an even Number')
Output:
2 is an even Number
4 is an even Number
6 is an even Number
8 is an even Number
10 is an even Number
26) Write python code to print the multiples of 10 for numbers in a given
range.
Ans:
#Print multiples of 10 for numbers in a range
for num in range(5):
if num > 0:
print(num * 10)
Output:
10
20
30
40
27) Write a program to find the average of 3 numbers
Source code:
a=int(input('Enter no1:'))
b=int(input('Enter no2:'))
c=int(input('Enter no3:'))
t=a+b+c
print('Average is:',t/3)
Output is:
Enter no1:1
Enter no2:1
Enter no3:1
Average is: 1.0
28) Write a program to print output
1
1 2
1 2 3
Source code:
i=1
while(i<=3):
j=1
while(j<=i):
print(j, end=" ") # inner while loop
j=j+1
print("\n")
i=i+1
29) Write a program to print output
1
22
333
4444
55555
Source code:
for num in range(6):
for i in range(num):
print (num, end=" ")
print("\n")
30) Write Python Program to find the factors of a number x
Source code:
x= int(input(―enter no ―)
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
Output is:
enter no ―20
The factors of 20 are:
1
2
4
5
10
20
31) Write a program to create a simple calculator performing only four
basic operations.
Source Code:
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program terminated")
else:
result = val1/val2
else:
print("Wrong input,program terminated")
print("The result is ",result)
Output:
Enter value 1: 84
Enter value 2: 4
Enter any one of the operator (+,-,*,/): /
The result is 21.0
32) Write python code to print output
12345
1234
123
12
1
Source code:
i=6
while i >= 0:
for j in range (1, i):
print(j, end=' ')
print('\n')
i=i-1
33) Find the sum of all the positive numbers entered by the user. As soon
as the user enters a negative number, stop taking in any further input
from the user and display the sum .
Source Code:
entry = 0
sum1 = 0
print("Enter numbers to find their sum, negative number ends the loop:")
while True:
entry = int(input())
if (entry < 0):
break
sum1 += entry
print("Sum =", sum1)
Output:
Enter numbers to find their sum, negative number ends the loop:
345
-1
Sum = 12