Python Programs
Every common Python program for O Level M3, PR-3 and school practicals — each with the code, its output, how it works step by step, and a Try it Yourself button that runs it in your browser.
Input and operators
- Python Program to Add Two NumberseasyThe first program every Python course teaches after Hello World. It shows the three things almost every program does: read input, work on it, and print a result.
- Python Program to Find Area and Perimeter of a RectangleeasyA classic formula program: read two measurements, apply two formulas, print two answers on separate lines.
- Python Program to Convert Celsius to FahrenheiteasyA formula program that also teaches something important about Python 3: the / operator always gives a decimal answer.
- Python Program to Calculate Simple InteresteasyA formula program seen in almost every beginner exam. It reads three values and applies the simple interest formula.
if / else
- Python Program to Check Even or Odd NumbereasyThe simplest decision-making program. It introduces the modulus operator %, which gives the remainder of a division.
- Python Program to Find the Largest of Three NumberseasyA favourite practical question because it tests whether you can build a decision with more than two branches.
- Python Program to Check Leap YeareasyLeap year looks simple but has a trap — century years. That is exactly why examiners like it.
- Python Program to Check Vowel or ConsonanteasyA short program that shows how Python's in operator can replace a long chain of comparisons.
while loop
- Python Program to Find the Sum of Digits of a NumbereasyThe standard way to take a number apart digit by digit — the same technique is used for reverse, palindrome and Armstrong programs.
- Python Program to Reverse a NumbermediumReversing digits with arithmetic, not string slicing, is what the practical exam usually asks for.
- Python Program to Check Armstrong NumbermediumA number is an Armstrong number when the sum of its digits, each raised to the power of the number of digits, equals the number itself.
- Python Program to Convert Decimal to BinaryhardRepeated division by 2 gives a number's binary digits — read from the last remainder to the first.
for loop
Functions
- Python Program to Find Factorial of a NumbermediumFactorial (n!) is n × (n − 1) × … × 1. Writing it as a function is one of the most common PR-3 questions.
- Python Program to Find GCD and LCM of Two NumbersmediumEuclid's method finds the greatest common divisor quickly; the LCM then follows from a simple formula.
Loops with else
Loops
Strings
Lists
- Python Program to Find Sum, Maximum and Minimum of a ListmediumReading a whole line of numbers into a list is a pattern you will use in almost every list program.
- Python Program to Find the Second Largest Number in a ListmediumOnce a list is sorted, its largest numbers sit at the end — and negative indexes count from the end.
- Python Program for Bubble SorthardBubble sort repeatedly swaps neighbours that are in the wrong order, so large values "bubble" to the end.
- Python Program to Remove Duplicates from a ListhardThe trick is to keep the ORDER — which is why a set alone is not enough.
Nested loops
- Python Program to Print a Right Triangle Star PatternmediumPattern programs are a practical-exam favourite. Python's string repetition makes this one very short.
- Python Program to Print a Number Triangle PatternmediumThe classic nested-loop pattern: the outer loop picks the row, the inner loop prints that row's numbers.
- Python Program to Print Prime Numbers in a RangehardThis combines two ideas: a loop over a range of numbers, and the prime check for each one.
