Python Examples
Classic Python programs, each with the code, its exact output, a step-by-step explanation, the mistakes people make and a few exercises. Every output was produced by running the program.
Each example opens in the online Python Compiler with one click, so you can change it and run it straight away, or press Visualize step by step to watch the program run line by line with its variables and call stack. Everything runs in your browser, with nothing to install.
Also available: JavaScript Examples.
Basics
Input, arithmetic and if/else: the first programs everyone writes.
- Python Program to Add Two Numbers
Add two numbers in Python, first with fixed values and then with numbers read from input(), and learn why input() needs int() or float() before adding.
Beginner - Python Program to Swap Two Variables
Swap the values of two variables in Python with tuple unpacking (a, b = b, a), and see the temporary-variable method other languages use.
Beginner - Python Program to Check if a Number is Even or Odd
Check whether a number is even or odd in Python with the modulo operator (%), including negative numbers, plus a short version with a conditional expression.
Beginner - Python Program to Find the Largest Among Three Numbers
Find the largest of three numbers in Python with if/elif/else and the and operator, then do it in one line with the built-in max() function.
Beginner - Python Program to Check Leap Year
Check whether a year is a leap year in Python with the full rule (divisible by 4, but centuries only when divisible by 400), and compare with calendar.isleap().
Beginner
Numbers
Primes, factorials, Fibonacci and other loops over digits and divisors.
- Python Program to Check Prime Number
Check whether a number is prime in Python by testing divisors up to its square root, with a step-by-step explanation and the bugs that make 1 or 9 look prime.
Beginner - Python Program to Print All Prime Numbers in an Interval
Print all prime numbers between two numbers in Python, first with a nested loop and for-else, then with the much faster Sieve of Eratosthenes.
Intermediate - Python Program to Find the Factorial of a Number
Find the factorial of a number in Python with a loop, with recursion and with math.factorial(), and see how recursive calls stack up in the visualizer.
Beginner - Python Program to Print the Fibonacci Sequence
Print the Fibonacci sequence in Python with a loop and tuple unpacking, and see why the naive recursive version gets so slow, plus a cached fix.
Beginner - Python Program to Check Armstrong Number
Check whether a number is an Armstrong (narcissistic) number in Python by raising each digit to the number of digits, and find all of them below 10,000.
Beginner - Python Program to Find the Sum of Digits of a Number
Find the sum of the digits of a number in Python with a while loop using % and //, and in one line with sum() and map(), handling negative numbers too.
Beginner - Python Program to Find the GCD (HCF) of Two Numbers
Find the greatest common divisor (GCD, also called HCF) of two numbers in Python with Euclid's algorithm and math.gcd(), and use it to get the LCM.
Beginner - Python Program to Convert Decimal to Binary
Convert a decimal number to binary in Python by repeated division by 2, then with bin() and format(), and convert binary back to decimal with int(text, 2).
Beginner
Strings
Reversing, counting and comparing text.
- Python Program to Check if a String is a Palindrome
Check whether a string or number is a palindrome in Python with slicing ([::-1]), ignoring case, spaces and punctuation, plus a two-pointer version.
Beginner - Python Program to Reverse a String
Reverse a string in Python with slicing [::-1], with reversed() and join(), and with a loop, and learn to reverse the order of words in a sentence.
Beginner - Python Program to Count the Number of Vowels in a String
Count the vowels in a string in Python with a loop and a set, count each vowel separately with a dictionary, and do it in one line with sum().
Beginner - Python Program to Check if Two Strings are Anagrams
Check whether two strings are anagrams in Python by sorting their letters or counting them with collections.Counter, ignoring case and spaces.
Beginner - Python Program to Count the Frequency of Words in a String
Count how often each word appears in a text in Python with a dictionary and dict.get(), then with collections.Counter, ignoring case and punctuation.
Intermediate
Lists and algorithms
Searching, sorting and working with lists.
- Python Program to Find the Largest Element in a List
Find the largest number in a Python list with a loop, then with max(), and find the second largest correctly even when the largest value appears twice.
Beginner - Python Program to Remove Duplicates from a List
Remove duplicate items from a Python list while keeping the original order, with a loop and a set, with dict.fromkeys(), and know when set() is enough.
Beginner - Python Program for Bubble Sort
Sort a list with bubble sort in Python, step through every swap in the visualizer, stop early when the list is already sorted, and compare with sorted().
Intermediate - Python Program for Binary Search
Implement binary search in Python on a sorted list, see how it halves the search range each step, and use the bisect module for real code.
Intermediate
Patterns and small projects
Nested loops and complete little programs.
- Python Program to Print a Star Pattern (Pyramid)
Print star patterns in Python: a right triangle, a centred pyramid and an inverted triangle, built with nested loops and string multiplication.
Beginner - Python Program to Display the Multiplication Table
Print the multiplication table of any number in Python with a for loop and aligned f-strings, then a full 1-to-10 grid with nested loops.
Beginner - Python Program to Make a Simple Calculator
Build a simple calculator in Python that reads expressions like "12 * 4", picks the operation from a dictionary and handles division by zero and bad input.
Intermediate