Python Articles

Page 49 of 855

Count Number of Lowercase Characters in a String in Python Program

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

When it is required to count the number of lower case characters in a string, the ‘islower’ method and a simple ‘for’ loop can be used.Below is the demonstration of the same −Examplemy_string = "Hi there how are you" print("The string is ") print(my_string) my_counter=0 for i in my_string:    if(i.islower()):       my_counter=my_counter+1 print("The number of lowercase characters in the string are :") print(my_counter)OutputThe string is Hi there how are you The number of lowercase characters in the string are : 15ExplanationA string is defined and is displayed on the console.A counter value is initialized to 0.The ...

Read More

Extract Unique dictionary values in Python Program

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

When it is required to extract unique values from a dictionary, a dictionary is created, and the ‘sorted’ method and dictionary comprehension is used.Below is a demonstration for the same −Examplemy_dict = {'hi' : [5, 3, 8, 0],    'there' : [22, 51, 63, 77],    'how' : [7, 0, 22],    'are' : [12, 11, 45],    'you' : [56, 31, 89, 90]} print("The dictionary is : ") print(my_dict) my_result = list(sorted({elem for val in my_dict.values() for elem in val})) print("The unique values are : ") print(my_result)OutputThe dictionary is : {'hi': [5, 3, 8, 0], 'there': ...

Read More

Python Program to Find if Undirected Graph contains Cycle using BFS

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 515 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Examplefrom collections import deque def add_edge(adj: list, u, v):    adj[u].append(v)    adj[v].append(u) def detect_cycle(adj: list, s, V, visited: list):    parent = [-1] * V ...

Read More

Insertion at the beginning in OrderedDict using Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 579 Views

When it is required to insert the elements at the beginning of an ordered dictionary, the ‘update’ method can be used.Below is the demonstration of the same −Examplefrom collections import OrderedDict my_ordered_dict = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) print("The dictionary is :") print(my_ordered_dict) my_ordered_dict.update({'Mark':'7'}) my_ordered_dict.move_to_end('Mark', last = False) print("The resultant dictionary is : ") print(my_ordered_dict)OutputThe dictionary is : OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) The resultant dictionary is : OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')])ExplanationThe required packages are imported.An ordered dictionary is created using OrderedDict’.It is displayed on the console.The ‘update’ method is used to ...

Read More

Check order of character in string using OrderedDict( ) in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 779 Views

When it is required to check the order of the character in the string, the ‘OrderedDict’ method can be used.Below is the demonstration of the same −Examplefrom collections import OrderedDict def check_order(my_input, my_pattern):    my_dict = OrderedDict.fromkeys(my_input)    pattern_length = 0    for key, value in my_dict.items():       if (key == my_pattern[pattern_length]):          pattern_length = pattern_length + 1       if (pattern_length == (len(my_pattern))):          return 'The order of pattern is correct'    return 'The order of pattern is incorrect' my_input = 'Hi Mark' input_pattern = 'Ma' print("The ...

Read More

Python dictionary with keys having multiple inputs

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

When it is required to create a dictionary where keys have multiple inputs, an empty dictionary can be created, and the value for a specific key can be specified.Below is the demonstration of the same −Examplemy_dict = {} a, b, c = 15, 26, 38 my_dict[a, b, c] = a + b - c a, b, c = 5, 4, 11 my_dict[a, b, c] = a + b - c print("The dictionary is :") print(my_dict)OutputThe dictionary is : {(15, 26, 38): 3, (5, 4, 11): -2}ExplanationThe required packages are imported.An empty dictionary is defined.The values for a specific key are specified.The output is displayed on the console.

Read More

Python counter and dictionary intersection example

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 358 Views

When it is required to demonstrate a counter and dictionary intersection, the Counter and dictionary can be used.Below is the demonstration of the same −Examplefrom collections import Counter def make_string(str_1, str_2):    dict_one = Counter(str_1)    dict_two = Counter(str_2)    result = dict_one & dict_two    return result == dict_one string_1 = 'Hi Mark' string_2 = 'how are yoU' print("The first string is :") print(string_1) print("The second string is :") print(string_2) if (make_string(string_1, string_2)==True):    print("It is possible") else:    print("It is not possible")OutputThe first string is : Hi Mark The second string is : how are ...

Read More

Python dictionary, set and counter to check if frequencies can become same

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 403 Views

When it is required to check if the frequency of a dictionary, set and counter are same, the Counter package is imported and the input is converted into a ‘Counter’. The values of a dictionary are converted to a ‘set’ and then to a list. Based on the length of the input, the output is displayed on the console.Below is the demonstration of the same −Examplefrom collections import Counter def check_all_same(my_input):    my_dict = Counter(my_input)    input_2 = list(set(my_dict.values()))    if len(input_2)>2:       print('The frequencies are not same')    elif len (input_2)==2 and input_2[1]-input_2[0]>1:       print('The ...

Read More

Keys associated with Values in Dictionary in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 470 Views

When it is required to find the keys associated with specific values in a dictionary, the ‘index’ method can be used.Below is the demonstration of the same −Examplemy_dict ={"Hi":100, "there":121, "Mark":189} print("The dictionary is :") print(my_dict) dict_key = list(my_dict.keys()) print("The keys in the dictionary are :") print(dict_key) dict_val = list(my_dict.values()) print("The values in the dictionary are :") print(dict_val) my_position = dict_val.index(100) print("The value at position 100 is : ") print(dict_key[my_position]) my_position = dict_val.index(189) print("The value at position 189 is") print(dict_key[my_position])OutputThe dictionary is : {'Hi': 100, 'there': 121, 'Mark': 189} The keys in the dictionary are : ['Hi', ...

Read More

Maximum and Minimum K elements in Tuple using Python

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

When it is required to find the maximum and minimum K elements in a tuple, the ‘sorted’ method is used to sort the elements, and enumerate over them, and get the first and last elements.Below is the demonstration of the same −Examplemy_tuple = (7, 25, 36, 9, 6, 8) print("The tuple is : ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) my_result = [] my_tuple = list(my_tuple) temp = sorted(my_tuple) for idx, val in enumerate(temp):    if idx < K or idx >= len(temp) - K:       my_result.append(val) ...

Read More
Showing 481–490 of 8,547 articles
« Prev 1 47 48 49 50 51 855 Next »
Advertisements