DELHI PUBLIC SCHOOL-BOPAL, AHMEDABAD
Subject: ARTIFICIAL INTELLIGENCE Session: 2026-2027
PYTHON PRACTICE PROGRAM
-----------------------------------------------------------------------------------------------------------------------
Programs using Conditional Statements: if…elif... else…
Program-1 #WAP to decide the person is eligible for vote or not.
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Program-2 #WAP to find given number is odd or even.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Program-3 #WAP to compare 2 numbers.
n1=int(input("Enter first number: "))
n2=int(input("Enter second number: "))
if n1>n2:
print(n1,"is greater than",n2)
elif n1<n2:
print(n1,"is less than",n2)
else:
print(n1,"is equal to",n2)
Program-4 #WAP to find the greatest of 3 numbers.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Greatest number is:", a)
elif b >= a and b >= c:
print("Greatest number is:", b)
else:
print("Greatest number is:", c)
Program-5 #WAP to calculate the electricity bill based on the number of units consumed, using the
following rate structure:
If the consumption is up to 100 units, the rate is Rs. 3 per unit.
If the consumption is between 101 and 200 units, the first 100 units are charged at Rs. 3 per unit, and
the remaining units (up to 200) are
charged at Rs. 5 per unit.
If the consumption exceeds 200 units, the first 100 units are charged at Rs. 3 per unit, the next 100 units
(101–200) at Rs. 5 per unit, and all
units above 200 are charged at Rs. 10 per unit.
units = int(input("Enter electricity units consumed: "))
if units <= 100:
bill = units * 3
elif units <= 200:
bill = (100 * 3) + (units - 100) * 5
else:
bill = (100 * 3) + (100 * 5) + (units - 200) * 10
print("Electricity bill: ₹", bill)
Program-6 #WAP to determine the type of triangle based on the lengths of its three sides. The program
should classify the triangle as one of the following:
Equilateral Triangle: All three sides are equal.
Isosceles Triangle: Any two sides are equal.
Scalene Triangle: All three sides are different.
a = int(input("Enter first side: "))
b = int(input("Enter second side: "))
c = int(input("Enter third side: "))
if a == b == c:
print("Equilateral triangle")
elif a == b or b == c or a == c:
print("Isosceles triangle")
else:
print("Scalene triangle")
Program-7 #WAP to check whether the given year is a leap year or not.
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")
Program-8 # WAP to create a menu driven program for the following arithmetic operations
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Select the appropriate operation: ")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice = int(input("Enter your choice (1/2/3/4/5): "))
if choice <0 or choice >4:
print("Invalid choice")
elif choice == 1:
print("Result:", a + b)
elif choice == 2:
print("Result:", a - b)
elif choice == 3:
print("Result:", a * b)
else:
print("Result:", a / b)
Program-9 #WAP to calculate the bonus earned by a salesperson based on their total sales amount,
using the following criteria:
If the sales amount is less than Rs. 50,000, no bonus is awarded.
If the sales amount is Rs. 50,000 or more but less than Rs. 1,00,000, the bonus is 5% of the sales amount.
If the sales amount is Rs. 1,00,000 or more but less than Rs. 2,00,000, the bonus is 10% of the sales
amount.
If the sales amount is Rs. 2,00,000 or more but less than Rs. 3,00,000, the bonus is 15% of the sales
amount.
If the sales amount is Rs. 3,00,000 or more, the bonus is 20% of the sales amount.
saleamt=float(input("Enter the sale amount: "))
if saleamt<50000:
print("No bonus")
elif saleamt>=50000 and saleamt<100000:
print("Bonus is",saleamt*5/100)
elif saleamt>=100000 and saleamt<200000:
print("Bonus is",saleamt*10/100)
elif saleamt>=200000 and saleamt<300000:
print("Bonus is",saleamt*15/100)
else:
print("Bonus is",saleamt*20/100)saleamt=float(input("Enter the sale amount: "))
if saleamt<50000:
print("No bonus")
elif saleamt>=50000 and saleamt<100000:
print("Bonus is",saleamt*5/100)
elif saleamt>=100000 and saleamt<200000:
print("Bonus is",saleamt*10/100)
elif saleamt>=200000 and saleamt<300000:
print("Bonus is",saleamt*15/100)
else:
print("Bonus is",saleamt*20/100)
Program-10 #WAP to verify the login & password. Assume Login is 'admin' and password is '1234'.
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin":
if password == "1234":
print("Login successful")
else:
print("Incorrect password")
else:
print("Invalid username")
Programs: Python loops- for loop & while loop
1. WAP to display first ‘n’ numbers using for loop.
n=int(input("Enter a number:-"))
for i in range(1,n+1):
print(i, end=" ")
2. WAP to display odd numbers below ‘n’ using for loop .
n=int(input("Enter a number:-"))
for i in range(1,n+1,2):
print(i, end=" ")
3. WAP to display first ‘n’ odd numbers using for loop .
n=int(input("How many odd numbers:-"))
no=1
for i in range(n):
print(no, end=" ")
no=no+2
4. WAP to display even numbers below ‘n’ using for loop.
n=int(input("Enter a number:-"))
for i in range(2,n+1,2):
print(i, end=" ")
5. WAP to display first ‘n’ even numbers using for loop .
n=int(input("How many even numbers:-"))
no=2
for i in range(n):
print(no, end=" ")
no=no+2
6. WAP to display the sum of first ‘n’ numbers using for loop .
n=int(input("Enter a number:-"))
sum=0
for i in range(1,n+1):
sum+=i
print("Sum of first ", n, "numbers is ", sum)
7. WAP to display the factorial of ‘n’ using for loop.
n=int(input("Enter a number:-"))
fact=1
for i in range(1,n+1):
fact*=i
print("Factorial of ", n, "is ", fact)
8. WAP to display all numbers in a range using while loop.
lowerlimit=int(input("Lower Limit of a range:-"))
upperlimit=int(input("Upper Limit of a range:-"))
i=lowerlimit
while i<=upperlimit:
print(i, end=" ")
i+=1
9. WAP to display numbers from n to 1 using for loop & while loop.
n=int(input("Enter a number:-"))
#Using for loop
for i in range(n,0,-1):
print(i,end=" ")
print()
#Using while loop
i=n
while i>0:
print(i,end=" ")
i-=1
10. WAP to reverse a numbers using while loop.
n=int(input("Enter a number:-"))
n1=n
rev=0
while n1 != 0:
lastdigit=n1%10
rev=rev*10+lastdigit
n1=n1//10
print("Reverse of", n, "is", rev)
11. WAP to find the sum of digits of a number using while loop.
n=int(input("Enter a number:-"))
n1=n
sum=0
while n1 != 0:
lastdigit=n1%10
sum=sum+lastdigit
n1=n1//10
print("Sum of digits of ", n, "is", sum)
12. WAP to display the multiplication table of ‘n’ for ‘m’ rows using while loop.
n=int(input("Which Number's multiplication table:-"))
m=int(input("No. of rows:-"))
i=1
while i<=m:
print(i,"x",n,"=",i*n)
i+=1
13. WAP to find whether the given number is a prime or not using while loop.
n=int(input("Enter a Number:-"))
i=2
while i<n:
if n%i==0:
print(n,"is not a prime number")
break
i=i+1
else:
print(n, "is a palindrome")
14. WAP to find whether the given number is a perfect number or not using while loop.
n=int(input("Enter a Number:-"))
sum=0
i=1
while i<n:
if n%i==0:
sum+=i
i=i+1
if sum==n:
print(n, "is a perfect number")
else:
print(n, "is not a perfect number")
15. WAP to find whether the given number is an Armstrong number or not using while loop.
n=int(input("Enter a number:-"))
n1=n
sum=0
while n1 != 0:
lastdigit=n1%10
sum=sum+lastdigit**3
n1=n1//10
if sum==n:
print(n, "is armstrong number")
else:
print(n, "is not an armstrong number")
16. WAP to display first n terms of Fibonacci Series.
Programs: Python Strings
#1. WAP to display each character of the string one by one.
s1=input(“Enter a string-”)
for i in s1:
print(i)
#alternate method
L=len(s1)
for i in range(L):
print(s1[i])
#2. WAP to display each character of a string in a line
s1=input(“Enter a string-”)
for i in s1:
print(i, end= " ")
#3. WAP to find the length of a string
s1=input(“Enter a string-”)
count=0
for i in s1:
count=count+1
print("Length of the string=",count)
#4. WAP to reverse a string
s1=input(“Enter a string-”)
rev= ""
for i in s1:
rev=i+rev
print("Reverse of the string=",rev)
# alternate method
rev=s1[::-1]
print("Reverse of the string using slicing=",rev)
#5. WAP to count alphabets, digits & special character in a string
s1=input(“Enter a string-”)
a,d,s=0,0,0
for i in s1:
if (i>= "A" and i<= "Z") or (i>= "a" and i<= "z"):
a+=1
elif i>= "0" and i<= "9":
d+=1
else:
s+=1
print("No. of alphabets=",a)
print("No. of digits=",d)
print("No. of special characters=",s)
#6. WAP to count lowercase, uppercase & spaces in a string
s1=input("Enter a string-”)
l,u,s=0,0,0
for i in s1:
if i>= "A" and i<= "Z":
u+=1
elif i>= "a" and i<= "z":
l+=1
elif i==" ":
s+=1
print("No. of uppercase=",u)
print("No. of lowercase=",l)
print("No. of spaces=",s)
#7. WAP to count vowels & consonants in a string
s1=input("Enter a string-")
v,c=0,0
for i in s1:
if i in "AEIOUaeiou":
v+=1
elif (i>= "A" and i<= "Z") or (i>= "a" and i<= "z"):
c+=1
print("No. of Vowels=",v)
print("No. of consonants=",c)
#8. WAP to convert all uppercase to lowercase to uppercase and
#keep rest of the characters as it is.
s1=input("Enter a string-")
new= ""
for i in s1:
if i>= "A" and i<= "Z":
new=new+chr(ord(i)+32)
elif i>= "a" and i<= "z":
new=new+chr(ord(i)-32)
else:
new=new+i
print("The original string is", s1)
print("New String is ",new)
#9. WAP to count the number of characters in a string
#without using string function
s1=input("Enter a string-”)
leng=0
for i in s1:
leng=leng+1
print("Length of the string=",leng)
#10. WAP to convert the string to lowercase letter
#without using string function
s1=input("Enter a string-”)
s2= ""
for i in s1:
if i>= "A" and i<= "Z":
s2=s2+chr(ord(i)+32)
else:
s2=s2+i
print("string in lowercase-", s2)
#11. WAP to replace all digits with “@” and all special characters with “#”
# keep all the other characters as it is
s1=input("Enter a string-”)
s2= ""
for i in s1:
if i>= "0" and i<= "9":
s2=s2+"@"
elif (i>= "A" and i<= "Z") or (i>= "a" and i<= "z"):
print("")
else:
s2=s2+ "#"
print("New String=", s2)
#12. WAP to check whether the given string is a palindrome or not
s1=input("Enter a string-")
leng=len(s1)-1
for i in s1:
if i!=s1[leng]:
print(s1, "is not a palindrome")
break
leng=leng-1
else:
print(s1, "is a palindrome")
#alternate method
#by finding the reverse and the check whether both strings are same or not
rev= ""
for i in s1:
rev=i+rev
if s1==rev:
print(s1, "is a palindrome")
else:
print(s1, "is not a palindrome")
Programs: Python list
#1. WAP to create a list from the user.
L1=[]
n=int(input(“Enter no. of items:”))
for i in range(n):
item=input("Enter an item:")
[Link](item)
print(L1)
#2. WAP to get an element from index position
L1=[16, 21, 45,10,19]
ind=int(input("Index position-"))
print(L1[ind])
#3. WAP to reverse a list without using functions or slicing
L1=[16, 21, 45, 10, 19]
print("Oringinal List=",L1)
leng=len(L1)-1
x=int(len(L1)/2)
for i in range(x):
temp=L1[i]
L1[i]=L1[leng]
L1[leng]=temp
leng=leng-1
print("Reversed List=",L1)
#4. WAP to find an item from a list and replace with user entered item
L1=[16, 21, 45, 10, 19]
print("Original List=",L1)
item1=int(input("Enter item to be replaced?"))
item2=int(input("With which item?"))
index=0
for i in range(len(L1)):
if L1[i]==item1:
L1[i]=item2
print("List after change=",L1)
break
else:
print("Item is not present in the list")
#5. WAP to find the largest number in a list
L1=[16, 21, 45, 10, 19]
print("Original List=",L1)
Large=L1[0]
for i in L1:
if i>Large:
Large=i
print("Largest Item=",Large)
#6. WAP to find the sum of all the numbers in a list
L1=[16, 21, 45, 10, 19]
print("Original List=",L1)
sum=0
for i in L1:
sum=sum+i
print("Sum of all numbers in a list=",sum)