Are you looking for some Python programs for practice? Look no further! Here are some engaging Python programs to sharpen your skills. To execute below programs you can use Online Python – IDE, Editor,Compiler, Interpreter.
Python Programs for Practice 1: Input a number and check zero or positive or negative.
n = float(input("Enter a number:"))
if n > 0:
print("Positive")
elif n == 0:
print("Zero")
else:
print("Negative")
Output:
Enter a number:
1
Positive
Python Programs for Practice 2: Input 2 numbers and find smaller and larger
a = int(input("First number:"))
b = int(input("Second number:"))
if a < b:
print(a, "is smaller")
print(b, "is larger")
else:
print(b, "is smaller")
print(a, "is larger")
Output:
First number:
1
Second number:
2
1 is smaller
2 is larger
Python Programs for Practice 3: Input 2 numbers and check divisibility of first number by second
a = int(input("First number:"))
b = int(input("Second number:"))
if a % b == 0:
print(a, "is divisible by", b)
else:
print(a, "is not divisible by", b)
Output:
First number:
4
Second number:
2
4 is divisible by 2
Python Programs for Practice 4: Find largest of 3 objects (Checks ASCII)
a = input('first number:')
b = input('second number:')
c = input('third number:')
if a > b:
if a > c:
l = a
else:
l = c
elif b > c:
l = b
else:
l = c
print('Largest number is', l)
Output:
first number:
1
second number:
2
third number:
3
Largest number is 3
Python Programs for Practice 5: Largest of float object, using logical operator
n1 = float(input("Enter first number:"))
n2 = float(input("Enter second number:"))
n3 = float(input("Enter third number:"))
if (n1 >= n2) and (n1 >= n3):
l = n1
elif (n2 >= n1) and (n2 >= n3):
l = n2
else:
l = n3
print("The largest number is", l)
Output:
Enter first number:
1
Enter second number:
2
Enter third number:
3
The largest number is 3.0
Python Programs for Practice 6: Input marks out of 100 and assign grade as A(90-100), B(70-89), C(50-69), F(0-49)
a = int(input("Enter the marks:"))
if a >= 90:
gr = 'A'
elif a > 70:
gr = 'B'
elif a >= 50:
gr = 'C'
else:
gr = 'F'
print("Grade is", gr)
Output:
Enter the marks:
95
Grade is A
Python Programs for Practice 7: Input a number if it is 2 digit and less than 50 then print “GOOD” otherwise print “BETTER”. If it is 3 digit and less than 500 then print “Best” otherwise print “Excellent”. But if it is less than or equal to zero then print “BAD”
Analysis:
Condition 1: Check for two digit number
Sub-Condition 2: a) range <50 —>Good
b) range >=50 —>Better
Condition 2: Check for three digit number
Sub-Condition 2: a) range <500 —>Best
b) range>=500 —> Excellent
Otherwise —> Bad
n = int(input('Give a number:'))
if n > 9 and n < 50:
print('Good')
elif n >= 50 and n < 100:
print('Better')
elif n >= 100 and n < 500:
print('Best')
elif n >= 500 and n < 1000:
print('Excellent')
else:
print('Bad')
Output:
Give a number:
512
Excellent
Python Programs for Practice 8: You have 100 rupees in your purse and you have to purchase item A. If its price is less than rupees fifteen then purchase 6 units but if price is between rupees 15-40 then purchase only 2 units. Find the remaining amount in your pocket.
Analysis:
Price | Units | Remaining Amount |
p<=15 | 6 | 100 – price*6 |
15<p<=40 | 2 | 100 – price*2 |
p = int(input('Give price of item:'))
b = 100
if 0 < p <= 15:
b = 100 - p * 6
elif 15 < p <= 40:
b = 100 - p * 2
else:
print('Do not purchase')
print('Balance amount=', b)
Output:
Give price of item:
25
Balance amount= 50
Python Programs for Practice 9: Input a number and compute it’s factorial.
n = int(input("Enter a number:"))
f = 1
if n < 0:
print("Factorial does not exist for negative numbers")
elif n == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, n + 1):
f = f * i
print("The factorial is", f)
Output:
Enter a number:
7
The factorial is 5040
OR
n=int(input('Enter a number:'))
fact=1
for m in range(1,n+1):
fact=fact*m
print("Factorial is",fact)
Output:
Enter a number:
7
Factorial is 1
Factorial is 2
Factorial is 6
Factorial is 24
Factorial is 120
Factorial is 720
Factorial is 5040
As a beginner, these were good programs
Thank you so much