Python Revision – Coding Questions with Answers
Q1. x = 5; y = 2; print(x ** y, x // y, x % y)
Answer: 25 2 1
Q2. for i in range(1, 5): if i == 3: continue print(i, end=' ')
Answer: 1 2 4
Q3. for i in range(2, 6): for j in range(1, i): print(j, end='') print()
Answer: 1 12 123 1234
Q4. x = 10; if x > 0: print('Positive') elif x == 0: print('Zero') else: print('Negative')
Answer: Positive
Q5. a, b, c = 2, 5, 3; if a > b and a > c: print('A') elif b > c: print('B') else: print('C')
Answer: B
Find whether a number is even or odd.
n = 7
if n % 2 == 0:
print('Even')
else:
print('Odd')
Output: Odd
Check if a number is divisible by 5 and 11.
n = 55
if n % 5 == 0 and n % 11 == 0:
print('Divisible')
else:
print('Not Divisible')
Output: Divisible
Print the sum of first N natural numbers.
n = 5
s = n * (n + 1) // 2
print(s)
Output: 15
Display the multiplication table of a given number.
n = 3
for i in range(1, 11):
print(n, 'x', i, '=', n*i)
Output: Table of 3
Find the largest of three numbers.
a, b, c = 10, 20, 15
print(max(a, b, c))
Output: 20
Print all prime numbers between 1 and 50.
for n in range(2, 51):
for i in range(2, n):
if n % i == 0:
break
else:
print(n, end=' ')
Output: 2 3 5 ... 47
Calculate the factorial of a number.
n = 5
fact = 1
for i in range(1, n+1):
fact *= i
print(fact)
Output: 120
Count the number of vowels in a string.
s = 'hello world'
count = 0
for ch in s:
if ch in 'aeiou':
count += 1
print(count)
Output: 3
Display the pattern: * ** *** ****
for i in range(1, 5):
print('*'*i)
Output:
*
**
***
****
Check if a number is palindrome.
n = 121
if str(n) == str(n)[::-1]:
print('Palindrome')
else:
print('Not Palindrome')
Output: Palindrome