Python Practice Problems – Answers & Explanations
Numbers
Check if number is even or odd
Use n % 2 == 0 to determine evenness.
Sum 1 to 100
Use sum(range(1, 101)).
If Statements
Positive/negative/zero
Use if n>0, elif n<0, else.
Grading system
Compare ranges using if/elif blocks.
For Loops
Multiples of 3
Loop from 1–100 and check i % 3 == 0.
Count vowels
Iterate string and count characters in 'aeiou'.
Strings
Reverse a string
Use a loop building the reversed string.
Character counts
Use a dictionary or count() in a loop.
Lists
Remove duplicates
Use list(set(lst)) or filter manually.
Find largest/smallest
Use max(lst) and min(lst).
More with Lists
Flatten nested list
Loop sublists and extend a result list.
Rotate left
[Link]([Link](0)).
While Loops
Ask until 'exit'
Use while True and break on input == 'exit'.
Fibonacci to 100
Generate iteratively a,b = b,a+b.
Dictionaries
Word frequencies
Split sentence and increment dictionary counters.
Swap keys/values
Use {v:k for k,v in [Link]()}.
Text Files
Count lines
Open file and use len([Link]()).
Write numbers
Write each number using a loop with [Link]().
Functions
Factorial
Use recursion or iterative multiplication.
Palindrome check
Compare string to its reversed version.