Category: python
python coding questions
Published on 05 Mar 2026
Explanation
Write a Python program to reverse a string
using slicing.
Code:
def reverse_string(s):
return s[::-1]
print(reverse_string("Python"))
Explanation
Write a Python program to calculate the
factorial of a number using a loop.
Code:
def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i
return fact
print(factorial(5))
Explanation
Write a Python program to check whether a
given number is prime.
Code:
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
print(is_prime(7))
Explanation
Write a Python program to print the
Fibonacci series up to n terms.
Code:
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(10)
Explanation
Write a Python program to count the
frequency of each character in a string
using a dictionary.
Code:
def count_characters(s):
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
return freq
print(count_characters("hello"))