Programming Articles - Page 1327 of 3366

Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively

AmitDiwan
Updated on 12-Mar-2021 13:04:59

763 Views

When it is required to check the number of times a given letter occurs in a string using recursion, a method can be defined, and an ‘if’ condition can be used.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same − Live Demodef check_frequency(my_str, my_ch):    if not my_str:       return 0    elif my_str[0]==my_ch:       return 1+check_frequency(my_str[1:], my_ch)    else:       return check_frequency(my_str[1:], my_ch) my_string = input("Enter the string :") my_char = input("Enter the character ... Read More

Python Program to Determine Whether a Given Number is Even or Odd Recursively

AmitDiwan
Updated on 12-Mar-2021 13:03:21

879 Views

When it is required to check if a given number is an odd number or an even number using recursion, recursion can be used.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same − Live Demodef check_odd_even(my_num):    if (my_num < 2):       return (my_num % 2 == 0)    return (check_odd_even(my_num - 2)) my_number = int(input("Enter the number that needs to be checked:")) if(check_odd_even(my_number)==True):    print("The number is even") else:    print("The number is odd!")OutputEnter the number that needs ... Read More

Python Program that Displays which Letters are in the Two Strings but not in Both

AmitDiwan
Updated on 12-Mar-2021 13:01:30

297 Views

When it is required to display the letters that occur in both strings separately, but are not repeated, the user input is taken, and the ‘list’ and ‘set’ are used to achieve the same.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). The ‘list’ method converts a given iterable to a list type.Python comes with a datatype known as ‘set’. This ‘set’ contains elements that are unique only.The set is useful in performing operations such as intersection, difference, union and symmetric difference.ExampleBelow is a demonstration for ... Read More

Python Program that Displays which Letters are in the First String but not in the Second

AmitDiwan
Updated on 12-Mar-2021 13:00:07

477 Views

When it is required to display the letters that are present in the first string but not in the second string, two string inputs are taken from user. The ‘set’ is used to find the difference between the two strings.Python comes with a datatype known as ‘set’. This ‘set’ contains elements that are unique only.The set is useful in performing operations such as intersection, difference, union and symmetric difference.ExampleBelow is a demonstration for the same − Live Demomy_str_1 = input("Enter the first string...") my_str_2 = input("Enter the second string...") my_result = list(set(my_str_1)-set(my_str_2)) print("The letters in first string but not in second ... Read More

Python Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character

AmitDiwan
Updated on 12-Mar-2021 12:54:12

2K+ Views

When it is required to create a dictionary with the key as the first character and the associated value as the word which is the starting of that character, the s’plit’ method, a dictionary and simple ‘if’ condition is used.ExampleBelow is a demonstration for the same − Live Demomy_string=input("Enter the string :") split_string = my_string.split() my_dict={} for elem in split_string:    if(elem[0] not in my_dict.keys()):       my_dict[elem[0]]=[]       my_dict[elem[0]].append(elem)    else:       if(elem not in my_dict[elem[0]]):          my_dict[elem[0]].append(elem) print("The dictionary created is") for k, v in my_dict.items():    print(k, ":", v)OutputEnter the ... Read More

Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary

AmitDiwan
Updated on 12-Mar-2021 12:51:19

7K+ Views

When it is required to count the frequency of words that appear in a string with the help of dictionary, the ‘split’ method is used to split the values, and a list comprehension is used.The list comprehension is a shorthand to iterate through the list and perform operations on it.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).ExampleBelow is a demonstration for the same − Live Demomy_string = input("Enter the string :") my_list=[] my_list=my_string.split() word_freq=[my_list.count(p) for p in my_list] print("The frequency of words is ...") print(dict(zip(my_list, word_freq)))OutputEnter ... Read More

Python Program to Form a Dictionary from an Object of a Class

AmitDiwan
Updated on 12-Mar-2021 12:49:57

3K+ Views

When it is required to form a dictionary with the help of an object and class, a class is defined. An ‘init’ function is defined, that assigns values to variables. An instance of the class is created, and the init function is called.ExampleBelow is a demonstration for the same − Live Democlass base_class(object):    def __init__(self):       self.A = 32       self.B = 60 my_instance = base_class() print("An instance of the class has been created") print(my_instance.__dict__)OutputAn instance of the class has been created {'A': 32, 'B': 60}ExplanationA ‘base_class’ is defined, and an object is passed to it.An ... Read More

Python Program to Multiply All the Items in a Dictionary

AmitDiwan
Updated on 12-Mar-2021 12:48:43

872 Views

When it is required to multiply all the elements in a dictionary, the key values in the dictionary are iterated over. The key is multiplied with the previous key, and output is determined.The dictionary is a set of key-value pairs.ExampleBelow is a demonstration for the same − Live Demomy_dict = {'Jane':99, 'Will':54, 'Mark':-3} my_result = 2 for key in my_dict:    my_result = my_result * my_dict[key] print("The reuslt of multiplying keys in a dictionary is : ") print(my_result)OutputThe reuslt of multiplying keys in a dictionary is : -32076ExplanationA dictionary is defined.A variable is assigned a certain value.The ‘key’ in the dictionary ... Read More

Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x,x*x).

AmitDiwan
Updated on 12-Mar-2021 12:47:14

2K+ Views

When it is required to generate a dictionary that contains numbers within a given range in a specific form, the input is taken from the user, and a simple ‘for’ loop is used.ExampleBelow is a demonstration for the same − Live Demomy_num = int(input("Enter a number.. ")) my_dict = dict() for elem in range(1, my_num+1):    my_dict[elem] = elem*elem print("The generated elements of the dictionary are : ") print(my_dict)OutputEnter a number.. 7 The generated elements of the dictionary are : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}ExplanationThe number is taken as user input.An ... Read More

Python Program to Concatenate Two Dictionaries Into One

AmitDiwan
Updated on 12-Mar-2021 12:44:07

2K+ Views

When it is required to concatenate two dictionaries into a single entity, the ‘update’ method can be used.A dictionary is a ‘key-value’ pair.Below is a demonstration for the same −Example Live Demomy_dict_1 = {'J':12, 'W':22} my_dict_2 = {'M':67} print("The first dictionary is :") print(my_dict_1) print("The second dictionary is :") print(my_dict_2) my_dict_1.update(my_dict_2) print("The concatenated dictionary is :") print(my_dict_1)OutputThe first dictionary is : {'J': 12, 'W': 22} The second dictionary is : {'M': 67} The concatenated dictionary is : {'J': 12, 'W': 22, 'M': 67}ExplanationTwo dictionaries are defined, and are displayed on the console. The ‘update’ method is called on the first dictionary by ... Read More

Advertisements