Python Programs With Output
250 programs, each with its output, an explanation and another way to write it.
Basics
- Display a Welcome Message
- Find the Larger of Two Numbers
- Find the Largest of Three Numbers
- Check a Leap Year
- Check Whether a Number Is Even or Odd
- Swap Two Variables
- Calculate Simple Interest
- Calculate Compound Interest
- Convert Celsius to Fahrenheit
- Find the Area of a Circle
- Print a Multiplication Table
- Make a Simple Calculator
- Calculate BMI
- Calculate a Student's Grade
- Print Hello World
- Add Two Numbers
- Check if a Number Is Positive, Negative or Zero
- Find the ASCII Value of a Character
- Find the Average of Numbers
- Generate a Random Number
- Convert Kilometers to Miles
- Calculate Profit or Loss
- Calculate an Electricity Bill
- Find the Area of a Triangle
- Find the Area and Perimeter of a Rectangle
- Check Whether a Triangle Is Equilateral, Isosceles or Scalene
- Check if a Person Is Eligible to Vote
- Calculate Speed, Distance and Time
- Calculate a Discount
- Check if a Character Is a Vowel or Consonant
- an ATM Machine
- a Currency Converter
- Print Even Numbers in a Range
- Check if a Character Is a Letter, Digit or Symbol
- Print a Temperature Conversion Table
Numbers
- Check a Perfect Number
- Check an Armstrong Number
- Check a Palindrome Number
- Check a Prime Number
- Print the Fibonacci Series
- Find the GCD and LCM of Two Numbers
- Find the Sum of a Series
- Find the Factorial of a Number
- Find the Sum of Digits of a Number
- Reverse a Number
- Solve a Quadratic Equation
- Find the Square Root of a Number
- Find the Sum of Natural Numbers
- Print All Prime Numbers in a Range
- Find Armstrong Numbers in a Range
- Find the Factors of a Number
- Find the Prime Factors of a Number
- Convert Decimal to Binary, Octal and Hexadecimal
- Convert Binary to Decimal
- Calculate the Power of a Number
- Count the Number of Digits in a Number
- Check a Strong Number
- Check a Neon Number
- Check a Harshad Number
- Check a Happy Number
- Check an Automorphic Number
- Check a Disarium Number
- Check if a Number Is a Perfect Square
- Find the Sum of Squares of the First N Numbers
- Find the Distance Between Two Points
- Find Pythagorean Triplets
- Print the Collatz Sequence
- Convert a Number to Words
- Convert Roman Numerals to Integers
Strings
- Count Vowels, Consonants, Uppercase and Lowercase Letters
- Check a Palindrome String
- Reverse a String
- Generate a Random Password
- Count the Frequency of Each Character in a String
- Count the Words in a String
- Remove Punctuation from a String
- Sort Words in Alphabetical Order
- Check if Two Strings Are Anagrams
- Count Each Vowel in a String
- Remove Vowels from a String
- Capitalize the First Letter of Each Word
- Check if a String Contains a Substring
- Split a String into a List
- Replace a Character in a String
- Count Occurrences of a Character in a String
- Remove Spaces from a String
- Toggle the Case of a String
- Find the Longest Word in a Sentence
- Find Duplicate Characters in a String
- Find the First Non-Repeating Character
- Check if a String Is a Pangram
- Count Letters, Digits and Spaces in a String
- Reverse the Words in a Sentence
- Encrypt and Decrypt Text with a Caesar Cipher
- Compress a String
- Remove Duplicate Characters from a String
- Make an Acronym from a Phrase
- Check if a String Is a Number
- Validate an Email Address
- Extract Numbers from a String
- Count the Frequency of Words in a String
Lists
- Find the Largest and Smallest Number in a List
- Swap Elements at Even and Odd Positions in a List
- Find the Second Largest Number in a List
- Remove Duplicates from a List
- a Menu-Driven List
- Find the Sum of Elements in a List
- Reverse a List
- Count Occurrences of an Element in a List
- Separate Even and Odd Numbers in a List
- Remove an Element from a List
- Merge Two Lists
- Find Common Elements in Two Lists
- Flatten a Nested List
- Rotate a List
- Split a List into Chunks
- Find Duplicates in a List
- Find the Cumulative Sum of a List
- Multiply All Numbers in a List
- Sort a List of Tuples by the Second Item
- Make a List of Squares Using List Comprehension
- Count Positive and Negative Numbers in a List
- Find the Kth Largest Element in a List
- Move All Zeros to the End of a List
- Find the Missing Number in a List
- Find Pairs with a Given Sum
- Convert a List to a String
- Swap the First and Last Elements of a List
- Check if a List Is Sorted
- Count the Frequency of Each Element in a List
- Find the Sum of a Nested List
- Copy a List
Dictionaries
- Find Students Who Scored Above 75 Using a Dictionary
- Merge Two Dictionaries
- Sort a Dictionary by Value
- Sort a Dictionary by Key
- Check if a Key Exists in a Dictionary
- Create a Dictionary from Two Lists
- Remove a Key from a Dictionary
- Find the Sum of All Values in a Dictionary
- Find the Key with the Maximum Value in a Dictionary
- Make a Simple Phone Book
- Work with a Nested Dictionary
Patterns
- Print a Star Pattern
- Print a Number Pattern
- Print an Alphabet Pattern
- Print a Pyramid Pattern
- Print an Inverted Pyramid
- Print a Diamond Pattern
- Print Floyd's Triangle
- Print Pascal's Triangle
- Print a Hollow Square Pattern
- Print a Hollow Triangle Pattern
- Print a Number Pyramid
- Print a Right-Aligned Triangle
- Print an Hourglass Pattern
- Print a Chessboard Pattern
- Print a Square Star Pattern
- Print an X Pattern
- Print a Heart Pattern
- Print a Butterfly Pattern
- Print a 0-1 Triangle Pattern
- Print a Character Pyramid
- Print a Rhombus Pattern
- Print an Inverted Number Triangle
- Print a Staircase Pattern
Searching and sorting
Matrices
File handling
- Read a Text File and Separate Words with #
- Count Vowels, Consonants, Uppercase and Lowercase Characters in a Text File
- Copy Lines Without the Letter 'a' to Another File
- Search a Roll Number in a Binary File
- Update Marks in a Binary File
- Store and Search User IDs and Passwords in a CSV File
- Count the Words "Me" and "My" in a Text File
- Display Lines Starting with T in a Text File
- Count Words in a Text File
- Read a File Line by Line
- Write to a File
- Append Text to a File
- Copy the Contents of a File
- Count the Lines in a File
- Count the Characters in a File
- Search for a Word in a File
- Read a CSV File
- Write a CSV File
- Read and Write a JSON File
- Find the Longest Line in a File
Data structures
- Implement a Stack Using a List
- Implement a Queue
- Implement a Linked List
- Check Balanced Parentheses Using a Stack
- Implement a Circular Queue
- Implement a Binary Search Tree
- Implement a Priority Queue
- Reverse a String Using a Stack
- Convert Infix to Postfix
- Breadth-First Search on a Graph
- Depth-First Search on a Graph
- Implement a Doubly Linked List
Recursion
Classes and objects
Dates and times
Games and random numbers
Want to write them yourself first? Try the Python exercises, where Check tells you if your code is right.