Python Articles

Page 359 of 855

Python - Check if a number and its double exists in an array

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 648 Views

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 More

Python Pandas – Remove numbers from string in a DataFrame column

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 3K+ Views

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 More

Python - Find the number of prime numbers within a given range of numbers

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 3K+ Views

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 More

Python - Given an integer list, find the third maximum number if it exists

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 461 Views

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 More

Python - Find the length of the last word in a string

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

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 More

Python Pandas - Display unique values present in each column

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 636 Views

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 More

Python Program to accept string starting with vowel

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

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 More

Python - Find words greater than given length

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

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 More

Python Program to check if String contains only Defined Characters using Regex

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 561 Views

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 More

Python Program to accept string ending with alphanumeric character

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 700 Views

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
Showing 3581–3590 of 8,546 articles
« Prev 1 357 358 359 360 361 855 Next »
Advertisements