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
Python Articles
Page 689 of 855
Python 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 MorePython program to find occurrence to each character in given string
In this article, we will learn different methods to count the occurrence of each character in a given string. Python provides several approaches ranging from manual counting to built-in modules. Problem statement − We are given a string, we need to find the occurrence of each character in a given string. Here we will be discussing 3 approaches as discussed below ? Using Manual Dictionary Approach This brute-force approach iterates through each character and manually counts occurrences using a dictionary ? Example test_str = "Tutorialspoint" # count dictionary count_dict = {} for i ...
Read MorePython program to create a dictionary from a string
In this article, we will learn different methods to convert a string into a dictionary in Python. This is useful when you have string data that represents key-value pairs and need to work with it as a dictionary. Problem statement − We are given a string input and need to convert it into a dictionary type. Here we will discuss three methods to solve this problem, each suitable for different string formats. Method 1 − Using eval() Function The eval() method works when the string syntax already resembles a dictionary format with curly braces, quotes, and ...
Read MoreCount words in a sentence in Python program
In this article, we will learn different approaches to count the number of words in a string in Python. This is a common text processing task that can be accomplished using several methods. Problem statement − We are given a string and need to count the number of words in it. Using split() Method The split() method breaks the string into a list using space as the default delimiter. This is the simplest and most commonly used approach ? Example test_string = "Tutorials point is a learning platform" # Display original string print("The ...
Read MoreCount positive and negative numbers in a list in Python program
In this article, we will learn how to count positive and negative numbers in a Python list using different approaches. We'll explore various methods from simple loops to more advanced functional programming techniques. Problem statement − We are given a list, we need to count positive and negative numbers in it and display them. Using For Loop The most straightforward approach is to iterate through each element and check if it's positive or negative using a for loop ? numbers = [1, -2, -4, 6, 7, -23, 45, 0] pos_count, neg_count = 0, 0 ...
Read More