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
Programming Articles
Page 40 of 2547
Python – 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 MoreCategorizing input data in Python lists
Categorizing input data in Python lists means separating list elements by their data types (strings, numbers, etc.). Python provides several built-in functions like isinstance(), type(), and filter() to accomplish this task efficiently. What is Data Categorization? Data categorization involves grouping list elements based on their data types. For example, separating strings from numbers in a mixed list ? mixed_data = ["book", 23, "note", 56, "pen", 129, 34.5] print("Original list:", mixed_data) Original list: ['book', 23, 'note', 56, 'pen', 129, 34.5] Using isinstance() with filter() The isinstance() function checks if an object ...
Read MorePython – Alternate rear iteration
Alternate rear iteration means traversing a list from the end to the beginning while selecting every alternate element. Python provides several approaches using range(), slicing, and lambda functions to achieve this efficiently. What is Alternate Rear Iteration? In alternate rear iteration, we start from the last element of a list and move backwards, picking every second element. For example, in the list [1, 2, 3, 4, 5, 6, 8], we would get [8, 5, 3, 1]. Using range() with Negative Step The range() function allows us to iterate backwards with a step of −2 to get ...
Read MoreHow to Find the average of two list using Python?
Finding the average of two lists is a common task in data analysis and Python programming. Python provides several approaches including the statistics module, NumPy arrays, and basic arithmetic operations. Using the statistics Module The statistics.mean() function provides a straightforward way to calculate the average of combined lists ? import statistics # Initialize two lists numbers_1 = [56, 34, 90] numbers_2 = [23, 87, 65] # Combine lists and find average using statistics.mean() avg_of_lists = statistics.mean(numbers_1 + numbers_2) print("Average of two lists:", avg_of_lists) Average of two lists: 59.166666666666664 ...
Read More