0% found this document useful (0 votes)
1 views6 pages

Here Are

The document provides 15 practice questions for a Python Level 2 syllabus, covering various topics such as basics, strings, control flow, functions, and file handling. Each question includes a brief description and a corresponding Python code solution. The questions range from simple tasks like checking odd/even numbers to more complex challenges like implementing a bank account class and performing binary search on a rotated list.

Uploaded by

Abdul Wasay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Here Are

The document provides 15 practice questions for a Python Level 2 syllabus, covering various topics such as basics, strings, control flow, functions, and file handling. Each question includes a brief description and a corresponding Python code solution. The questions range from simple tasks like checking odd/even numbers to more complex challenges like implementing a bank account class and performing binary search on a rotated list.

Uploaded by

Abdul Wasay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Here are 15 additional practice questions covering all chapters of your

Python Level 2 syllabus, with solutions:

Chapter 1: Basics & I/O

Q1. Odd/Even Checker with File Logging


Write a program that takes an integer input, checks if it's odd or even, and
logs the result to a file odd_even_log.txt with a timestamp.

python

Copy

Download

from datetime import datetime

num = int(input("Enter a number: "))

result = "odd" if num % 2 else "even"

with open("odd_even_log.txt", "a") as f:

f.write(f"{datetime.now()}: {num} is {result}\n")

print(f"{num} is {result}")

Chapter 2: Strings

Q2. Password Strength Checker


Check if a password has:

 ≥8 chars, 1 uppercase, 1 lowercase, 1 digit, and 1 special char (!


@#$%^&*).

python

Copy

Download

import re

password = input("Enter password: ")

is_strong = (len(password) >= 8 and

re.search(r"[A-Z]", password) and

re.search(r"[a-z]", password) and

re.search(r"\d", password) and


re.search(r"[!@#$%^&*]", password))

print("Strong" if is_strong else "Weak")

Chapter 3: Control Flow

Q3. Multiplication Table Generator


Print a multiplication table for a number up to 10, but skip multiples of 3.

python

Copy

Download

n = int(input("Enter number: "))

for i in range(1, 11):

if i % 3 != 0:

print(f"{n} x {i} = {n*i}")

Chapter 4: Functions

Q4. Recursive Fibonacci with Cache


Implement a recursive Fibonacci function that caches results to avoid
redundant calculations.

python

Copy

Download

cache = {0: 0, 1: 1}

def fib(n):

if n not in cache:

cache[n] = fib(n-1) + fib(n-2)

return cache[n]

print(fib(10)) # Output: 55

Chapter 5: Advanced Strings


Q5. Anagram Checker
Check if two strings are anagrams (ignoring case and spaces).

python

Copy

Download

s1 = input("String 1: ").lower().replace(" ", "")

s2 = input("String 2: ").lower().replace(" ", "")

print("Anagram" if sorted(s1) == sorted(s2) else "Not Anagram")

Chapter 6: Data Structures

Q6. List Intersection with Duplicates


Find common elements between two lists, preserving duplicates.

python

Copy

Download

from collections import defaultdict

def common_elements(list1, list2):

count1 = defaultdict(int)

for x in list1:

count1[x] += 1

result = []

for x in list2:

if count1[x] > 0:

result.append(x)

count1[x] -= 1

return result

print(common_elements([1,2,2,3], [2,2,4])) # Output: [2, 2]

Chapter 7: File Handling


Q7. CSV Column Sum Calculator
Read a CSV file data.csv and sum the values in the second column.

python

Copy

Download

import csv

total = 0

with open("data.csv") as f:

reader = csv.reader(f)

next(reader) # Skip header

for row in reader:

total += float(row[1])

print("Sum:", total)

Competition-Style Challenges

Q8. Binary Search on Rotated List


Find a target value in a rotated sorted list (e.g., [5,6,1,2,3,4]).

python

Copy

Download

def search_rotated(nums, target):

left, right = 0, len(nums)-1

while left <= right:

mid = (left + right) // 2

if nums[mid] == target:

return mid

# Left half is sorted

if nums[left] <= nums[mid]:

if nums[left] <= target < nums[mid]:

right = mid - 1
else:

left = mid + 1

# Right half is sorted

else:

if nums[mid] < target <= nums[right]:

left = mid + 1

else:

right = mid - 1

return -1

print(search_rotated([4,5,6,1,2,3], 2)) # Output: 4

Error Handling

Q9. Safe Division with Logging


Divide two numbers, log errors to errors.log, and return None on failure.

python

Copy

Download

def safe_divide(a, b):

try:

return a / b

except Exception as e:

with open("errors.log", "a") as f:

f.write(f"Error: {e}\n")

return None

print(safe_divide(10, 0)) # Logs "ZeroDivisionError"

OOP

Q10. Bank Account Class


Implement a BankAccount class with deposit/withdraw methods and
transaction history.
python

Copy

Download

class BankAccount:

def __init__(self):

self.balance = 0

self.transactions = []

def deposit(self, amount):

self.balance += amount

self.transactions.append(f"Deposit: +{amount}")

def withdraw(self, amount):

if self.balance >= amount:

self.balance -= amount

self.transactions.append(f"Withdraw: -{amount}")

else:

print("Insufficient funds")

def history(self):

return self.transactions

account = BankAccount()

account.deposit(100)

account.withdraw(30)

print(account.history()) # Output: ['Deposit: +100', 'Withdraw: -30']

You might also like