Programming Articles

Page 15 of 2547

How to Invert Python Tuple Elements?

Pranay Arora
Pranay Arora
Updated on 27-Mar-2026 550 Views

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 More

Convert Lists into Similar key value lists in Python

Pranay Arora
Pranay Arora
Updated on 27-Mar-2026 286 Views

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 More

Divide one Hermite series by another in Python using NumPy

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 192 Views

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 More

Divide a DataFrame in a ratio

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 1K+ Views

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

Digital Band Pass Butterworth Filter in Python

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 2K+ Views

A Band Pass Filter is a filter that passes frequencies within a specific range and rejects frequencies outside this range. The Butterworth band pass filter is designed to have the flattest possible frequency response in the pass band, making it ideal for applications requiring minimal ripple. Filter Specifications The following specifications define a typical digital band pass Butterworth filter: Sampling rate: 40 kHz Pass band edge frequencies: 1400 Hz to 2100 Hz Stop band edge frequencies: 1050 Hz to 2450 Hz Pass band ripple: 0.4 dB Minimum stop band attenuation: 50 dB Implementation Steps ...

Read More

Differentiate Hermite series and multiply each differentiation by scalar using NumPy in Python

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 164 Views

The Hermite_e series (probabilist's Hermite polynomial) is a mathematical function used in quantum mechanics and probability theory. NumPy provides the hermite.hermder() function to differentiate Hermite series and multiply each differentiation by a scalar value. Hermite_e Series Formula The Hermite_e polynomial is defined as: H_n(x) = (−1)^n e^(x²/2) d^n/dx^n(e^(−x²/2)) Where: H_n(x) is the nth Hermite polynomial of degree n x is the independent variable d^n/dx^n denotes the nth derivative with respect to x Syntax The polynomial.hermite.hermder() function syntax is: numpy.polynomial.hermite.hermder(c, m=1, scl=1, axis=0) Parameters: c − Array ...

Read More

Difference between casefold() and lower() in Python

Pranavnath
Pranavnath
Updated on 27-Mar-2026 960 Views

Python provides two similar string methods for converting text to lowercase: casefold() and lower(). While they appear similar, they handle Unicode characters differently, making each suited for specific use cases. Understanding casefold() The casefold() method performs aggressive case folding by converting characters to lowercase and normalizing special Unicode characters. This makes it ideal for case-insensitive comparisons across different languages. Example text = "Déjà Vuß" result = text.casefold() print(result) déjà vuss Notice how the German ß character is converted to "ss" for more accurate comparison. Understanding lower() The lower() ...

Read More

Python calendar module : monthdays2calendar() method

Pranavnath
Pranavnath
Updated on 27-Mar-2026 389 Views

The Python calendar module provides various methods for working with dates and calendars. The monthdays2calendar() method is particularly useful for generating structured calendar layouts that include both day numbers and their corresponding weekday information. What is monthdays2calendar()? The monthdays2calendar() method returns a matrix representing a month's calendar where each day is paired with its weekday number. Unlike monthcalendar() which returns only day numbers, this method provides tuples of (day, weekday) for each position in the calendar grid. Syntax calendar.Calendar().monthdays2calendar(year, month) Parameters year − The year as a four-digit integer month − ...

Read More

Different ways to access Instance Variable in Python

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 808 Views

Instance variables represent the state or attributes of an object in Python. Each instance of a class can have its own set of instance variables with unique values. These variables are defined within class methods and remain accessible throughout the instance's lifespan. Python provides several ways to access instance variables, each serving different purposes and use cases. Let's explore the most common approaches ? Using Dot Notation The most straightforward way to access instance variables is using dot notation. This method directly accesses the variable through the instance name ? instance.variable_name Where instance ...

Read More

Different ways of sorting Python Dictionary by Keys

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 249 Views

Sorting refers to the technique of arranging elements in a defined order. Python provides several methods to sort dictionary keys. This article covers the most common approaches for sorting dictionaries by their keys. Using sorted() Function The sorted() function sorts elements in iterable data structures and returns a new sorted list. For dictionaries, it can sort the keys alphabetically ? data = {"Name": ["John", "Alice", "Bob"], "Age": [25, 30, 35], "City": ["NYC", "LA", "Chicago"]} print("Original dictionary:", data) ...

Read More
Showing 141–150 of 25,466 articles
« Prev 1 13 14 15 16 17 2547 Next »
Advertisements