Python Programs
Practice problems solved the Pythonic way. Each program states the problem, explains the approach, shows code you can run and edit in your browser, and lists variations. They are ordered roughly from easiest to hardest inside each group.
Number programs
Python Program to Check Even or Odd Number
To check if a number is even or odd in Python, test `number % 2 == 0`: a remainder of 0 means the number is even, anything else m…
Python Program to Check Prime NumberA prime number is a whole number greater than 1 that has no divisors other than 1 and itself; in Python you test it by trying div…
Python Program to Find Factorial of a NumberThe factorial of n (written n!) is the product of all whole numbers from 1 to n, and in Python you can compute it with a loop, wi…
Python Program to Print the Fibonacci SeriesThe Fibonacci series starts with 0 and 1, and every next term is the sum of the previous two; in Python the simplest way to print…
Python Program to Check Armstrong NumberAn Armstrong number equals the sum of its digits each raised to the power of the number of digits; for example `153 = 1^3 + 5^3 +…
Python Program to Find GCD of Two NumbersThe GCD (greatest common divisor, also called HCF) of two numbers is the largest number that divides both exactly; in Python use …
Python Program to Check Leap YearA year is a leap year if it is divisible by 4, except century years, which must also be divisible by 400; in Python that is `year…
Python Program to Find the Sum of Digits of a NumberTo find the sum of the digits of a number in Python, either convert it to a string and add each digit with `sum(int(d) for d in s…
Python Program to Print a Multiplication TableTo print a multiplication table in Python, loop `i` from 1 to 10 with `range(1, 11)` and print `n * i` for each step, for example…
Python FizzBuzz ProgramFizzBuzz asks you to print the numbers 1 to 100, replacing multiples of 3 with `Fizz`, multiples of 5 with `Buzz` and multiples o…
String programs
Python Program to Check Palindrome (String and Number)
A palindrome reads the same forwards and backwards. In Python, clean the text with `lower()` and compare it to its reverse: `s ==…
Python Program to Count Vowels in a StringTo count vowels in Python, loop over the lowercase string and count the characters found in `"aeiou"`: `sum(1 for ch in text.lowe…
Python Anagram Program: Check if Two Strings Are AnagramsTwo strings are anagrams if they contain the same letters the same number of times. In Python, compare `sorted(a) == sorted(b)` a…
Python Word Count Program: Count Words in a StringTo count words in Python, split the string on whitespace and take the length: `len(text.split())`.
Python Program to Count Character Frequency in a StringTo count character frequency in Python, loop over the string and keep a dictionary of counts: `freq[ch] = freq.get(ch, 0) + 1`, o…
List and dictionary programs
Python Program to Find the Largest Number in a List
The largest number in a list is `max(numbers)`. To do it by hand, start with the first item and replace it whenever you meet a bi…
Python Program to Find the Second Largest Number in a ListTo find the second largest number, remove duplicates, sort, and take the second from the end: `sorted(set(nums))[-2]`.
Python Program to Calculate the Average of a ListThe average of a list is the sum divided by the count: `sum(numbers) / len(numbers)`.
Python Program to Flatten a Nested List of Any DepthTo flatten a list of lists one level, use `[x for row in nested for x in row]`. For unknown depth, use a recursive function.
Python Program to Transpose a MatrixTo transpose a matrix stored as a list of lists, use `[list(row) for row in zip(*matrix)]`: rows become columns and columns becom…
Python Program to Merge Two Sorted ListsTo merge two sorted lists, compare their front items repeatedly and take the smaller one. In Python, `list(heapq.merge(a, b))` do…
Algorithms
Python Program for Linear Search (with Examples)
Linear search checks every item of a list from left to right and returns the index of the first match, or `-1` if the target is n…
Python Binary Search Program (Iterative and Recursive)Binary search finds a value in a **sorted** list by repeatedly comparing the target with the middle item and discarding the half …
Python Bubble Sort Program (with Explanation)Bubble sort sorts a list by repeatedly comparing neighbouring items and swapping them when they are in the wrong order, so the la…
Python Selection Sort Program (with Explanation)Selection sort sorts a list by repeatedly finding the smallest remaining item and swapping it into the next position at the front…
Python Tower of Hanoi Program (Recursion Explained)The Tower of Hanoi puzzle is solved in Python with a short recursive function: move n − 1 disks to the spare peg, move the bigges…
Mini programs
Python Number Guessing Game Program
A number guessing game picks a secret number with `random.randint()`, then loops with `input()` telling the player "too low" or "…
Python Simple Calculator ProgramA simple Python calculator reads two numbers and an operator with `input()`, then uses `if/elif` (or a dictionary of functions) t…
Python Random Password Generator ProgramA Python password generator joins randomly chosen characters from `string.ascii_letters`, `string.digits` and `string.punctuation…
Python BMI Calculator ProgramA BMI calculator computes `weight_kg / height_m ** 2` and uses `if/elif` to name the category: below 18.5 underweight, below 25 n…
Python Rock Paper Scissors Game ProgramA Python rock paper scissors game lets the computer pick with `random.choice()`, reads the player's choice with `input()` and dec…
Python Tip Calculator Program (Split the Bill)A tip calculator multiplies the bill by the tip percentage divided by 100, adds it to the bill, then divides the total by the num…
Python Star Pattern Programs (Triangle, Pyramid, Diamond)Star patterns in Python are printed row by row with a loop and string repetition: `print("*" * i)` gives a right triangle, and `p…
Explore more
Python ReferencePython ComparisonsPython How-To GuidesPython Cheat SheetsPython glossaryPython tutorial