Python Programs
Python Programs | Python Programming Examples
Last Updated : 17 Mar 2025
Python is one of the most popular and widely used programming languages globally. Due to its easy syntax, it is often the preferred choice for most programmers, especially beginners. To make your hands strong in Python, it is better to upscale your logical concepts and programming skills.
The following sections contain a wide range of Python programming examples. These Python code examples cover the Python language, including Lists, Strings, Dictionaries, Tuples, sets, and many more.
What is the Python Programming Language?
Unlike many other programming languages, Python is well known for its simplicity and readability due to having a high-level interpretation that makes it not complicated to understand. Developed by Guido van Rossum in 1991 and released to the public, Python ranks among the most sought-after programming languages today, alongside Shell Script, Java, C++, and Perl, used in web development, data analysis, artificial intelligence, scientific computing, and more.
What is a Program?
A program, in the realm of programming languages of computer science and software development, is a definition of a set of instructions in a certain language which are given a task or tasked to resolve a particular problem. These guides are known as the code. They are written in a language, which a computer can follow in order to fulfil the result that we want.
Lets see the list of Python programs from basics to advanced in the entire tutorial.
To begin, we will start with the basic concept in Python. Practicing the basics will strength the fundamental syntax and programming logic of Python. In this section, we will explore the common Python programming examples.
- Python program to print "Hello Python"
- Python program to do arithmetical operations
- Python program to find the area of a triangle
- Python program to solve quadratic equation
- Python program to swap two variables
- Python program to generate a random number
- Python program to convert kilometers to miles
- Python program to convert Celsius to Fahrenheit
- Python program to display calendar
- Python Program to Check if a Number is Positive, Negative or Zero
- Python Program to Check if a Number is Odd or Even
- Python Program to Check Leap Year
- Python Program to Check Prime Number
- Python Program to Print all Prime Numbers in an Interval
- Python Program to Find the Factorial of a Number
- Python Program to Display the multiplication Table
- Python Program to Print the Fibonacci sequence
- Python Program to Check Armstrong Number
- Python Program to Find Armstrong Number in an Interval
- Python Program to Find the Sum of Natural Numbers
- Python Program to Print Reverse of a String
- Python Program to Print Sum of First Ten Natural Numbers
In the next section we will see the list of programs based on functions.
Before we going to see the list of programs based on functions l, let us know what is a function.
What is a Function in Python?
In the field of computer science, a function is a code that has a self contained block of code and performs a concrete job or operation. Abilities of functions are made to divide program into modules and they are reusable, this makes programming more readable, concise and convenient to debug.
Python Function Programs
- Python Program to Find LCM
- Python Program to Find HCF
- Python Program to Convert Decimal to Binary, Octal and Hexadecimal
- Python Program To Find ASCII value of a character
- Python Program to Make a Simple Calculator
- Python Program to Display Calendar
- Python Program to Display Fibonacci Sequence Using Recursion
- Python Program to Find Factorial of Number Using Recursion
- Python Program to Calculate the Power of a Number
What is Python Number?
In Python, Numbers are a fundamental data type used to represent and manipulate numerical values. Python has support for three types of numbers, including integers, floating-point numbers, and complex numbers. These numbers are defined as int, float, and complex classes in Python.
Python Number Programs
- Python program to check if the given number is a Disarium Number
- Python program to print all disarium numbers between 1 to 100
- Python program to check if the given number is Happy Number
- Python program to print all happy numbers between 1 and 100
- Python program to determine whether the given number is a Harshad Number
- Python program to print all pronic numbers between 1 and 100
- Python program to print first ten natural numbers.
- Python Progran to check an Armstrong number or not
What is an Array?
An array, one of the most important elements, is used in computer science as a fundamental data structure in which a collection of elements is stored in the contagious memory locations and all these data are usually of one type. The arrays give the chance to grouped and access data in a consecutive chunk of memory space. They can be accessed through their indices or positions in the array.
Let's see the list of programs based on an arrays below:
Python Array Programs
- Python program to copy all elements of one array into another array
- Python program to find the frequency of each element in the array
- Python program to left rotate the elements of an array
- Python program to print the duplicate elements of an array
- Python program to print the elements of an array
- Python program to print the elements of an array in reverse order
- Python program to print the elements of an array present on even position
- Python program to print the elements of an array present on odd position
- Python program to print the largest element in an array
- Python program to print the smallest element in an array
- Python program to print the number of elements present in an array
- Python program to print the sum of all elements in an array
- Python program to right rotate the elements of an array
- Python program to sort the elements of an array in ascending order
- Python program to sort the elements of an array in descending order
- Python Program to Merge Two Arrays into a Single Array
- Python Program to Insert an Element into an Array
- Python Program to Delete an Element from an Array
What is a Matrix?
In Computer Science, a matrix is an abbreviation of a two dimensional array, where the elements are retrieved via providing both horizontal and vertical indices or coordinates.
Matrices are being used in the areas like computer graphics, machine learning, and numerical simulations for example image processing, coarse-grained data analysis, and solving systems of equations.
Let's see the list of python programs based on Matrix below:
Python Matrix Programs
- Python Program to Add Two Matrices
- Python Program to Multiply Two Matrices
- Python Program to Transpose a Matrix
- Python Program to Perform Matrix Rotation
- Matrix Concatenation Program
- Python Program to Print Inverse of a Matrix
- Matrix Rank Calculation
- Python Program to perform vertical Concatenation in Matrix
- Python Program to fetch the Kth Column of Matrix
What is a String?
In the programmes of computers, "string" is known as a series of character(s), mostly used for the representation of the texts. Impressively, most programming languages, including Python, have a choice of using either single quote (') or double quote (").For example:
Now we will see the list of python programs based on Strings below.
Python String Programs
- Python Program to Sort Words in Alphabetic Order
- Python Program to Remove Punctuation From a String
- Python Program to reverse a string
- Python Program to convert list to string
- Python Program to convert int to string
- Python Program to concatenate two strings
- Python Program to generate a Random String
- Python Program to convert Bytes to string
- Python Program to check whether given string is a palindrome or not
- Python Program to print length of a string
- Python Program to reverse the characters of a string
- Convert the string into lowercase to uppercase
- Convert the string into uppercase to lowercase
- Python Program to find the occurrence of a substring within a string
- Python Program to convert Hexadecimal String to Decimal String
- Python program to check if a Substring is Present in the String
- Python program to convert Snake case to Pascal case
- Python program to print even length words in a string
- Python program to accept the strings which contains all vowels
- Python program to count the Number of matching characters in a pair of String
- Python program to Remove all duplicates from a given string
- Python program to check to find the Least Frequent Character in String
- Python program to find the Maximum frequency character in String
- Python program to check if the given String contains any special character
- Python Program to check if the string is binary string or not
- Python Program to find the uncommon words in two Strings
- Python Program to replace duplicate Occurrence
- Python Program to replace multiple words with K in the given String
- Python Program to find the permutation of the string
- Python Program to check for URL in a String
- Python Program to find a String of Code
- Python Program to perform string slicing to rotate a string
- Python Program to perform String slicing to check if a string can become empty by recursive deletion
- Python Program to find all duplicate characters
- Remove Multiple Characters from a String in Python
What is a List?
From Python a "list" is a decked-out data structure that you can keep different items in and access them depending on the position. The support of the built-in data type, sequence of elements within square brackets [ ], is one of the many functions that sets Python apart. Such Lists can contain elements of mixed types, and these elements may be either numbers, strings, other lists or any other Python object.
Here's an example of a simple list:
Lists offer several important characteristics:
- Ordered: Agree to a list in which elements are added in order.
- Mutable: Near the end of the list, there is a very distinct portion to be altered once the whole list and all its items have been created. Therefore, you can append, change, or delete components of the list.
- Allows Duplicates: Sets cannot include similar elements.
- Heterogeneous: Lists have the ability to contain elements of different data types.
- Dynamic: List's dimension may change as and when elements are inserted or deleted thereof.
Below we will see the python Programs based on list.
Python List Programs
- Python Program to append element in the list
- Python Program to compare two lists
- Python Program to convert list to dictionary
- Python Program to remove an element from a list
- Python Program to add two lists
- Python Program to convert List to Set
- Python Program to convert list to string
- Python Program to remove duplicates from a list
- Python program to print length of a list
- Python program to clear a list
- Python program to Reversing a List
- Python program to find the sum of elements in List
- Python program to multiply all numbers in the List
- Python program to find the smallest number in a list
- Python program to find the largest number in a list
- Python program to find the second largest number in a list
- Python program to find N largest elements from a list
- Python program to find the even numbers in a list
- Python program to find the Odd numbers in a List
- Python program to print all even numbers in a range
- Python program to print all odd numbers in a range
- Python program to print positive numbers in a list
- Python program to print negative numbers in a list
- Python program to print all positive numbers in a range
- Python program to print all negative numbers in a range
- Python program to remove multiple elements from a list
- Python program to remove empty List from List
- Python program to calculate the average of list in Python
- Python program to count the occurrences of an element in a list
- Python program to remove empty tuples from a list
- Python program to remove the duplicates from a list
- Python program to calculate the sum of number digits in List
- Python program to break a list into chunks of size N
- Python program to sort the values of first List using second List
- Python Program to Find Intersection of Two Lists
What is a Data Structure?
A data structure is a method of data storage in a computer where data is organized and arranged in a manner that allows the most effective access, modification, and processing. Data structures are used to structure and store data, providing users with functionality that enables fast searches and manipulation of information.
Linear Data Structures:
Non sequential structures such as linear data organization structures in accordance with the sequence order, where each element is positioned between its predecessor and successor. Examples include:
- Arrays
- Linked Lists
- Stacks
- Queues
Non-Linear Data Structures:
Non-lineer data structures facilitate in arranging the elements in a more intricate way creating relationship other than just the purely sequential predecessor-successor quote. Examples include:
- Trees
- Graphs
- Heaps
Let's see list of Programs based on Singly Linked List.
Python Singly Linked List Programs
- Python program to create a Singly Linked List of N nodes and count the number of nodes
- Python program to create a Singly Linked List of n nodes and display it in reverse order
- Python program to create and display a Singly Linked List
- Python program to delete a node from the beginning of the Singly Linked List
- Python program to delete a node from the end of the Singly Linked List
- Python program to delete a node from the middle of the Singly Linked List
- Python program to find the maximum and minimum value node from a Singly Linked List
- Python program to insert a new node at the beginning of the Singly Linked List
- Python program to insert a new node at the end of the Singly Linked List
- Python program to insert a new node at the middle of the Singly Linked List
- Python program to remove duplicate elements from a Singly Linked List
- Python program to search an element in a Singly Linked List
- Python program to sort the elements of the Singly Linked List
Python Doubly Linked List Programs
- Python program to convert a given binary tree to doubly linked list
- Python program to create a doubly linked list from a ternary tree
- Python program to create a doubly linked list of n nodes and count the number of nodes
- Python program to create a doubly linked list of n nodes and display it in reverse order
- Python program to create and display a doubly linked list
- Python program to delete a new node from the beginning of the doubly linked list
- Python program to delete a new node from the end of the doubly linked list
- Python program to delete a new node from the middle of the doubly linked list
- Python program to find the maximum and minimum value node from a doubly linked list
- Python program to insert a new node at the beginning of the Doubly Linked list
- Python program to insert a new node at the end of the Doubly Linked List
- Python program to insert a new node at the middle of the Doubly Linked List
- Python program to remove duplicate elements from a Doubly Linked List
- Python program to rotate doubly linked list by N nodes
- Python program to search an element in a doubly linked list
Python Dictionary Programs
- Python Program to create a dictionary
- Python Program to convert list to dictionary
- Python Program to sort a dictionary
- Python Program to Merge two Dictionaries
Python Searching and Sorting Programs
- Binary Search in Python
- Linear Search in Python
- Effective Root Searching Algorithms in Python
- Bubble Sort in Python
- Insertion Sort in Python
- Heap Sort in Python
- Merge Sort in Python
- Recursive Insertion Sort in Python
- QuickSort in Python in Python
- Iterative Quick Sort in Python
- Selection Sort in Python
- Iterative Merge Sort in Python
- Tim Sort in Python
- Counting Sort in Python
- ShellSort in Python
- Topology Sorting in Python
- Radix Sort in Python
- Binary Insertion Sort in Python
- Bitonic Sort in Python
- Comb Sort in Python
- Pigeonhole Sort in Python
- Cocktail Sort in Python
- Gnome Sort in Python
- Odd-Even Sort / Brick Sort in Python
- BogoSort or Permutation Sort in Python
- Cycle Sort in Python
- Stooge Sort in Python
Python Circular Linked List Programs
- Python program to create a Circular Linked List of N nodes and count the number of nodes
- Python program to create a Circular Linked List of n nodes and display it in reverse order
- Python program to create and display a Circular Linked List
- Python program to delete a node from the beginning of the Circular Linked List
- Python program to delete a node from the end of the Circular Linked List
- Python program to delete a node from the middle of the Circular Linked List
- Python program to find the maximum and minimum value node from a circular linked list
- Python program to insert a new node at the beginning of the Circular Linked List
- Python program to insert a new node at the end of the Circular Linked List
- Python program to insert a new node at the middle of the Circular Linked List
- Python program to remove duplicate elements from a Circular Linked List
- Python program to search an element in a Circular Linked List
- Python program to sort the elements of the Circular Linked List
Programs Based on Queues
- Python program to find the size of the queue
- Python program to add elements in the queue
- Python programs to delete elements from the queue
- Python program to print peek element from the queue
- Python program to check isFull() condition in a queue
- Python program to check isEmpty() condition in a queue
Python Programs Based on Stack
- Python program to perform push() operation
- Python program to perform pop() operation
- Python program to find length of the stack
- Python program to print peek element from the stack
Python Programs Based on Trees
- Python program to implement Binary Search Tree
- Python program to implement Balanced Binary Tree
- Python Program to implement AVL Trees
What is a Graph?
A graph is a non-linear data structure comprised of a set of vertices (or points that might be alternatively referred to as nodes) that are connected by edges (or links and arcs). Graphs are often pictorially illustrated to represent the relations between two variables. It can be implemented as an equivalent of real-life network structures, either social networks, road nets, channel connections, etc.
Here are some key components and characteristics of graphs:
1. Vertices (Nodes):
A simple graph is made up of dots, or vertices. The graph is visually constructed from the vertices which are the entities such as the people in a social network, the cities in a road network, and the routers in a computer network.
2. Edges (Links):
Nodes of a graph represent the relationships or linkages between any two vertices. While the direction of relationships may be either directed, which means the relationship is one-way, or undirected, which means that the relationship is two-way.
In a directed graph, for an edge, there is a directed flow or relationship attached to it, inducing an edge from one vertex to the other.
In a graph that has no direction (undirected graph), edges represents a mutual relationship of two vertices.
3. Path:
Putting together the vertices in the graph with the connecting edges is known as a path. Links may depict bypasses, relationships or links between graph's elements.
4. Cycle:
A cycle in a graph is the steps of a path that commence at the same vertex and go through all the vertices and edges of a graph.
Types of Graphs:
Graphs can be categorized into various types based on their properties and characteristics, including:
In the first graph (the directed graph), each node has an arrow that points to another node, expressing a certain relationship between them. In the second one, there are no arrows and the nodes are linked by lines expressing their relative position.
Weighted Graphs and Unweighted Graphs; These are two types of graphs used in standard network topologies.
- DAGs (DAGs refers to directed graphs with no cycles), and Cyclic Graphs are two very important graph categories.
- Bipartite Graphs
- Complete Graphs
Let us see some python programs based on graphs.
Python Programs based on Graphs
- Python program to implement Breath First Search
- Python program to implement Depth First Search
- Python program to find all connected components in an undirected graph using BFS or DFS
What is an Algorithm?
An algorithm is a set of steps scheduled and organized how to solve a problem or perform a certain computation. It is a description of a set of input data as well as the processing required to produce the desired result. The algorithms are in fact the main core behind the computer science as it is so efficient in solving numerous computation problems.
Python Programs Based on Algorithms
- Python program to implement Linear Search
- Python program to implement Binary Search
- Python program to implement Bubble Sort
- Python program to implement Insertion Sort
- Python program to implement Selection Sort
- Python program to implement Merge Sort
- Python program to implement Quick Sort
- Python program to Dijkstra's algorithm to find minimum spanning tree
- Python program to implement Prims Algorithm
- Python program to implement Kruskal's Algorithm
- Python program to implement greedy algorithm to solve Fractional Knap Sack
- Python program to solve coin change problem using greedy approach
- Python program to solve N-Queen problem
File Handling in Python
File handling plays an important role in nay programming languages including Python, when the user wants to permanently store the data into the file. In this section, we will explore the different File Handing programming examples.
- How to read CSV file in Python?
- How to read JSON file in Python?
- How to read a text file in Python?
- How to Write in a Text File using Python?
- How to Create Requirements.txt File in Python?
- How to find 'n' Character Words in a Python Text File?
- How to get the line number in which given word is present?
- How to count the number of lines in a Python text file?
- How to remove repeated lines from a Python file?
- How to append the content of one file to another file in Python?
Other important Programs
- Tower of Hanoi using Python
- Python program to reverse a Linked List
- Python Program to Print Pascal Triangle
- Basic Euclidean algorithms in Python
- Sieve of Eratosthenes in Python
- Caesar Cipher in Python
- How to clear Python shell
- How to Print Pattern in Python
- How to Convert Text to Speech in Python
- Program of Cumulative sum in python
- How to get the current date in Python?
- Create BMI Calculator using Python
- Python Program to Find Number of Days Between Two Given Dates
Python Programs FAQs
1. What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability. It's widely used in web development, data science, automation, and more.
2. Who developed Python?
Python was created by Guido van Rossum and released in 1991.
3. What is the latest version of Python?
As of mid-2025, the latest stable version is likely Python 3.12 (you can check on python.org).
4. Is Python case-sensitive?
Yes, Python is case-sensitive. For example, Variable and variable are different.
5. Why is Python so popular?
Because it is:
- Easy to learn and write
- Open-source
- Has huge community support
- Supports multiple domains (web, AI, automation, etc.)