Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Server Side Programming Articles - Page 1186 of 2646
8K+ 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
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
915 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).
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
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
737 Views
When it is required to reverse a string without using recursion technique, simple negative indexing can be used.Indexing helps the values to access the elements at a specific index.ExampleBelow is a demonstration for the same − Live Demomy_string = str(input("Enter a string that needs to be reversed: ")) print("The string after reversal is: ") print(my_string[::-1])OutputEnter a string that needs to be reversed: Jane The string after reversal is: enaJExplanationThe string input is taken by the user.This value is assigned to a variable.It is displayed on the console.It is negative indexed and displayed on the console.Read More
275 Views
When it is required to flatten a list without using recursion technique, the lambda function, ‘sum’ method, ‘map’ method and the ‘isinstance’ method can be used.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 ‘isinstance’ method checks to see if a given parameter belong to a specific data type or not.Anonymous function is a function which is defined without a name. In general, functions in Python are defined using ‘def’ keyword, but anonymous function is defined with the help of ‘lambda’ keyword. It takes a single ... Read More
6K+ Views
When it is required to find the factorial of a number without using recursion, the ‘while’ loop can be used.ExampleBelow is a demonstration for the same − Live Demomy_num = int(input("Enter a number :")) my_factorial = 1 while(my_num>0): my_factorial = my_factorial*my_num my_num=my_num-1 print("The factorial of the number is : ") print(my_factorial)OutputEnter a number :7 The factorial of the number is : 5040ExplanationThe input number is takne from the user.A variable is assigned to 1.It is checked to see for being 0.If not, it is multiplied by the previous value in the variable.It is assigned to the same variable.This is ... Read More
5K+ Views
When it is required to find the Fibonacci series without using recursion technique, the input is taken from the user, and a ‘while’ loop is used to get the numbers in the sequence.ExampleBelow is a demonstration for the same − Live Demofirst_num = int(input("Enter the first number of the fibonacci series... ")) second_num = int(input("Enter the second number of the fibonacci series... ")) num_of_terms = int(input("Enter the number of terms... ")) print(first_num, second_num) print("The numbers in fibonacci series are : ") while(num_of_terms-2): third_num = first_num + second_num first_num=second_num second_num=third_num print(third_num) num_of_terms=num_of_terms-1OutputEnter the first number of the ... Read More
2K+ Views
When it is required to find the length of a list with the help of recursion technique, a user defined method is used, and simple indexing technique is used.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 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 list_length(my_list): if not my_list: return 0 return 1 + list_length(my_list[1::2]) + list_length(my_list[2::2]) my_list = ... Read More