Found 26504 Articles for Server Side Programming

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

AmitDiwan
Updated on 20-Sep-2021 10:59:47

384 Views

When it is required to find the third maximum in a list of integers, a method is defined that takes a list as a parameter. It initializes a list of floating point numbers to infinity. The values in the list are iterated over, and compared with infinite values. Depending on the result, the output is displayed on the console.ExampleBelow is a demonstration of the samedef third_max_num(my_num): my_result = [float('-inf'), float('-inf'), float('-inf')] for num in my_num: if num not in my_result: ... Read More

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

AmitDiwan
Updated on 20-Sep-2021 10:57:00

2K+ Views

When it is required to find the length of the last word in a string, a method is defined that removes the extra empty spaces in a string, and iterates through the string. It iterates until the last word has been found. Then, its length is found and returned as output.ExampleBelow is a demonstration of the samedef last_word_length(my_string): init_val = 0 processed_str = my_string.strip() for i in range(len(processed_str)): if processed_str[i] == " ": init_val ... Read More

Python Pandas - Display unique values present in each column

AmitDiwan
Updated on 20-Sep-2021 11:00:41

544 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 pdCreate a DataFrame with two columns and duplicate records −dataFrame = pd.DataFrame(    {       "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] } )Find unique values by setting each column in the unique() method −resStudent = pd.unique(dataFrame.Student) resResult = pd.unique(dataFrame.Result)ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame(    {       ... Read More

Python Pandas - Fill NaN with Polynomial Interpolation

AmitDiwan
Updated on 20-Sep-2021 10:54:05

888 Views

To fill NaN with Polynomial Interpolation, use the interpolate() method on the Pandas series. With that, set the “method” parameter to “polynomial”.At first, import the required libraries −import pandas as pd import numpy as npCreate a Pandas series with some NaN values. We have set the NaN using the numpy np.nan −d = pd.Series([10, 20, np.nan, 65, 75, 85, np.nan, 100]) Find polynomial interpolation using the method parameter of the interpolate() method −d.interpolate(method='polynomial', order=2)ExampleFollowing is the code −import pandas as pd import numpy as np # pandas series d = pd.Series([10, 20, np.nan, 65, 75, 85, np.nan, 100]) ... Read More

Python Program to accept string starting with vowel

AmitDiwan
Updated on 20-Sep-2021 10:54:51

2K+ Views

When it is required to accept a string that starts with a vowel, a ‘startswith’ function is used to check if the string begins with a specific character (vowel) or not.ExampleBelow is a demonstration of the samemy_list = ["Hi", "there", "how", "are", "u", "doing"] print("The list is : ") print(my_list) my_result = [] vowel = "aeiou" for sub in my_list: flag = False for letter in vowel: if sub.startswith(letter): flag = True ... Read More

Python - Find words greater than given length

AmitDiwan
Updated on 20-Sep-2021 10:52:51

1K+ Views

When it is required to find words that are greater than a specific length, a method is defined that splits the string, and iterates through it. It checks the length of the word and compares it with the given length. If they match, it is returned as output.ExampleBelow is a demonstration of the samedef string_check(string_length, my_string):    result_string = []    words = my_string.split(" ")    for x in words:       if len(x) > string_length:          result_string.append(x)    return result_string string_length = 3 my_string ="Python is always fun to learn" print("The string is :") ... Read More

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

AmitDiwan
Updated on 20-Sep-2021 10:43:00

471 Views

When it is required to check if a given string contains specific characters using regular expression, a regular expression pattern is defined, and the string is subjected to follow this pattern.ExampleBelow is a demonstration of the sameimport re def check_string(my_string, regex_pattern): if re.search(regex_pattern, my_string): print("The string contains the defined characters only") else: print("The doesnot string contain the defined characters") regex_pattern = re.compile('^[Python]+$') my_string_1 = 'Python' print("The string is :") print(my_string_1) check_string(my_string_1 , regex_pattern) my_string_2 = 'PythonInterpreter' print("The string ... Read More

Python Program to accept string ending with alphanumeric character

AmitDiwan
Updated on 20-Sep-2021 10:41:24

629 Views

When it is required to check if a string ends with an alphanumeric character or not, the regular expression is used. A method is defined that checks to see an alphanumeric characters, and returns the string as output.ExampleBelow is a demonstration of the sameimport re regex_expression = '[a-zA-z0-9]$' def check_string(my_string): if(re.search(regex_expression, my_string)): print("The string ends with alphanumeric character") else: print("The string doesnot end with alphanumeric character") my_string_1 = "Python@" print("The string is :") print(my_string_1) check_string(my_string_1) ... Read More

Python - Check whether a string starts and ends with the same character or not

AmitDiwan
Updated on 20-Sep-2021 10:39:53

1K+ Views

When it is required to check if a string begins and ends with the same character or not, regular expression can be used. A method can be defined that uses the ‘search’ function to see if a string begins and ends with a specific character.ExampleBelow is a demonstration of the sameimport re regex_expression = r'^[a-z]$|^([a-z]).*\1$' def check_string(my_string): if(re.search(regex_expression, my_string)): print("The given string starts and ends with the same character") else: print("The given string doesnot start and end with the ... Read More

Python Program to check if a string starts with a substring using regex

AmitDiwan
Updated on 20-Sep-2021 10:37:01

465 Views

When it is required to check if a string starts with a specific substring or not, with the help of regular expression, a method is defined that iterates through the string and uses the ‘search’ method to check if a string begins with a specific substring or not.ExampleBelow is a demonstration of the sameimport re def check_string(my_string, sub_string) : if (sub_string in my_string): concat_string = "^" + sub_string result = re.search(concat_string, my_string) if result : ... Read More

Advertisements