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 359 of 855
Python - Check if a number and its double exists in an array
When it is required to check if a number and its double exists in an array, we can iterate through the array and check if double of each element exists. Python provides multiple approaches to solve this problem efficiently. Method 1: Using Nested Loops This approach iterates through each element and checks if its double exists in the remaining elements ? def check_double_exists(my_list): for i in range(len(my_list)): for j in (my_list[:i] + my_list[i+1:]): ...
Read MorePython Pandas – Remove numbers from string in a DataFrame column
To remove numbers from strings in a DataFrame column, we can use the str.replace() method with regular expressions. This is useful for cleaning data where numbers need to be stripped from text fields. Basic Setup First, let's import pandas and create a sample DataFrame with student records ? import pandas as pd # Create DataFrame with student records dataFrame = pd.DataFrame({ "Id": ['S01', 'S02', 'S03', 'S04', 'S05', 'S06', 'S07'], "Name": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', ...
Read MorePython - Find the number of prime numbers within a given range of numbers
When it is required to find the prime numbers within a given range of numbers, the range is entered and it is iterated over. The % modulus operator is used to check divisibility and identify prime numbers. What is a Prime Number? A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example, 2, 3, 5, 7, 11 are prime numbers. Basic Approach Below is a demonstration of finding prime numbers in a range ? lower_range = 670 upper_range = 699 print("The lower ...
Read MorePython - Given an integer list, find the third maximum number if it exists
Finding the third maximum number in an integer list requires tracking the three largest distinct values. Python provides several approaches: using a tracking algorithm, built-in functions, or set operations to handle duplicates. Using Tracking Variables This approach maintains three variables to track the largest, second largest, and third largest values ? def third_max_num(numbers): result = [float('-inf'), float('-inf'), float('-inf')] for num in numbers: if num not in result: ...
Read MorePython - Find the length of the last word in a string
Finding the length of the last word in a string is a common string manipulation task in Python. There are several approaches to solve this problem, from custom iteration to using built-in string methods. Method 1: Using Custom Iteration This approach removes extra spaces and iterates through the string to count characters of the last word ? def last_word_length(my_string): init_val = 0 processed_str = my_string.strip() for i in range(len(processed_str)): if processed_str[i] ...
Read MorePython Pandas - Display unique values present in each column
To display unique values in each column, use the unique() method and set the column within it. At first, import the required library − import pandas as pd Create a DataFrame with two columns and duplicate records − import pandas as pd dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } ) print("DataFrame ...") print(dataFrame) ...
Read MorePython Program to accept string starting with vowel
When it is required to accept strings that start with a vowel, Python's startswith() method can be used to check if a string begins with specific characters. This method is particularly useful for filtering strings based on their first character. Basic Example Below is a demonstration of filtering strings that start with vowels ? my_list = ["Hi", "there", "how", "are", "u", "doing"] print("The list is:") print(my_list) my_result = [] vowels = "aeiou" for word in my_list: flag = False for ...
Read MorePython - Find words greater than given length
When it is required to find words that are greater than a specific length, we can define a method that splits the string and iterates through it. It checks the length of each word and compares it with the given length. If the word length is greater, it is added to the result. Using a Custom Function Below is a demonstration using a custom function ? def string_check(string_length, my_string): result_string = [] words = my_string.split(" ") for x in words: ...
Read MorePython Program to check if String contains only Defined Characters using Regex
When it is required to check if a given string contains only specific characters using regular expression, a regular expression pattern is defined, and the string is subjected to follow this pattern. Understanding the Regex Pattern The pattern ^[Python]+$ breaks down as follows: ^ − Start of string [Python] − Character set containing P, y, t, h, o, n + − One or more occurrences $ − End of string Example Below is a demonstration of the same − import re def check_string(my_string, regex_pattern): if re.search(regex_pattern, ...
Read MorePython Program to accept string ending with alphanumeric character
When it is required to check if a string ends with an alphanumeric character or not, regular expressions can be used. An alphanumeric character is any letter (a-z, A-Z) or digit (0-9). This tutorial demonstrates how to create a function that validates whether a string's last character is alphanumeric. Using Regular Expressions The re.search() method can match patterns at the end of a string using the $ anchor ? import re regex_expression = '[a-zA-Z0-9]$' def check_string(my_string): if re.search(regex_expression, my_string): print("The string ends ...
Read More