O Level Python Practical Solutions (M3-R5)

Practice exam questions, execute code live, and track your overall progress!

๐Ÿ“š Practical Completion Progress 0 / 4 Practiced (0%)
Practical #1EasyWeightage: 15 Marks

Write a Python program to check if a number is Prime or Not.

Problem Statement: Take an integer input and determine whether it is a prime number or not.
โฑ Practice Timer (Recommended: 10 mins)
00:00
Solution Code:
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

num = 29
if is_prime(num):
    print(f"{num} is a Prime Number!")
else:
    print(f"{num} is Not a Prime Number.")
๐Ÿ’ก How this code worksโž•
Looping up to โˆšn ensures optimization. If no factor is found, the number is prime.
Console Output
Practical #2MediumWeightage: 15 Marks

Write a Python program to print Fibonacci Series up to N terms.

Problem Statement: Display the Fibonacci sequence up to a specified number of terms.
โฑ Practice Timer (Recommended: 10 mins)
00:00
Solution Code:
n_terms = 10
n1, n2 = 0, 1
count = 0

if n_terms <= 0:
   print("Please enter a positive integer")
elif n_terms == 1:
   print("Fibonacci sequence:", n1)
else:
   print("Fibonacci sequence:")
   while count < n_terms:
       print(n1, end="  ")
       nth = n1 + n2
       n1 = n2
       n2 = nth
       count += 1
print()
๐Ÿ’ก How this code worksโž•
Uses two pointers n1 and n2 to track and compute subsequent sequence terms.
Console Output
Practical #3EasyWeightage: 10 Marks

Write a Python program to check whether a string is Palindrome or Not.

Problem Statement: Verify if an input string matches its reversed form.
โฑ Practice Timer (Recommended: 5 mins)
00:00
Solution Code:
def is_palindrome(s):
    rev_s = s[::-1]
    return s.lower() == rev_s.lower()

my_str = "Madam"
if is_palindrome(my_str):
    print(f"'{my_str}' is a Palindrome!")
else:
    print(f"'{my_str}' is Not a Palindrome.")
๐Ÿ’ก How this code worksโž•
String slicing [::-1] reverses string order efficiently.
Console Output
Practical #4MediumWeightage: 15 Marks

Write a Python program to find Factorial of a number using Recursion.

Problem Statement: Calculate the factorial of a positive integer using recursive functions.
โฑ Practice Timer (Recommended: 10 mins)
00:00
Solution Code:
def factorial(n):
    if n == 1 or n == 0:
        return 1
    else:
        return n * factorial(n - 1)

num = 5
print(f"The factorial of {num} is {factorial(num)}")
๐Ÿ’ก How this code worksโž•
Function calls itself recursively until n == 0 or 1.
Console Output

Create Account