20 Python Basic Questions with Answers
1. Print Hello World
Q: Write a Python program to print "Hello, World!".
A:
print("Hello, World!")
2. Add Two Numbers
Q: Write a program to add two numbers: 5 + 10.
A:
a=5
b = 10
print(a + b)
3. Find the Square of a Number
Q: Write a program to find the square of 7.
A:
num = 7
print(num * num)
4. Find the Cube of a Number
Q: Write a program to find the cube of 4.
A:
num = 4
print(num ** 3)
5. Swap Two Numbers
Q: Swap the values of x = 3 and y = 7.
A:
x=3
y=7
x, y = y, x
print(x, y)
6. Check Even or Odd
Q: Write a program to check if 12 is even or odd.
A:
num = 12
if num % 2 == 0:
print("Even")
else:
print("Odd")
7. Largest of Two Numbers
Q: Find the largest number between 8 and 15.
A:
a=8
b = 15
print(max(a, b))
8. Convert Celsius to Fahrenheit
Q: Convert 25°C to Fahrenheit.
A:
c = 25
f = (c * 9/5) + 32
print(f)
9. Simple Interest
Q: Write a program to calculate Simple Interest where P=1000, R=5, T=2.
A:
P = 1000
R=5
T=2
SI = (P * R * T) / 100
print(SI)
10. Reverse a String
Q: Reverse the string "Python".
A:
s = "Python"
print(s[::-1])
11. Count Characters in String
Q: Find the length of "Hello".
A:
s = "Hello"
print(len(s))
12. Sum of First 10 Natural Numbers
Q: Find the sum of numbers from 1 to 10.
A:
total = 0
for i in range(1, 11):
total += i
print(total)
13. Factorial of a Number
Q: Find factorial of 5.
A:
num = 5
fact = 1
for i in range(1, num + 1):
fact *= i
print(fact)
14. Multiplication Table
Q: Print the multiplication table of 6.
A:
for i in range(1, 11):
print(6, "x", i, "=", 6*i)
15. Fibonacci Series
Q: Print first 5 terms of Fibonacci series.
A:
a, b = 0, 1
for i in range(5):
print(a, end=" ")
a, b = b, a + b
16. Check Prime Number
Q: Check if 7 is prime or not.
A:
num = 7
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
print("Prime" if is_prime else "Not Prime")
17. Find Maximum in List
Q: Find the largest number in [4, 9, 1, 7].
A:
numbers = [4, 9, 1, 7]
print(max(numbers))
18. Sum of List Elements
Q: Find the sum of [3, 5, 2, 8].
A:
numbers = [3, 5, 2, 8]
print(sum(numbers))
19. Find Vowels in String
Q: Count vowels in "education".
A:
s = "education"
vowels = "aeiou"
count = 0
for ch in s:
if ch in vowels:
count += 1
print(count)
20. Simple Calculator
Q: Write a simple calculator for a=8, b=2.
A:
a=8
b=2
print("Sum:", a+b)
print("Difference:", a-b)
print("Product:", a*b)
print("Division:", a/b)