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 Pranavnath
Page 6 of 39
Blackman in Python Numpy
The Blackman window is a widely used window function in signal processing that helps reduce spectral leakage effects. NumPy provides efficient array operations to implement this window function using its mathematical formula and vectorized operations. In this article, we'll explore three different methods to implement the Blackman window in Python using NumPy. Each approach demonstrates different programming techniques while achieving the same result. Blackman Window Formula The Blackman window is defined by the formula: w(n) = 0.42 - 0.5 * cos(2πn/(N-1)) + 0.08 * cos(4πn/(N-1)) Where n is the sample index (0 to N-1) and ...
Read MoreHow to Append suffix/prefix to strings in a Python list?
In Python, you often need to add prefixes or suffixes to all strings in a list. This is common when formatting data, adding file extensions, or modifying text elements. Python provides several built-in methods like map(), list comprehensions, and reduce() to accomplish this efficiently. Using map() Function The map() function applies a given function to each item in a list. Combined with lambda expressions, it's perfect for adding prefixes and suffixes ? # Original list of strings items = ['note', 'book', 'pen'] suffix = 's' prefix = 'my_' # Adding suffix using map() and lambda ...
Read MorePython – Aggregate values by tuple keys
When working with data in Python, you often need to aggregate values by tuple keys — combining values that share the same tuple identifier. This is useful for grouping data by multiple attributes and performing calculations like sums, averages, or counts. Using defaultdict() Method The defaultdict class from the collections module provides an efficient way to aggregate values by automatically handling missing keys ? from collections import defaultdict # Sample data: (product, cost) tuples item_data = [ ('Milk', 30), ('Tomato', 100), ('Lentils', 345), ...
Read MorePython – Check for descending Sorted list
A list is a data structure in Python that stores elements within square brackets. Lists can be sorted in ascending order (each element is smaller than the next) or descending order (each element is larger than the next). This article demonstrates three methods to check if a list is sorted in descending order. Using Iteration Method This approach iterates through the list and compares each element with the next one. If any element is smaller than the next element, the list is not in descending order ? def is_descending(numbers): # Iterate through ...
Read MorePython – Append K characters N times
In Python, you may need to append a specific character multiple times to a string. This tutorial demonstrates three different approaches to append K characters N times using join(), textwrap(), and reduce() methods. Using join() Method The join() method provides a clean way to append characters by creating a list and joining them ? # Number of times the character has to be repeated N = 6 # Character that has to be repeated K_character = '$' # Original string original_string = 'Python' # Create list of K characters and join them repeated_chars = [K_character ...
Read MoreCharacter Encoding in Python
Character encoding is the process of converting text into bytes that computers can store and process. Python 3 uses Unicode by default and supports various encoding formats, with UTF-8 being the most common. Understanding Character Encoding In character encoding, each character is mapped to a numeric value. For example: C = 67 D = 68 E = 69 Character Number Binary D 68 1000100 UTF-8 Encoding in Python UTF-8 is Python's default encoding method with these characteristics − ASCII characters use one byte (0-127) ...
Read MorePython – Bitwise OR among list elements
The bitwise OR operation combines the binary representations of numbers using the "|" operator. In Python, you can perform bitwise OR among all elements in a list using several approaches including iteration, lambda functions, and NumPy. Understanding Bitwise OR The bitwise OR operator "|" compares each bit position and returns 1 if at least one of the corresponding bits is 1 − # Example: 40 | 50 # 40 in binary: 101000 # 50 in binary: 110010 # Result: 111010 (which is 58 in decimal) print(f"40 | 50 = ...
Read MoreHow to Alternate vowels and consonants in Python Strings?
Alternating vowels and consonants in a string means rearranging characters so that consonants and vowels appear in alternating positions. Python provides several approaches using built-in functions like zip(), lambda functions, and zip_longest(). Using join() and zip() Methods This approach separates vowels and consonants into different lists, then combines them alternately using zip() ? Algorithm Step 1 − Initialize two empty lists for vowels and consonants Step 2 − Iterate through the string to separate vowels and consonants Step 3 − Use zip() to pair consonants with vowels Step 4 − Use join() to create the alternating ...
Read MorePython – Alternate Character Addition
Alternate character addition combines characters from two strings by taking one character from each string in turn. Python provides several approaches to achieve this string manipulation technique. Understanding Alternate Character Addition Given two strings like "Python" and "Golang", alternate character addition creates a new string by combining characters: P+G, y+o, t+l, h+a, o+n, n+g, resulting in "PGyotlhaonng". Using List Comprehension with zip() The most concise approach uses zip() and join() methods ? string1 = "Python" string2 = "Golang" # Combine characters alternately using zip() and join() new_str = ''.join([a + b for a, ...
Read MoreHow to Convert a Python String to Alternate Cases?
Converting a string to alternate cases means changing characters at even positions to lowercase and odd positions to uppercase (or vice versa). Python provides several approaches to achieve this pattern using for loops, join() method, and regular expressions. Using For Loop The most straightforward approach uses a for loop to iterate through each character and apply case conversion based on the index position ? text = "Welcome to Tutorialspoint" result = "" for i in range(len(text)): if i % 2 == 0: result ...
Read More