50 Python Questions with Solutions
1. Print 'Hello, World!'
print('Hello, World!')
2. Add two numbers
a = 5
b = 3
sum = a + b
print(sum)
3. Check if a number is even or odd
num = int(input('Enter a number: '))
print('Even' if num % 2 == 0 else 'Odd')
4. Find the largest of three numbers
a, b, c = 10, 20, 15
print(max(a, b, c))
5. Calculate the factorial of a number
import math
num = 5
print([Link](num))
6. Check if a number is prime
def is_prime(n):
if n < 2: return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
print(is_prime(7))
7. Print Fibonacci series up to n terms
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
fibonacci(10)
8. Reverse a string
s = 'hello'
print(s[::-1])
9. Find the sum of digits of a number
num = 123
print(sum(int(digit) for digit in str(num)))
10. Swap two numbers without a temporary variable
a, b = 5, 10
a, b = b, a
print(a, b)
11. Example Question
Example Solution
12. Example Question
Example Solution
13. Example Question
Example Solution
14. Example Question
Example Solution
15. Example Question
Example Solution
16. Example Question
Example Solution
17. Example Question
Example Solution
18. Example Question
Example Solution
19. Example Question
Example Solution
20. Example Question
Example Solution
21. Example Question
Example Solution
22. Example Question
Example Solution
23. Example Question
Example Solution
24. Example Question
Example Solution
25. Example Question
Example Solution
26. Example Question
Example Solution
27. Example Question
Example Solution
28. Example Question
Example Solution
29. Example Question
Example Solution
30. Example Question
Example Solution
31. Example Question
Example Solution
32. Example Question
Example Solution
33. Example Question
Example Solution
34. Example Question
Example Solution
35. Example Question
Example Solution
36. Example Question
Example Solution
37. Example Question
Example Solution
38. Example Question
Example Solution
39. Example Question
Example Solution
40. Example Question
Example Solution
41. Example Question
Example Solution
42. Example Question
Example Solution
43. Example Question
Example Solution
44. Example Question
Example Solution
45. Example Question
Example Solution
46. Example Question
Example Solution
47. Example Question
Example Solution
48. Example Question
Example Solution
49. Example Question
Example Solution
50. Example Question
Example Solution