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
Server Side Programming Articles
Page 14 of 2109
Program to Print K using Alphabets
Python provides various ways to create letter patterns using text characters. The letter K can be printed using different approaches and alphabets from the English language. Method 1: Using 'K' Character with Mathematical Logic This approach uses mathematical conditions to determine where to place the 'K' character ? string = "" j = 7 i = 0 for Row in range(0, 10): for Col in range(0, 10): if (Col == 1 or ...
Read MoreProgram to print window pattern
A window pattern is a visual representation that resembles a window frame using characters like asterisks (*) or plus signs (+). Python makes it easy to create such patterns using loops and string operations. Simple Rectangular Window Let's start with a basic rectangular window without any divisions ? def print_window(n): # Print the top row print("+" * (2 * n + 1)) # Print the middle rows for i in range(n - 1): ...
Read MorePython - Unique Values Multiplication
Python lists allow duplicate values, which is useful in most cases. However, sometimes we need to remove duplicates and perform operations on unique values only. In this article, we'll explore multiple approaches to find unique values from a list and calculate their multiplication. Using set() to Remove Duplicates The set() function creates an unordered collection with no duplicate elements, making it perfect for extracting unique values ? def calculate_product(numbers): result = 1 for num in numbers: result *= num ...
Read MorePython - Unique Tuple Frequency (Order Irrespective)
In this article, we will find the frequency of unique tuples in a list where order doesn't matter. This means tuples like (1, 2, 3) and (1, 3, 2) are considered identical since they contain the same elements. Problem Understanding Input data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)] print("Input:", data) Input: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)] Expected Output Frequency of unique tuples = 2 Explanation: Tuples at indices 0, ...
Read MorePython - Uneven Sized Matrix Column Minimum
In Python, when dealing with matrices of uneven row lengths, finding the minimum values in each column requires special handling. This article explores seven different methods to tackle this problem, from basic loops to advanced libraries like NumPy and Pandas. You'll learn how to handle uneven-sized matrices and extract column-wise minimum values efficiently using various approaches. Using Nested Loops This method iterates through the matrix using nested loops and tracks the minimum value for each column. It's straightforward but may be slower for large datasets ? matrix = [ [3, 8, ...
Read MorePython - Tuple value product in dictionary
Dictionaries in Python are widely used to store data in key-value pairs. Sometimes we need to calculate the product of elements at corresponding positions across tuple values in a dictionary. This commonly arises in data manipulation and analysis scenarios. Problem Statement Given a dictionary with tuples as values, we want to multiply elements at the same index positions across all tuples. Input input_dict = {'a': (1, 3, 5, 7), 'b': (2, 4, 6, 8), 'c': (2, 3, 5, 7)} print("Input:", input_dict) Input: {'a': (1, 3, 5, 7), 'b': (2, 4, 6, 8), 'c': ...
Read MoreHow to Invert Python Tuple Elements?
Python tuples store data in the form of individual elements with a fixed order. In this article, we'll explore various methods to invert (reverse) the order of tuple elements ? Sample Input and Output Input (5, 6, 7, 8) Output (8, 7, 6, 5) Using Tuple Slicing The most Pythonic way uses slice notation with step -1 to reverse the tuple ? original_tuple = (1, 2, 3, 4, 5) inverted_tuple = original_tuple[::-1] print("Original tuple:", original_tuple) print("Inverted tuple:", inverted_tuple) Original tuple: (1, 2, 3, 4, 5) ...
Read MoreConvert Lists into Similar key value lists in Python
Converting two separate lists into a key-value mapping is a common data processing task in Python. The first list serves as keys, while the second list provides values. When keys repeat, their corresponding values are grouped together into lists. Example Input and Output keys = [3, 4, 3, 4, 5, 5] values = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig'] # Expected output: # {3: ['apple', 'cherry'], 4: ['banana', 'date'], 5: ['elderberry', 'fig']} Using defaultdict with zip() The most efficient approach uses defaultdict to automatically create empty lists for new keys ? ...
Read MoreDivide one Hermite series by another in Python using NumPy
The Hermite series is a mathematical technique used to represent infinite series of Hermite polynomials. Hermite polynomials are orthogonal polynomials that solve the Hermite differential equation. NumPy provides functions to work with Hermite series, including division operations. What is a Hermite Series? A Hermite series is represented by the equation: f(x) = Σn=0^∞ cn Hn(x) Where: Hn(x) is the nth Hermite polynomial cn is the nth coefficient in the expansion Creating Hermite Series First, let's create Hermite series using NumPy's polynomial.hermite.poly2herm() function − import numpy as np from numpy.polynomial ...
Read MoreDivide a DataFrame in a ratio
Pandas DataFrames often need to be divided into smaller parts based on specific ratios for tasks like train-test splits in machine learning. Python provides several methods to split DataFrames proportionally using different approaches. There are three main ways to divide DataFrame data based on ratio: Using np.random.rand() Using pandas.DataFrame.sample() Using numpy.split() Using numpy.random.rand() This method creates random values for each row and filters based on a threshold. For a 60-40 split, we use 0.6 as the threshold ? Syntax import numpy as np ratio = np.random.rand(len(dataframe)) part1 = dataframe[ratio < threshold] ...
Read More