Python Programming Coding Questions
Practice 70 most asked Python programming coding interview questions with solutions - Easy, Medium, and Hard levels.
1. Print "Hello World"
EasyOutput: Hello World
print("Hello World")
2. Add two numbers
Easy
Input: 10
20
Output: 30
a = int(input())
b = int(input())
print(a + b)
3. Find maximum of two numbers
Easy
Input: 10
20
Output: 20
a = int(input())
b = int(input())
print(max(a, b))
4. Check if number is even or odd
EasyInput: 7 Output: Odd
n = int(input())
if n % 2 == 0:
print("Even")
else:
print("Odd")
5. Check if number is positive, negative or zero
EasyInput: -45 Output: Negative
n = int(input())
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
6. Find factorial of a number
EasyInput: 7 Output: 5040
n = int(input())
fact = 1
for i in range(1, n + 1):
fact *= i
print(fact)
7. Print multiplication table
EasyInput: 7 Output: 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
n = int(input())
for i in range(1, 11):
print(f"{n} x {i} = {n * i}")
8. Print Fibonacci series
EasyInput: 10 Output: 0 1 1 2 3 5 8 13 21 34
n = int(input())
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a + b
9. Check if a number is prime
EasyInput: 29 Output: Prime
n = int(input())
if n <= 1:
print("Not Prime")
else:
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
10. Sum of digits of a number
EasyInput: 456 Output: 15
n = int(input())
sum_digits = 0
while n > 0:
sum_digits += n % 10
n //= 10
print(sum_digits)
11. Reverse a number
EasyInput: 1234 Output: 4321
n = int(input())
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
print(rev)
12. Check palindrome number
EasyInput: 1221 Output: Palindrome
n = int(input())
temp = n
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")
13. Count number of digits
EasyInput: 90876 Output: 5
n = int(input())
count = 0
while n > 0:
count += 1
n //= 10
print(count)
14. Find sum of natural numbers
EasyInput: 10 Output: 55
n = int(input())
sum_n = n * (n + 1) // 2
print(sum_n)
15. Find largest of three numbers
EasyInput: 5 12 9 Output: 12
a = int(input())
b = int(input())
c = int(input())
print(max(a, b, c))
16. Swap two numbers
EasyInput: 3 7 Output: 7 3
a = int(input())
b = int(input())
a, b = b, a
print(a, b)
17. Find area of circle
EasyInput: 7 Output: 153.93804002589985
import math
r = float(input())
area = math.pi * r * r
print(area)
18. Convert Celsius to Fahrenheit
EasyInput: 25 Output: 77.0
celsius = float(input())
fahrenheit = (celsius * 9/5) + 32
print(fahrenheit)
19. Check leap year
EasyInput: 2024 Output: Leap Year
year = int(input())
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not Leap Year")
20. Find simple interest
EasyInput: 1000 5 2 Output: 100.0
p = float(input())
r = float(input())
t = float(input())
si = (p * r * t) / 100
print(si)
21. Find compound interest
EasyInput: 1000 5 2 Output: 102.5
p = float(input())
r = float(input())
t = float(input())
ci = p * (1 + r/100)**t - p
print(ci)
22. Check Armstrong number
EasyInput: 153 Output: Armstrong
n = int(input())
temp = n
sum_arm = 0
digits = len(str(n))
while n > 0:
sum_arm += (n % 10) ** digits
n //= 10
if temp == sum_arm:
print("Armstrong")
else:
print("Not Armstrong")
23. Print all prime numbers in a range
EasyInput: 10 30 Output: 11 13 17 19 23 29
start = int(input())
end = int(input())
for num in range(start, end + 1):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
break
else:
print(num, end=" ")
24. Find GCD of two numbers
EasyInput: 24 36 Output: 12
import math
a = int(input())
b = int(input())
print(math.gcd(a, b))
25. Find LCM of two numbers
EasyInput: 12 18 Output: 36
import math
a = int(input())
b = int(input())
lcm = (a * b) // math.gcd(a, b)
print(lcm)
26. Check vowel or consonant
EasyInput: A Output: Vowel
ch = input().lower()
if ch in 'aeiou':
print("Vowel")
else:
print("Consonant")
27. Count vowels in a string
EasyInput: education Output: 5
s = input().lower()
count = sum(1 for ch in s if ch in 'aeiou')
print(count)
28. Reverse a string
EasyInput: Hello Output: olleH
s = input()
print(s[::-1])
29. Check palindrome string
EasyInput: madam Output: palindrome
s = input()
if s == s[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
30. Find length of string without using len()
EasyInput: hello Output: 5
s = input()
count = 0
for ch in s:
count += 1
print(count)
31. Find sum of list elements
MediumInput: 1 2 3 4 5 Output: 15
arr = list(map(int, input().split()))
print(sum(arr))
32. Find largest element in list
MediumInput: 4 9 2 7 5 Output: 9
arr = list(map(int, input().split()))
print(max(arr))
33. Find smallest element in list
MediumInput: 4 9 2 7 5 Output: 2
arr = list(map(int, input().split()))
print(min(arr))
34. Sort a list in ascending order
MediumInput: 5 1 4 2 3 Output: [1, 2, 3, 4, 5]
arr = list(map(int, input().split()))
arr.sort()
print(arr)
35. Linear search in list
MediumInput: 10 20 30 40 50 30 Output: Found at index 2
arr = list(map(int, input().split()))
key = int(input())
if key in arr:
print(f"Found at index {arr.index(key)}")
else:
print("Not Found")
36. Binary search in sorted list
MediumInput: 1 3 5 7 9 11 7 Output: Found at index 3
arr = list(map(int, input().split()))
key = int(input())
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == key:
print(f"Found at index {mid}")
break
elif arr[mid] < key:
left = mid + 1
else:
right = mid - 1
else:
print("Not Found")
37. Remove duplicates from list
MediumInput: 1 2 2 3 4 4 5 Output: [1, 2, 3, 4, 5]
arr = list(map(int, input().split()))
unique = list(set(arr))
print(unique)
38. Find frequency of elements in list
Medium
Input: 1 2 2 3 3 3 4
Output: {1: 1, 2: 2, 3: 3, 4: 1}
arr = list(map(int, input().split()))
freq = {}
for num in arr:
freq[num] = freq.get(num, 0) + 1
print(freq)
39. Merge two lists
MediumInput: 1 2 3 4 5 6 Output: [1, 2, 3, 4, 5, 6]
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
merged = arr1 + arr2
print(merged)
40. Find second largest element
MediumInput: 10 20 20 30 40 Output: 30
arr = list(map(int, input().split()))
arr = list(set(arr))
arr.sort()
if len(arr) >= 2:
print(arr[-2])
else:
print("Not enough elements")
41. Rotate list by k positions
MediumInput: 1 2 3 4 5 2 Output: [3, 4, 5, 1, 2]
arr = list(map(int, input().split()))
k = int(input())
k = k % len(arr)
rotated = arr[k:] + arr[:k]
print(rotated)
42. Find missing number in list (1 to n)
MediumInput: 1 2 4 5 6 Output: 3
arr = list(map(int, input().split()))
n = len(arr) + 1
expected_sum = n * (n + 1) // 2
actual_sum = sum(arr)
print(expected_sum - actual_sum)
43. Check if list is sorted
MediumInput: 1 2 3 4 5 Output: Sorted
arr = list(map(int, input().split()))
if arr == sorted(arr):
print("Sorted")
else:
print("Not Sorted")
44. Find pairs with given sum
MediumInput: 1 2 3 4 5 5 Output: [(1, 4), (2, 3)]
arr = list(map(int, input().split()))
target = int(input())
pairs = []
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
if arr[i] + arr[j] == target:
pairs.append((arr[i], arr[j]))
print(pairs)
45. Count words in a string
MediumInput: Hello world from Python Output: 4
s = input()
words = s.split()
print(len(words))
46. Remove spaces from string
MediumInput: Hello world Python Output: HelloworldPython
s = input()
result = s.replace(" ", "")
print(result)
47. Check if two strings are anagrams
MediumInput: listen silent Output: Anagrams
s1 = input()
s2 = input()
if sorted(s1) == sorted(s2):
print("Anagrams")
else:
print("Not Anagrams")
48. Find first non-repeating character
MediumInput: swiss Output: w
s = input()
for ch in s:
if s.count(ch) == 1:
print(ch)
break
else:
print("None")
49. Count uppercase and lowercase letters
MediumInput: Hello World Output: Uppercase: 2, Lowercase: 8
s = input()
upper = sum(1 for ch in s if ch.isupper())
lower = sum(1 for ch in s if ch.islower())
print(f"Uppercase: {upper}, Lowercase: {lower}")
50. Toggle case of string
MediumInput: Hello World Output: hELLO wORLD
s = input()
print(s.swapcase())
51. Find all substrings of a string
MediumInput: abc Output: ['a', 'ab', 'abc', 'b', 'bc', 'c']
s = input()
substrings = []
for i in range(len(s)):
for j in range(i + 1, len(s) + 1):
substrings.append(s[i:j])
print(substrings)
52. Replace substring in string
MediumInput: I like apples apples oranges Output: I like oranges
s = input()
old = input()
new = input()
print(s.replace(old, new))
53. Check if string contains only digits
MediumInput: 12345 Output: 12345
s = input()
if s.isdigit():
print("Only digits")
else:
print("Not only digits")
54. Find longest word in string
MediumInput: I love programming in Python Output: programming
s = input()
words = s.split()
longest = max(words, key=len)
print(longest)
55. Remove punctuation from string
MediumInput: Hello, world! Welcome to Python. Output: Hello world Welcome to Python
import string
s = input()
result = s.translate(str.maketrans('', '', string.punctuation))
print(result)
56. Matrix addition
HardInput: 2 3 1 2 3 4 5 6 6 5 4 3 2 1 Output: [7, 7, 7] [7, 7, 7]
rows = int(input())
cols = int(input())
mat1 = [list(map(int, input().split())) for _ in range(rows)]
mat2 = [list(map(int, input().split())) for _ in range(rows)]
result = [[mat1[i][j] + mat2[i][j] for j in range(cols)] for i in range(rows)]
for row in result:
print(row)
57. Matrix multiplication
HardInput: 2 3 1 2 3 4 5 6 3 2 7 8 9 10 11 12 Output: [58, 64] [139, 154]
r1, c1 = map(int, input().split())
mat1 = [list(map(int, input().split())) for _ in range(r1)]
r2, c2 = map(int, input().split())
mat2 = [list(map(int, input().split())) for _ in range(r2)]
result = [[sum(mat1[i][k] * mat2[k][j] for k in range(c1))
for j in range(c2)] for i in range(r1)]
for row in result:
print(row)
58. Transpose of matrix
HardInput: 2 3 1 2 3 4 5 6 Output: [1, 4] [2, 5] [3, 6]
rows = int(input())
cols = int(input())
mat = [list(map(int, input().split())) for _ in range(rows)]
transpose = [[mat[j][i] for j in range(rows)] for i in range(cols)]
for row in transpose:
print(row)
59. Generate Pascal's triangle
HardInput: 5 Output: [1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1]
n = int(input())
triangle = []
for i in range(n):
row = [1] * (i + 1)
for j in range(1, i):
row[j] = triangle[i-1][j-1] + triangle[i-1][j]
triangle.append(row)
print(row)
60. Find all permutations of string
HardInput: abc Output: ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
from itertools import permutations
s = input()
perms = [''.join(p) for p in permutations(s)]
print(perms)
61. Find all combinations of list
HardInput: 1 2 3 4 2 Output: [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
from itertools import combinations
arr = list(map(int, input().split()))
r = int(input())
combs = list(combinations(arr, r))
print(combs)
62. Implement stack using list
HardOutput: 20
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop() if self.items else None
def peek(self):
return self.items[-1] if self.items else None
def is_empty(self):
return len(self.items) == 0
s = Stack()
s.push(10)
s.push(20)
print(s.pop())
63. Implement queue using list
HardOutput: 10
from collections import deque
class Queue:
def __init__(self):
self.items = deque()
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
return self.items.popleft() if self.items else None
def is_empty(self):
return len(self.items) == 0
q = Queue()
q.enqueue(10)
q.enqueue(20)
print(q.dequeue())
64. Find longest common subsequence
HardInput: AGGTAB GXTXAYB Output: 4
def lcs(s1, s2):
m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[m][n]
s1 = input()
s2 = input()
print(lcs(s1, s2))
65. Solve Tower of Hanoi
HardInput: 2 Output: Move disk 1 from A to B Move disk 2 from A to C Move disk 1 from B to C
def tower_of_hanoi(n, source, auxiliary, destination):
if n == 1:
print(f"Move disk 1 from {source} to {destination}")
return
tower_of_hanoi(n-1, source, destination, auxiliary)
print(f"Move disk {n} from {source} to {destination}")
tower_of_hanoi(n-1, auxiliary, source, destination)
n = int(input())
tower_of_hanoi(n, 'A', 'B', 'C')
66. Find nth Fibonacci using memoization
HardInput: 7 Output: 13
def fibonacci(n, memo={}):
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo)
return memo[n]
n = int(input())
print(fibonacci(n))
67. Merge two sorted lists
HardInput: 1 3 5 2 4 6 Output: [1, 2, 3, 4, 5, 6]
def merge_sorted(arr1, arr2):
result = []
i, j = 0, 0
while i < len(arr1) and j < len(arr2):
if arr1[i] < arr2[j]:
result.append(arr1[i])
i += 1
else:
result.append(arr2[j])
j += 1
result.extend(arr1[i:])
result.extend(arr2[j:])
return result
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
print(merge_sorted(arr1, arr2))
68. Quick Sort implementation
HardInput: 5 3 8 4 2 Output: [2, 3, 4, 5, 8]
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
arr = list(map(int, input().split()))
print(quick_sort(arr))
69. Merge Sort implementation
HardInput: 9 4 7 1 5 Output: [1, 4, 5, 7, 9]
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
arr = list(map(int, input().split()))
print(merge_sort(arr))
70. Find all prime factors of a number
HardInput: 60 Output: [2, 2, 3, 5]
def prime_factors(n):
factors = []
d = 2
while d * d <= n:
while n % d == 0:
factors.append(d)
n //= d
d += 1
if n > 1:
factors.append(n)
return factors
n = int(input())
print(prime_factors(n))