Array / List Programs
To scale up Array logic, try out the below-listed Python array programming
examples. Here, you will find all the important Python programming examples
that are related to the Python array concept.
1. Sum of array
2. Largest element in an array
3. Array rotation
4. Reversal algorithm for array rotation
5. Split the array and add the first part to the end
6. Find reminder of array multiplication divided by n
7. Check if given array is Monotonic
8. Interchange first and last elements in a list
9. Swap two elements in a list
10. Ways to find length of list
11. Ways to check if element exists in list
12. Different ways to clear a list
13. Reversing a List
14. Find sum of elements in list
15. Multiply all numbers in the list
16. Find smallest number in a list
17. Find largest number in a list
18. Find second largest number in a list
19. Find N largest elements from a list
20. Even numbers in a list
21. Odd numbers in a List
22. Print all even numbers in a range
23. Print all odd numbers in a range
24. Print positive numbers in a list
25. Print negative numbers in a list
26. Print all positive numbers in a range
27. Print all negative numbers in a range
28. Remove multiple elements from a list
29. Remove empty List from List
30. Cloning or Copying a list
31. Count occurrences of an element in a list
32. Remove empty tuples from a list
33. Print duplicates from a list of integers
34. Find Cumulative sum of a list
35. Sum of number digits in List
36. Break a list into chunks of size N
37. Sort the values of first list using second list
Matrix Programs
Get a detailed list of Python Matrix examples and boost your understanding of
matrix concepts in Python.
1. Add two Matrices
2. Multiply two matrices
3. Matrix Product
4. Adding and Subtracting Matrices
5. Transpose a matrix in Single line
6. Matrix creation of n*n
7. Get Kth Column of Matrix
8. Vertical Concatenation in Matrix
String Programs
If you are looking for Python programming examples that are based on the
Python string concept, then scroll down to the below section and practice a
wide range of Python string program examples.
1. Check if a string is palindrome or not
2. Check whether the string is Symmetrical or Palindrome
3. Reverse words in a given String
4. Ways to remove i’th character from string
5. Check if a Substring is Present in a Given String
6. Words Frequency in String Shorthands
7. Convert Snake case to Pascal case
8. Find length of a string (4 ways)
9. Print even length words in a string
10. Accept the strings which contains all vowels
11. Count the Number of matching characters in a pair of string
12. Remove all duplicates from a given string
13. Least Frequent Character in String
14. Maximum frequency character in String
15. Check if a string contains any special character
16. Generating random strings until a given string is generated
17. Find words which are greater than given length k
18. Removing i-th character from a string
19. Split and join a string
20. Check if a given string is binary string or not
21. Find uncommon words from two Strings
22. Replace duplicate Occurrence in String
23. Replace multiple words with K
24. Permutation of a given string using inbuilt function
25. Check for URL in a String
26. Execute a String of Code
27. String slicing to rotate a string
28. String slicing to check if a string can become empty by recursive
deletion
29. Counter| Find all duplicate characters in string
30. Replace all occurrences of a substring in a string
Dictionary Programs
In this section, you will find out all the important practice sets or examples
related to the Python Dictionary concept.
1. Extract Unique values dictionary values
2. Find the sum of all items in a dictionary
3. Ways to remove a key from dictionary
4. Ways to sort list of dictionaries by values Using itemgetter
5. Ways to sort list of dictionaries by values Using lambda function
6. Merging two Dictionaries
7. Convert key-values list to flat dictionary
8. Insertion at the beginning in OrderedDict
9. Check order of character in string using OrderedDict( )
10. Dictionary and counter to find winner of election
11. Append Dictionary Keys and Values ( In order ) in dictionary
12. Sort Python Dictionaries by Key or Value
13. Sort Dictionary key and values List
14. Handling missing keys in Python dictionaries
15. Dictionary with keys having multiple inputs
16. Print anagrams together using List and Dictionary
17. K’th Non-repeating Character using List Comprehension and
OrderedDict
18. Check if binary representations of two numbers are anagram
19. Counter to find the size of largest subset of anagram words
20. Remove all duplicates words from a given sentence
21. Dictionary to find mirror characters in a string
22. Counting the frequencies in a list using dictionary
23. Convert a list of Tuples into Dictionary
24. Counter and dictionary intersection example (Make a string using
deletion and rearrangement)
25. Dictionary, set and counter to check if frequencies can become same
26. Scraping And Finding Ordered Words In A Dictionary
27. Possible Words using given characters
28. Keys associated with Values in Dictionary
Tuple Programs
Explore the wide range of Tuple programs here in this section of Python
programming examples.
1. Find the size of a Tuple
2. Maximum and Minimum K elements in Tuple
3. Create a list of tuples from given list having number and its cube in each
tuple
4. Adding Tuple to List and vice – versa
5. Closest Pair to Kth index element in Tuple
6. Join Tuples if similar initial element
7. Extract digits from Tuple list
8. All pair combinations of 2 tuples
9. Remove Tuples of Length K
10. Sort a list of tuples by second Item
11. Order Tuples using external List
12. Flatten tuple of List to tuple
13. Convert Nested Tuple to Custom Key Dictionary
Searching and Sorting Programs
In this section, on searching and sorting examples, we have mentioned all the
important example sets of Python searching and sorting to boost your Python
programming concept.
1. Binary Search (Recursive and Iterative)
2. Linear Search
3. Insertion Sort
4. Recursive Insertion Sort
5. QuickSort
6. Iterative Quick Sort
7. Selection Sort
8. Bubble Sort
9. Merge Sort
10. Iterative Merge Sort
11. Heap Sort
12. Counting Sort
13. ShellSort
14. Topological Sorting
15. Radix Sort
16. Binary Insertion Sort
17. Bitonic Sort
18. Comb Sort
19. Pigeonhole Sort
20. Cocktail Sort
21. Gnome Sort
22. Odd-Even Sort / Brick Sort
23. BogoSort or Permutation Sort
24. Cycle Sort
25. Stooge Sort
Pattern Printing Programs
Get a complete list of Python pattern printing examples in the below section.
1. Print the pattern ‘G’
2. Print an Inverted Star Pattern
3. Print double sided stair-case pattern
4. Print with your own font
Date-Time Programs
In this section, we have mentioned all important Python program examples
that are related to the Python Date-Time concept.
1. Get Current Time
2. Get Current Date and Time
3. Find yesterday’s, today’s and tomorrow’s date
4. Convert time from 12 hour to 24 hour format
5. Find difference between current time and given time
6. Create a Lap Timer
7. Convert date string to timestamp
8. Convert timestamp string to datetime object?
9. Find number of times every day occurs in a Year
Python Regex Programs
To boost Python Regex concept get a list of Python Regex programming
examples below.
1. Check if String Contain Only Defined Characters using Regex
2. Count Uppercase, Lowercase, special character and numeric values using
Regex
3. Find the most occurring number in a string using Regex
4. Regex to extract maximum numeric value from a string
5. Program to put spaces between words starting with capital letters using
Regex
6. Check whether a string starts and ends with the same character or not
7. Regex to find sequences of one upper case letter followed by lower case
letters
8. Remove duplicate words from Sentence
9. Remove all characters except letters and numbers
10. Accept string ending with alphanumeric character
11. Accept string starting with vowel
12. Check if a string starts with a substring using regex
13. Check if an URL is valid or not using Regular Expression
14. Parsing and Processing URL – Regex
15. Validate an IP address using ReGex
16. Check if email address valid or not
17. Find files having a particular extension using RegEx
18. Extract IP address from file
19. Check the validity of a Password
20. Categorize Password as Strong or Weak using Regex
Python File Handling Programs
If you want to scale up your Python file handling concept, explore the below
section and find out all the real-life Python programming examples on Python
file handling.
1. Read file word by word
2. Read character by character from a file
3. Get number of characters, words, spaces and lines in a file
4. Count the Number of occurrences of a key-value pair in a text file
5. Finding ‘n’ Character Words in a Text File
6. Obtain the line number in which given word is present
7. Count number of lines in a text file
8. Remove lines starting with any prefix
9. Eliminate repeated lines from a file
10. Read List of Dictionaries from File
11. Append content of one text file to another
12. Copy odd lines of one file to other
13. Merge two files into a third file
14. Reverse a single line of a text file
15. Reverse the content of a file and store it in another file
16. Reverse the Content of a File using Stack
More Popular Python Programs
Find a more important or popular list of Python programming examples below
and upscale your Python programming skills.
1. Reverse a linked list
2. Find largest prime factor of a number
3. Efficient program to print all prime factors of a given number
4. Product of unique prime factors of a number
5. Find sum of odd factors of a number
6. Program for Coin Change
7. Program for Tower of Hanoi
8. Sieve of Eratosthenes
9. Check if binary representation is palindrome
10. Basic Euclidean algorithms
11. Extended Euclidean algorithms
12. Maximum height when coins are arranged in a triangle
13. Find minimum sum of factors of number
14. Difference between sums of odd and even digits
15. Print Matrix in Z form
16. Smallest K digit number divisible by X
17. Print Number series without using any loop
18. Number of stopping station problem
19. Check if a triangle of positive area is possible with the given angles
20. Find the most occurring character and its count
21. Find sum of even factors of a number
22. Check if all digits of a number divide it
23. Check whether a number has consecutive 0’s in the given base or not
24. Number of solutions to Modular Equations
25. Legendre’s Conjecture
Python Program to Print Hello
world!
To understand this example, you should have the knowledge of the
following Python programming topics:
Your First Python Program
Python Basic Input and Output
Source Code
# This program prints Hello, world!
print('Hello, world!')
Run Code
Output
Hello, world!
In this program, we have used the built-in print() function to print the
string Hello, world! on our screen.
By the way, a string is a sequence of characters. In Python, strings are
enclosed inside single quotes, double quotes, or triple quotes.
Python Program to Add Two
Numbers
To understand this example, you should have the knowledge of the
following Python programming topics:
Python Basic Input and Output
Python Data Types
Python Operators
In the program below, we've used the + operator to add two numbers.
Example 1: Add Two Numbers
# This program adds two numbers
num1 = 1.5
num2 = 6.3
# Add two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Run Code
Output
The sum of 1.5 and 6.3 is 7.8
The program below calculates the sum of two numbers entered by the user.
Example 2: Add Two Numbers With User Input
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
# Add two numbers
sum = float(num1) + float(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Run Code
Output
Enter first number: 1.5
Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8
In this program, we asked the user to enter two numbers and this program
displays the sum of two numbers entered by user.
We use the built-in function input() to take the input. Since, input() returns
a string, we convert the string into number using the float() function. Then, the
numbers are added.
Alternative to this, we can perform this addition in a single statement without
using any variables as follows.
print('The sum is %.1f' %(float(input('Enter first number: ')) +
float(input('Enter second number: '))))
Run Code
Output
Enter first number: 1.5
Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8
Although this program uses no variable (memory efficient), it is harder to read.
Python Program to Find the
Square Root
To understand this example, you should have the knowledge of the
following Python programming topics:
Python Basic Input and Output
Python Data Types
Python Operators
Example: For positive numbers
# Python Program to calculate the square root
# Note: change this value for a different result
num = 8
# To take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
Run Code
Output
The square root of 8.000 is 2.828
In this program, we store the number in num and find the square root using
the ** exponent operator. This program works for all positive real numbers.
But for negative or complex numbers, it can be done as follows.
Source code: For real or complex numbers
# Find square root of real or complex numbers
# Importing the complex math module
import cmath
num = 1+2j
# To take input from the user
#num = eval(input('Enter a number: '))
num_sqrt = [Link](num)
print('The square root of {0} is {1:0.3f}
+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))
Run Code
Output
The square root of (1+2j) is 1.272+0.786j
In this program, we use the sqrt() function in the cmath (complex math)
module.
Note: If we want to take complex number as input directly, like 3+4j , we have
to use the eval() function instead of float().
The eval() method can be used to convert complex numbers as input to
the complex objects in Python. To learn more, visit Python eval() function.
Also, notice the way in which the output is formatted. To learn more,
visit string formatting in Python.