Class 9 – Python Programs Question & Answers (AI)
1. Write a program to print 'Hello World'.
print("Hello World")
2. Write a program to add two numbers.
a = 10
b = 20
print("Sum =", a + b)
3. Write a program to find even or odd number.
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")
4. Write a program to find the largest of two numbers.
a = 15
b = 9
if a > b:
print("A is largest")
else:
print("B is largest")
5. Write a program to print numbers from 1 to 10.
for i in range(1, 11):
print(i)
6. Write a program to find factorial of a number.
num = 5
fact = 1
for i in range(1, num+1):
fact *= i
print("Factorial =", fact)
7. Write a program to check prime number.
num = 7
flag = True
for i in range(2, num):
if num % i == 0:
flag = False
break
if flag:
print("Prime")
else:
print("Not Prime")
8. Write a program to print multiplication table.
num = 5
for i in range(1, 11):
print(num, "x", i, "=", num*i)
9. Write a program to reverse a string.
text = "Python"
print(text[::-1])
10. Write a program to count vowels in a string.
text = "education"
count = 0
for ch in text:
if ch in "aeiou":
count += 1
print("Vowels =", count)
11. Write a program to create a list and print it.
lst = [1,2,3,4,5]
print(lst)
12. Write a program to find sum of list elements.
lst = [1,2,3,4]
print("Sum =", sum(lst))
13. Write a program to create a dictionary.
student = {"name":"Ram", "age":14}
print(student)
14. Write a program to check palindrome string.
text = "madam"
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
15. Write a program to swap two numbers.
a = 5
b = 10
a, b = b, a
print(a, b)
16. Find the error in the following code:
for i in range(5)
print(i)
# Error: Missing colon (:) after range(5)
17. Find the error in the following code:
a = 10
if a = 5:
print("Five")
# Error: '=' should be '=='
18. Find the output:
x = [1,2,3]
[Link]([4,5])
print(len(x))
# Output: 4
19. Write a program to count digits in a number.
num = 12345
count = 0
while num > 0:
num //= 10
count += 1
print("Digits =", count)
20. Find the error:
print("Hello)
# Error: Missing closing quotation mark
21. Write a program to remove duplicates from list.
lst = [1,2,2,3,4,4]
unique = list(set(lst))
print(unique)
22. Find output:
a = "AI"
print(a * 3)
# Output: AIAIAI
23. Write a program to check Armstrong number (3 digit).
num = 153
temp = num
sum = 0
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if sum == num:
print("Armstrong")
else:
print("Not Armstrong")
24. Find the error:
list = [1,2,3]
print(list(5))
# Error: 'list' is used as variable name and cannot be called as function
25. Write a program to find minimum number in list.
lst = [5,2,9,1]
print("Minimum =", min(lst))