Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Pavitra
123 articles
Multi-Line printing in Python
Python offers several ways to print multiple lines of text. Instead of using multiple print() statements, you can use triple quotes, escape characters, or other techniques for cleaner multi-line output. Using Triple Quotes Triple quotes allow you to create multi-line strings that preserve formatting and line breaks ? print(''' Motivational Quote: Sometimes later becomes never, Do it now. Great things never come from comfort zones. The harder you work for something, the greater you'll feel when you achieve it. ''') Motivational Quote: Sometimes later becomes never, Do it now. Great things ...
Read MorePython-program-to-convert-pos-to-sop
In this article, we will learn how to convert a Product of Sum (POS) form to its equivalent Sum of Product (SOP) form in Boolean algebra. This conversion is useful in digital logic design and Boolean expression simplification. Problem statement − We are given a POS form and need to convert it into its equivalent SOP form. The conversion involves counting the number of variables in the POS form, extracting maxterms, and then finding the corresponding minterms to generate the SOP expression. Understanding POS to SOP Conversion In Boolean algebra: POS form: Product of sums ...
Read MorePython program to print all Prime numbers in an Interval
In this article, we will learn how to find and print all prime numbers within a given range using Python. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Problem statement − We are given an interval and need to compute all the prime numbers in the given range. We'll use a brute-force approach based on the basic definition of prime numbers. For each number in the range, we check if it has any divisors between 2 and itself (excluding 1 and the number itself). Basic ...
Read MorePython program to interchange first and last elements in a list
In this article, we will learn how to interchange the first and last elements in a Python list. This is a common programming task with multiple implementation approaches. Problem statement − We are given a list, and we need to swap the first element with the last element. There are 4 approaches to solve this problem as discussed below − Using Temporary Variable The most straightforward approach uses a temporary variable to hold one element during the swap operation ? def swapLast(data_list): size = len(data_list) # ...
Read MorePython program to insert an element into sorted list
In this article, we will learn how to insert an element into a sorted list while maintaining the sorted order. This is a common operation when working with ordered data structures. Problem statement − We are given a sorted list, and we need to insert an element while preserving the sorted order. There are two main approaches to solve this problem ? Using Linear Search (Brute Force) This approach searches for the correct position linearly and inserts the element ? def insert_linear(sorted_list, element): # Find the correct position to insert ...
Read MorePython program to get all subsets of a given size of a set
In this article, we will learn how to generate all subsets of a specific size from a given set. This is a common problem in combinatorics where we need to find all possible combinations of n elements from a larger set. Problem statement − We are given a set, we need to list all the subsets of size n. We have three approaches to solve the problem using Python's itertools module ? Using itertools.combinations() Method The most straightforward approach uses itertools.combinations() which returns tuples of the specified size ? import itertools def findsubsets(s, ...
Read MorePython Program to find whether a no is power of two
In this article, we will learn how to check whether a given number is a power of two. A number is a power of two if it can be expressed as 2^n where n is a non-negative integer (e.g., 1, 2, 4, 8, 16, 32...). Problem statement − We are given a number, we need to check that the number is a power of two or not. We can solve this using three approaches as discussed below. Using Division Method This approach repeatedly divides the number by 2 until we reach 1 or find that the ...
Read MorePython program to find the sum of all items in a dictionary
In this article, we will learn different approaches to calculate the sum of all values in a dictionary. Python provides several methods to iterate through dictionary values and compute their total. Problem statement − We are given a dictionary, and we need to calculate the sum of all its values. Here are four effective approaches to solve this problem ? Method 1: Using Dictionary Items Iteration This approach iterates through dictionary items and accesses values using keys ? # sum function def calculate_sum(my_dict): total = 0 ...
Read MorePython program to find the second maximum value in Dictionary
In this article, we will learn how to find the second maximum value in a Python dictionary. We'll explore three different approaches: using built-in sorting functions, manual sorting, and a brute-force method. Problem statement − We are given a dictionary with key-value pairs, and we need to find the second maximum value among all dictionary values. Using sorted() Function The simplest approach is to sort the dictionary values and access the second largest element using negative indexing ? # input dictionary example_dict = {"tutor": 3, "tutorials": 15, "point": 9, "tutorialspoint": 19} # sorting the ...
Read MorePython program to find the second largest number in a list
Finding the second largest number in a list is a common programming task. Python offers several approaches, each with different trade-offs in terms of readability, efficiency, and handling of edge cases. Using set() and remove() Functions This approach converts the list to a set to remove duplicates, then removes the maximum element to find the second largest ? numbers = [11, 22, 1, 2, 5, 67, 21, 32] # Convert to set to get unique elements unique_numbers = set(numbers) # Remove the largest element unique_numbers.remove(max(unique_numbers)) # Find the maximum of remaining elements second_largest = max(unique_numbers) ...
Read More