Introduction
Python is one of the most popular programming languages for both beginners and professionals.
If you are searching for simple python programs or a beginner python tutorial, this page will guide you with practical examples.
Here, you’ll find python programming examples and sample python code that help you build a strong foundation.
What is Python?
Python is an interpreted, high-level, and general-purpose programming language.
It is known for its simple syntax and versatility, making it ideal for learning and practicing simple code in python.
Its wide library support and active community help learners write python simple code quickly and efficiently.
Features of Python
– Easy to learn and use
– High-level language
– Extensive libraries
– Portable and dynamic
These features make it perfect for exploring simple programs in python for beginners.
Why Learn Python?
Learning Python enables you to automate tasks, analyze data, build applications, and create scripts.
For beginners, working with python programming examples and python script example files is the best way to get hands-on experience.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
Top 50 Python Programming Examples
Below are categorized python programming examples to help you understand different concepts through simple python programs.
Each example provides a simple code in python to illustrate common use cases.
Basic Programs
- Python Hello World Program
- Add Two Numbers
- Check Even or Odd Number
- Swap Two Variables
- Find Factorial of a Number
- Reverse a Number
- Program to check the prime number
- Program to find the GCD of two numbers
- Program to find the sum of digits.
- Program to check Armstrong number.
These examples are ideal for anyone looking for a simple program in python for beginners.
Each python script example focuses on a single concept, ensuring clarity and easy understanding.
Array Programs
- Program to the maximum in an array.
- Program to the minimum in an array.
- Program to find the sum of array elements.
- Program to reverse the array elements.
- Program to merge array elements.
- Program to remove duplicates from an array.
- Program to rotate array.
- Program to find the second largest array element.
List Programs
- Program to append elements to the list.
- Program to extend the list.
- Program to insert an element at the specific position.
- Program to remove an element from the list.
- Program to pop list element.
- Program to count occurrences of list elements.
Matrix Programs
- Program to create a matrix.
- Program to transpose a matrix.
- Program to perform the addition and subtraction of two matrices.
- Program to find matrix inversion.
- Program to find a trace of a matrix.
String Programs
String manipulation is essential in python programming examples.
Below are a few sample python code snippets that help you practice string operations:
- Python program to check if a string is palindrome or not
- Python program to check whether the string is Symmetrical or Palindrome
- Reverse words in a given String in Python
- Program to remove Kth character from string in Python
- Program to check if a Substring is Present in a Given String
Dictionary Programs
- Program to extract Unique values dictionary values
- Program to find the sum of all items in a dictionary
- Program to remove a key from a dictionary
- Program to sort the list of dictionaries by values in Python using itemgetter.
- Program to sort the list of dictionaries by values in Python using lambda
Tuple Programs
- Program to find the size of a Tuple
- Program to find the maximum and minimum K elements in Tuple
- Program to create a list of tuples from a given list having a number and its cube in each tuple
- Program to add a Tuple to the List
Searching and Sorting Programs
- Program for Binary Search
- Program for Linear Search
- Program for Insertion Sort
- Program for QuickSort
- Program for Selection Sort
- Program for Bubble Sort
- Program for Merge Sort
Basic Programs
Python Hello World Program
The given below is a basic syntax to write a Python program:
Code:
<b>print(‘Hello, World!’)</b>What this does:
- The print() function outputs the text “Hello, World!” to the console.
- This minimal example verifies that your Python installation works and that you understand how to run a script. Learn Python+1
Key basics:
- Variables: In Python you don’t declare types explicitly. You simply assign a value to a name, e.g. x = 10. The interpreter infers the type. Real Python
- Data types: Examples of built-in types include:
- int (whole numbers, e.g., 100)
- float (decimal numbers, e.g., 3.14)
- str (strings/text, e.g., “Hello”)
- bool (boolean, either True or False) Real Python
- Comments:
- Single-line comments use #.
- Multi-line comments or doc-strings typically use triple quotes “”” … “””.
- Environment / Version note: It is recommended to use Python 3 (not Python 2) — Python 3 uses print() as a function. Learn Python+1
- Trends & context: Python’s role continues to expand in data science, AI, automation and is highly popular in 2025. Python in Plain English+1
- Good practice tip: Use an editor or IDE with Python 3 support. Run the script via terminal/command-line or inside the IDE to ensure everything works as expected.
1. Add Two Numbers
Code:
<span style="font-weight: 400;"> </span><span style="font-weight: 400;">num1 = 50 # input number 1</span>
<span style="font-weight: 400;"> </span><span style="font-weight: 400;">num2 = 270 # input number 2 </span>
<span style="font-weight: 400;"> </span><span style="font-weight: 400;">sum = num1 + num2 # adding two numbers to get the sum</span>
<span style="font-weight: 400;"> </span><span style="font-weight: 400;">print(f"The sum of 2 numbers is: {sum}") # printing the sum</span>Output:
The sum of 2 numbers is: 320
2. Program to check whether a number is even or odd.
Code:
<span style="font-weight: 400;">number = int(input("Enter a number: ")) # getting a number input to check for odd or even </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># checking number using the remainder</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if number % 2 == 0:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print("The number is Even") # Even no if remainder is 0</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">else:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print("The number is Odd") # Odd no if remainder is not 0</span>Output:
Enter a number: 252
The number is Even <strong> </strong>
3. Program to swap two numbers.
Code:
<span style="font-weight: 400;">num1 = 56 # first number</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">num2 = 90 # second number </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The number 1 is {num1}, num2 = {num2}") # printing the original numbers </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># swapping two numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">num1, num2 = num2, num1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"Swapped numbers are: num1 = {num1}, num2 = {num2}") # printing the swapped numbers</span>Output:
The number 1 is 56, num2 = 90
Swapped numbers are: num1 = 90,
num2 = 56
4. Program to find the factorial of a number.
Code:
<span style="font-weight: 400;"># define a factorial function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def factorial(num):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if num == 0: # return 1 if num is 0</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return num * factorial(num - 1) # return factorial of num </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ans = int(input("Enter a number to find factorial: ")) # getting input to find factorial</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The Factorial of {ans} is {factorial(ans)}") # printing factorial of a number</span>Output:
Enter a number to find factorial: 5
The Factorial of 5 is 120
5. Program to reverse a number.
Code:
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">num = int(input("Enter a number: ")) # getting input to reverse it</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">rev_num = 0 # initial variable with 0 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># while loop until num is not 0</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">while num != 0:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">digit = num % 10 # finding the remainder</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">rev_num = rev_num * 10 + digit</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">num //= 10 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"Reversed Number Is: {rev_num}") # printing the results</span>Output:
Enter a number: 524512
Reversed Number Is: 215425
6. Program to check the prime number.
Code:
<span style="font-weight: 400;"># defining the prime check function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def prime_chk(num):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if num <= 1:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return False</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for i in range(2, int(num ** 0.5) + 1):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if num % i == 0:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return False</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return True </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ans = int(input("Enter a number to check for prime: "))</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The number {ans} is prime: {prime_chk(ans)}")</span>
<span style="font-weight: 400;"> </span>Output:
Enter a number to check for prime: 25
The number 25 is prime: False
7. Program to find the GCD of two numbers.
Code:
<span style="font-weight: 400;"># defining the gcd function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def gcd(a, b):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">while b:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">a, b = b, a % b</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return a </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">n1 = int(input("Enter one number: ")) # first input number</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">n2 = int(input("Enter second number: ")) # second input number</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"GCD of {n1} and {n2} is {gcd(n1, n2)}") # finding GCD of numbers</span>
Output:
Enter one number: 5
Enter second number: 10
GCD of 5 and 10 is 5
8. Program to find the sum of digits.
Code:
<span style="font-weight: 400;">num = int(input("Enter any number: ")) # taking an input number</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">sum = 0 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># while loop</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">while num != 0:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">sum += num % 10 # adding sum with the remainder of input number</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">num //= 10 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The sum of digits: {sum}")</span>
Output:
Enter any number: 251
The sum of digits: 8 <strong> </strong>
9. Program to check Armstrong number.
Code:
<span style="font-weight: 400;">n = int(input("Enter a number: ")) # taking an input number</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">sum_cubes = sum(int(digit) ** 3 for digit in str(n)) # finding sum cubes</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if n == sum_cubes:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The number {n} is an Armstrong number") # printing if n is armstrong</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">else:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The number {n} is not an Armstrong number") # printing if n is not armstrong</span>
<span style="font-weight: 400;"> </span>
<b>Output:</b>Enter a number: 25551
The number 25551 is not an Armstrong number
Array Programs
Array programs focus on performing operations like searching, sorting, and modifying collections of elements efficiently. These examples help build a strong foundation in handling data sequences in Python.
10. Program to the maximum in an array.
Code:
<span style="font-weight: 400;">arr = [45, 54, 76,123,25,123] # inputted array of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">max_value = max(arr) # finding the max value of the max function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The Maximum element in the array is: {max_value}") #printing the max element from array</span>
Output:
The Maximum element in the array is: 123
11. Program to the minimum in an array.
Code:
<span style="font-weight: 400;">arr = [45, 54, 76,123,25,123] # inputted array of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">min_value = min(arr) # finding the max value of the min function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The minimum element in the array is: {min_value}") #printing the min element from array</span>
<span style="font-weight: 400;"> </span>Output:
The minimum element in the array is: 25
12. Program to find the sum of array elements.
Code:
<span style="font-weight: 400;">arr = [45, 54, 76,123,25,123] # inputted array of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">sum_of_arr = sum(arr) # finding the sum value of array elements</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The sum of elements in the array is: {sum_of_arr}") # printing the sum of element in an array</span>
Output:
The sum of elements in the array is: 446
13. Program to reverse the array elements.
Code:
<span style="font-weight: 400;">arr = [45, 54, 76,123,25,123] # inputted array of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">reversed_arr = arr[::-1] # finding the reverse array elements</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The reversed array is: {reversed_arr}") # printing the reverse of elements in an array</span>
Output:
The reversed array is: [123, 25, 123, 76, 54, 45]
14. Program to merge array elements.
Code:
<span style="font-weight: 400;">arr1 = [45, 54, 76,123,25,123] # inputted array 1 of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr2 = [123, 25, 123, 76, 54, 45] # inputted array 2 of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">merged_arrays = arr1 + arr2 # finding the merged array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The merged array is: {merged_arrays}") # printing the merged array</span>
Output:
The merged array is: [45, 54, 76, 123, 25, 123, 123, 25, 123, 76, 54, 45]
15. Program to remove duplicates from an array.
Code:
<span style="font-weight: 400;">arr = [45, 54, 76,123,25,123] # inputted array of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">new_arr = list(set(arr)) # finding the new array with no duplicate elements</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The reversed array is: {new_arr}") # printing the new elements in an array</span>
Output:
The reversed array is: [76, 45, 54, 25, 123]
16 Program to rotate array.
Code:
<span style="font-weight: 400;"># defining the rotate function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def rotate(arr, d):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return arr[d:] + arr[:d] # rotating the array elements </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr = [45, 54, 76, 123, 25, 123] # inputted array of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">d = 3</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The rotated array is: {rotate(arr, d)}") # printing the rotated array</span>
Output:
The rotated array is: [123, 25, 123, 45, 54, 76]
17. Program to find the second largest array element.
Code:
<span style="font-weight: 400;">arr = [45, 54, 76, 123, 25, 123, 450] # inputted array of numbers </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr.remove(max(arr)) # removing the max element</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">sec_largest = max(arr) # finding the max element which is the second largest now </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The second largest element in the array is: {sec_largest}") # printing the second largest element of an array</span>
Output:
The second largest element in the array is: 123
List Programs
List programs cover essential operations such as adding, removing, and modifying elements. They help you understand Python’s dynamic and versatile list data structure for effective data manipulation.
18. Program to append elements to the list.
Code:
<span style="font-weight: 400;">user_list = [56, 73, 12, 6876, 1257, 120, 1223] # appending numbers in the list of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">user_list.append(609345)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The list after adding elements is: {user_list}") # printing the list in python after adding elements</span>
Output:
The list after adding elements is: [56, 73, 12, 6876, 1257, 120, 1223, 609345]
19. Program to extend the list.
Code:
<span style="font-weight: 400;">user_list_1 = [56, 73, 12, 6876, 1257, 120, 1223] # appending numbers in the list of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">user_list_2 = [98, 34, 23, 87, 1246, 123, 756] # appending numbers in the list of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">user_list_1.extend(user_list_2)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The list extending the list 1 is: {user_list_1}") # printing the list in python after extending</span>
Output:
The list extending the list 1 is: [56, 73, 12, 6876, 1257, 120, 1223, 98, 34, 23, 87, 1246, 123, 756]
20 Program to insert an element at the specific position.
Code:
<span style="font-weight: 400;">user_list_1 = [56, 73, 12, 6876, 1257, 120, 1223] # appending numbers in the list of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">user_list_1.insert(4, 85)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The list after inserting an element is: {user_list_1}") # printing the list in python after inserting an element</span>
Output:
The list after inserting an element is: [56, 73, 12, 6876, 85, 1257, 120, 1223]
21. Program to remove an element from the list.
Code:
<span style="font-weight: 400;">user_list_1 = [56, 73, 12, 6876, 1257, 120, 1223] # appending numbers in the list of numbers </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">user_list_1.remove(73) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The list after removing an element is: {user_list_1}") # printing the list in python after removal</span>
Output:
The list after removing an element is: [56, 12, 6876, 1257, 120, 1223]
22. Program to pop list element.
Code:
<span style="font-weight: 400;">user_list_1 = [56, 73, 12, 6876, 1257, 120, 1223] # appending numbers in the list of numbers </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">popped = user_list_1.pop() </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The list after popping an element is: {user_list_1}") # printing the list in python after popping</span>
Output:
The list after popping an element is: [56, 73, 12, 6876, 1257, 120]
23. Program to count occurrences of list elements.
Code:
<span style="font-weight: 400;">user_list_1 = [56, 73, 12, 73, 44, 73, 6876, 1257, 120, 1223] # appending numbers in the list of numbers</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">num = 73</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">cnt = user_list_1.count(num) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The count of {num}: {cnt}") # printing the list in python after counting a specific number</span>
Output:
The count of 73: 3
Matrix Programs
Matrix programs involve performing operations like creation, transformation, and arithmetic on matrices. These examples strengthen your understanding of structured numerical data handling in Python.
24. Program to create a matrix.
Code:
<span style="font-weight: 400;"># defining a matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">matrix = [</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[45, 65, 23],</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[90, 67, 12],</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[65, 12 ,46]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print("The given matrix is:") </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Using for-in to print the matrix rows</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for row in matrix:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(row)</span>
<span style="font-weight: 400;"> </span>Output:
The given matrix is:
[45, 65, 23]
[90, 67, 12]
[65, 12, 46]
25. Program to transpose a matrix.
Code:
<span style="font-weight: 400;"># defining a matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">matrix = [</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[45, 65, 23],</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[90, 67, 12],</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[65, 12 ,46]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Transposing the matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">transp = [[row[i] for row in matrix] for i in range(len(matrix[0]))] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print("The given matrix is:") </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Using for-in to print the matrix rows</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for row in transp:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(row)</span>
Output:
The given matrix is:
[45, 90, 65]
[65, 67, 12]
[23, 12, 46]
26. Program to perform the addition and subtraction of two matrices.
Code:
# Defining the first matrix
m1 = [
[45, 65, 23],
[90, 67, 12],
[65, 12 ,46]
]
# defining the second matrix
m2 = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
]
# Adding two matrices
addition_mat = [[m1[i][j] + m2[i][j] for j in range(len(m1[0]))] for i in range(len(m1))]
# Printing the matrix
print(“The addition of two matrices are:”)
# Using for-in to print the matrix
for row in addition_mat:
print(row)
# subtracting two matrices
subtracting_mat = [[m1[i][j] – m2[i][j] for j in range(len(m1[0]))] for i in range(len(m1))]
# Printing the matrix
print(“The subtraction of two matrices are:”)
# Using for-in to print the matrix
for row in subtracting_mat:
print(row)
Output:
The addition of two matrices are:
[54, 73, 30]
[96, 72, 16]
[68, 14, 47]
The subtraction of two matrices are:
[36, 57, 16]
[84, 62, 8]
[62, 10, 45]
27. Program to find matrix inversion.
Code:
<span style="font-weight: 400;"># defining the matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">matrix = [</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[1, 2],</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[4, 5]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Finding the determinant of the matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">det = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Check if the determinant is non-zero</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if det != 0:</span>
<span style="font-weight: 400;"># Calculate the inverse of the 2x2 matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">inv = [</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[matrix[1][1] / det, -matrix[0][1] / det],</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">[-matrix[1][0] / det, matrix[0][0] / det]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the inverse of the matrix</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print("the inverse of the matrix is-")</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for row in inv:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(row)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">else:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print("The given matrix is singular and does not have an inverse")</span>
<span style="font-weight: 400;"> </span>Output:
the inverse of the matrix is-
[-1.6666666666666667, 0.6666666666666666]
[1.3333333333333333, -0.3333333333333333]
28. Program to find a trace of a matrix.
Code:
# Defining the first matrix
m1 = [
[45, 65, 23],
[90, 67, 12],
[65, 12 ,46]
]
# Calculate the trace of the matrix
ans_matrix = sum(m1[i][i] for i in range(len(m1))) # finding the trace i.e., sum of the diagonal elements
# print the matrix
print(f”Trace of the matrix: {ans_matrix}”)
Output:
Trace of the matrix: 158
String Programs
String programs focus on operations like checking patterns, modifying text, and reversing strings. They help build a solid grasp of Python’s powerful string manipulation capabilities.
29. Python program to check if a string is palindrome or not
Code:
<span style="font-weight: 400;"># defining the palindrome function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def palindrome_chk(string):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Compare the string with its reverse</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return string == string[::-1] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Example usage</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">my_string = "level"</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if palindrome_chk(my_string):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The string {my_string} is a palindrome.")</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">else:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The string {my_string} is not a palindrome.")</span>
Output:
The string level is a palindrome.
30. Python program to check whether the string is Symmetrical or Palindrome
Code:
<span style="font-weight: 400;"># defining the symm_chk function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def symm_chk(string):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Check if the first half is equal to the second half</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">n = len(string)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return string[:n//2] == string[n//2:]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># defining the palindrome function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def palindrome_chk(string):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Compare the string with its reverse</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return string == string[::-1] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Example usage</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">my_string = "abba"</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The string {my_string} is symmetrical: {symm_chk(my_string)}")</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The string {my_string} is palindrome: {palindrome_chk(my_string)}")</span>
Output:
The string abba is symmetrical: False
The string abba is palindrome: True
31. Reverse words in a given String in Python
Code:
<span style="font-weight: 400;"># Defining function to reverse words</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def rev(s):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Split the string into words and reverse the word order</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">words = s.split()</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">rev = " ".join(reversed(words))</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return rev</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the reverse words of a string</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">my_string = "Hello HeroVide!"</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The reversed words of the string {my_string} is: {rev(my_string)}")</span>
Output:
The reversed words of the string Hello HeroVide! is: HeroVide! Hello
32. Program to remove Kth character from string in Python
Code:
<span style="font-weight: 400;"># defining function to remove ith char</span>
<span style="font-weight: 400;">def remove_character(s, i):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Remove the char at the ith position</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return s[:i] + s[i+1:]</span>
<span style="font-weight: 400;"># Printing the new string</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">my_string = "HeroVide"</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">i = 3 # position for removal </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The String {my_string} after removing the {i}-th character: {remove_character(my_string, i)}")</span>
<span style="font-weight: 400;"> </span>Output:
The String HeroVide after removing the 3-th character: HerVide
33. Program to check if a Substring is Present in a Given String
Code:
# defining the function to check for substring
def chk_subs(str, sub):
# Check if the substring is in the string
return sub in str
# Printing the string after checking the substring
my_str = “Welcome to HeroVired”
target = “HeroVired”
if chk_subs(my_str, target):
print(f”The substring ‘{target}’ is present in ‘{my_str}’.”)
else:
print(f”The substring ‘{target}’ is not present in ‘{my_str}’.”)
Output:
The substring ‘HeroVired’ is present in ‘Welcome to HeroVired’.
Dictionary Programs
Dictionary programs demonstrate how to store, access, and manipulate key-value pairs. These examples build a clear understanding of Python’s efficient mapping data structure.
34. Program to extract Unique values dictionary values
Code:
<span style="font-weight: 400;"># defining the function for checking unique values in the dictionary</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def chk_unique(di):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Extract unique values from dictionary values</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ans = set(value for values in di.values() for value in values)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return ans</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the dictionary values</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">di = {'A': [56, 45, 123], 'B': [123, 45, 2], 'C': [21, 45]}</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ans = chk_unique(di) # adding the unique values in the ans</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"Unique values: {ans}")</span>
Output:
Unique values: {2, 45, 21, 56, 123}
35. Program to find the sum of all items in a dictionary
Code:
<span style="font-weight: 400;"># defining the function for checking unique values in the dictionary</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def find_sum(di):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Calculating the sum of dictionary items</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return sum(di.values()) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the sum of items in a dictionary</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">di = {'a': 455, 'b': 455, 'c': 674}</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ans = find_sum(di)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The sum of all items: {ans}")</span>
<span style="font-weight: 400;"> </span>Output:
The sum of all items: 1584
36. Program to remove a key from a dictionary
Code:
<span style="font-weight: 400;"># defining the function for removing the key in the dictionary</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def rem_key(di, key):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Removing the specified key from the dictionary</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if key in di:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">del di[key]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return di </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Example usage</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">di = {'a': 34, 'b': 56, 'c': 6734}</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">key = 'c'</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The values in the dictionary after removing key '{key}' is: {rem_key(di, key)}")</span>
Output:
The values in the dictionary after removing key ‘c’ is: {‘a’: 34, ‘b’: 56}
37. Program to sort the list of dictionaries by values in Python using itemgetter.
Code:
<span style="font-weight: 400;"># importing the itemgetter from the operator</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">from operator import itemgetter </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># defining the function for sorting the dictionary</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def sorting_dict_item(my_list, key):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Sort the list of dictionaries by the specified key using itemgetter</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return sorted(my_list, key=itemgetter(key)) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the sorted lists after sorting</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">my_list = [{'vehicle': 'BMW', 'year': 2025}, {'vehicle': 'Mercedes', 'year': 2022}, {'vehicle': 'Audi', 'year': 2023}]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"Sorted list of dictionaries by 'year': {sorting_dict_item(my_list, 'year')}")</span>
Output:
Sorted list of dictionaries by ‘year’: [{‘vehicle’: ‘Mercedes’, ‘year’: 2022}, {‘vehicle’: ‘Audi’, ‘year’: 2023}, {‘vehicle’: ‘BMW’, ‘year’: 2025}]
38. Program to sort the list of dictionaries by values in Python using lambda
Code:
<span style="font-weight: 400;"># defining the function for sorting the dictionary</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def sorting_dict_item(my_list, key):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Sort the list of dictionaries by the specified key using the lambda function in Python</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return sorted(my_list, key=lambda x: x[key]) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the sorted lists after sorting</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">my_list = [{'vehicle': 'BMW', 'year': 2025}, {'vehicle': 'Mercedes', 'year': 2022}, {'vehicle': 'Audi', 'year': 2023}]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"Sorted list of dictionaries by 'year' using lambda: {sorting_dict_item(my_list, 'year')}")</span>
Output:
Sorted list of dictionaries by ‘year’ using lambda: [{‘vehicle’: ‘Mercedes’, ‘year’: 2022}, {‘vehicle’: ‘Audi’, ‘year’: 2023}, {‘vehicle’: ‘BMW’, ‘year’: 2025}]
Tuple Programs
Tuples is an immutable sequential data structure that allows for storing collections of heterogeneous data. Here are some of the best Python programming examples of tuples to practice.
39. Program to find the size of a Tuple
Code:
<span style="font-weight: 400;"># defining the function to find tuple size</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def my_tuple(item):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Return the size of the tuple in bytes</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return item.__sizeof__() </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the size of the tuple</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">item = (56, 35, 66, 23, 87)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The size of the tuple is: {my_tuple(item)} bytes")</span>
<b>Output:</b>The size of the tuple is: 64 bytes
40. Program to find the maximum and minimum K elements in Tuple
Code:
<span style="font-weight: 400;"># defining the function to find max and min K elements</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def max_min_k_elements(tup, k):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># sort the tuple and return the first and last k elements</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">sort_tuple = sorted(tup)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return sort_tuple[:k], sort_tuple[-k:]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># creating a tuple</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">tup = (8, 9, 10, 12, 46, 12, 45, 10) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># size of k</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">k = 2</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Finding the max and min elements from the function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">min_k_tuple, max_k_tuple = max_min_k_elements(tup, k) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># printing the minimum and maximum elements</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The minimum {k} elements is: {min_k_tuple}")</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The maximum {k} elements is: {max_k_tuple}")</span>
Output:
The minimum 3 elements is: [8, 9, 10]
The maximum 3 elements is: [12, 45, 46]
41. Program to create a list of tuples from a given list having a number and its cube in each tuple
Code:
<span style="font-weight: 400;"># defining the function</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def my_function(lst):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Convert a list to a list of tuples containing the number and their cube</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return [(x, x**3) for x in lst] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># creating a tuple</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">list = [8, 9, 10, 12, 46, 12, 45, 10] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Finding the cube of each list element</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">list_my = my_function(list)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># printing the minimum and maximum elements</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"List of tuples is: {list_my}")</span>
Output:
List of tuples is: [(8, 512), (9, 729), (10, 1000), (12, 1728), (46, 97336), (12, 1728), (45, 91125), (10, 1000)]
42. Program to add a Tuple to the List
Code:
<span style="font-weight: 400;"># defining the function to add a tuple to the list</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def add_tp_ls(ls, tp):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Add a tuple to the list</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ls.append(tp)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return ls </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># defining the function to add a list to a tuple</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def add_ls_tp(tp, ls):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Add a list to the tuple</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return tp + tuple(ls) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Defining list and tuple with values</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ls = [10,15, 20, 50]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">tp = (40, 60, 70, 80) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The list after adding tuple: {add_tp_ls(ls, tp)}")</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The tuple after adding list: {add_ls_tp(tp, ls)}")</span>
Output:
The list after adding tuple: [10, 15, 20, 50, (40, 60, 70, 80)]
The tuple after adding list: (40, 60, 70, 80, 10, 15, 20, 50, (40, 60, 70, 80))
43. Program to find the closest Pair to the Kth index element in the Tuple
Code:
<span style="font-weight: 400;"># defining the function to find the closest pair</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def find_cl_pair(tp, k):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Find the closest pair to the k-th index element in the tuple</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">k_el = tp[k]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">close = min(tp, key=lambda x: abs(x - k_el))</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return k_el, close </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Defining list and tuple with values</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">tp = (40, 60, 70, 80)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">k = 3 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">k_el, close = find_cl_pair(tp, k)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"Element at index {k}: {k_el}")</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"Closest element to {k_el}: {close}")</span>
Output:
Element at index 3: 80
Closest element to 80: 80
Searching and Sorting Programs
Searching and sorting programs focus on efficiently locating elements and arranging data. These examples strengthen algorithmic thinking and improve problem-solving skills in Python.
- Program for Binary Search
Code:
<span style="font-weight: 400;"># defining the binary search recursive function to search for an element</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def bs_recursive(arr, start, end, target):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Base case: If the element is not present</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if end < start:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return -1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">mid = (end + start) // 2 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># If the element is present at the middle</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if arr[mid] == target:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return mid</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># If the element at mid is greater than the target</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">elif arr[mid] > target:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return bs_recursive(arr, start, mid - 1, target)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">else:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return bs_recursive(arr, mid + 1, end, target) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def bs_iterative(arr, target):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">start, end = 0, len(arr) - 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">while start <= end:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">mid = (start + end) // 2 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if arr[mid] == target:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return mid</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">elif arr[mid] > target:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">end = mid - 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">else:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">start = mid + 1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return -1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the searched element using binary search</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr1 = [7, 8, 10, 2, 4, 80]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">target1 = 80 # element to search </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># find the target</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">result1 = bs_recursive(arr1, 0, 5, target1)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The element found using the Binary Search iterative approach is at index: {result1}")</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr2 = [7, 8, 10, 2, 4, 80]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">target2 = 10 # element to search</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># find the target</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">result2 = bs_iterative(arr2, target2)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The element found using the Binary Search recursive approach is at index: {result2}")</span>
<span style="font-weight: 400;"> </span>Output:
The element found using the Binary Search iterative approach is at index: 5
The element found using the Binary Search recursive approach is at index: 2
45. Program for Linear Search
Code:
<span style="font-weight: 400;"># defining the binary search recursive function to search for an element</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def ls_iterative(arr, target):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Traverse through the array to find the element</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for i in range(len(arr)):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if arr[i] == target:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return i</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return -1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the searched element using linear search</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr1 = [7, 8, 10, 2, 4, 80]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">target1 = 4 # element to search</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># find the target</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">result1 = ls_iterative(arr1, target1)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The element found using Linear Search at index: {result1}")</span>
Output:
The element found using Linear Search at index: 4
- Program for Insertion Sort
Code:
<span style="font-weight: 400;"># defining the sorting function to sort the array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def ins_sort(arr):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Traverse from 1 to the length of the array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for i in range(1, len(arr)):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">key = arr[i]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">j = i - 1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Move elements of arr[0..i-1] that are greater than key to 1 position ahead</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">while j >= 0 and key < arr[j]:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[j + 1] = arr[j]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">j -= 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[j + 1] = key </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the sorted array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr = [7, 8, 10, 2, 4, 80]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ins_sort(arr)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The sorted array is: {arr}")</span>
Output:
The sorted array is: [2, 4, 7, 8, 10, 80]
47. Program for QuickSort
Code:
<span style="font-weight: 400;"># defining the sorting function to sort the array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def find_partition(arr, low, high):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">i = low - 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">pivot = arr[high] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for j in range(low, high):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if arr[j] <= pivot:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">i += 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[i], arr[j] = arr[j], arr[i] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[i + 1], arr[high] = arr[high], arr[i + 1]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">return i + 1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def qs_sort(arr, low, high):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if low < high:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">pi = find_partition(arr, low, high)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">qs_sort(arr, low, pi - 1)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">qs_sort(arr, pi + 1, high) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the sorted array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr = [7, 8, 10, 2, 4, 80]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">qs_sort(arr, 0, len(arr) - 1)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The sorted array is: {arr}")</span>
<span style="font-weight: 400;"> </span>Output:
The sorted array is: [2, 4, 7, 8, 10, 80]
48. Program for Selection Sort
Code:
# defining the sorting function to sort the array
def sel_sort(arr):
# Traverse through all array elements
for i in range(len(arr)):
min_idx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
# Printing the sorted array
arr = [7, 8, 10, 2, 4, 80]
sel_sort(arr)
print(f”The sorted array is: {arr}”)
Output:
The sorted array is: [2, 4, 7, 8, 10, 80]
49. Program for Bubble Sort
Code:
<span style="font-weight: 400;"># defining the sorting function to sort the array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def bb_sort(arr):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">n = len(arr)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for i in range(n):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">for j in range(0, n-i-1):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if arr[j] > arr[j+1]:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[j], arr[j+1] = arr[j+1], arr[j] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the sorted array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr = [7, 8, 10, 2, 4, 80]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">bb_sort(arr)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"><strong>print(f"The sorted array is: {arr}") </strong></span>
Output:
The sorted array is: [2, 4, 7, 8, 10, 80] <strong> </strong> <strong> </strong>
50. Program for Merge Sort
Code:
<span style="font-weight: 400;"># defining the sorting function to sort the array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">def ms_sort(arr):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if len(arr) > 1: </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># find the midpoint</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">mid = len(arr) // 2</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">left = arr[:mid]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">right = arr[mid:] </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># sort the left part</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ms_sort(left)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># sort the right part</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ms_sort(right) </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">i = j = k = 0 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># run a while loop to check multiple cases</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">while i < len(left) and j < len(right):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">if left[i] < right[j]:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[k] = left[i]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">i += 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">else:</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[k] = right[j]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">j += 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">k += 1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">while i < len(left):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[k] = left[i]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">i += 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">k += 1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">while j < len(right):</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr[k] = right[j]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">j += 1</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">k += 1 </span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;"># Printing the sorted array</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">arr = [7, 8, 10, 2, 4, 80]</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">ms_sort(arr)</span>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">print(f"The sorted array is: {arr}")</span>
<span style="font-weight: 400;"> </span>
<b>Output:</b>
<span style="font-weight: 400;"> </span>
<span style="font-weight: 400;">The sorted array is: [2, 4, 7, 8, 10, 80]</span>
<span style="font-weight: 400;"> </span>Conclusion
In this article, we covered multiple Python programming examples for practice on various topics. The topics include basic, array programs, list programs, matrix programs, string, programs, etc. We have also covered using Python dictionary programs, tuple programs, and searching and sorting programs.
The examples discussed in this article provide a thorough manual for anyone wishing to practise and improve their Python skills, covering everything from simple string operations to advanced algorithms. Regardless of your level of programming experience, these examples will help you improve and advance your Python skills.
Can we create a matrix in Python?
How are dictionaries used in Python?
Is Python a good programming language?
What is the best Python program?
How to write a Python program?
Updated on November 6, 2025
