Found 10476 Articles for Python

Python program to print all even numbers in a range

Pavitra
Updated on 27-Sep-2019 08:01:48

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a range, we need to print all the even numbers in the given range.The brute-force approach is discussed below −Here we apply a range-based for loop which provides all the integers available in the input interval.After this, a check condition for even numbers is applied to filter all the odd numbers.This approach takes O(n) + constant time of comparison.Now let’s see the implementation below −Examplestart, end = 10, 29 # iteration for num in range(start, end + 1):    # check   ... Read More

Python Program to find whether a no is the power of two

Pavitra
Updated on 27-Sep-2019 07:56:11

250 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a number n, we need to check whether the given number is a power of two.ApproachContinue dividing the input number by two, i.e, = n/2 iteratively.We will check In each iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2.If n becomes 1 then it is a power of 2.Let’s see the implementation below −Exampledef isPowerOfTwo(n):    if (n == 0):       return False    while (n != 1):       ... Read More

Python program to find the most occurring character and its count

Pavitra
Updated on 26-Sep-2019 14:31:24

479 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an input string we need to find the most occurring character and its count.ApproachCreate a dictionary using Counter method having strings as keys and their frequencies as values.Find the maximum occurrence of a character i.e. value and get the index of it.Now let’s see the implementation below −Examplefrom collections import Counter    def find(input_):    # dictionary    wc = Counter(input_)    # Finding maximum occurrence    s = max(wc.values())    i = wc.values().index(s)    print (wc.items()[i]) # Driver program if __name__ ... Read More

Python program to find the highest 3 values in a dictionary

Pavitra
Updated on 04-Jul-2020 12:44:08

3K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a dictionary, we need to find the three highest valued values and display them.Approach 1 − Using the collections module ( Counter function )Example Live Demofrom collections import Counter # Initial Dictionary my_dict = {'t': 3, 'u': 4, 't': 6, 'o': 5, 'r': 21} k = Counter(my_dict) # Finding 3 highest values high = k.most_common(3) print("Dictionary with 3 highest values:") print("Keys: Values") for i in high:    print(i[0], " :", i[1], " ")OutputDictionary with 3 highest values: Keys: Values r : 21 t ... Read More

Python program to find sum of elements in list

Akshitha Mote
Updated on 07-Jan-2025 11:14:14

1K+ Views

In this article, lets see try to find the sum of elements in a list. For a given integer list, the program should return the sum of all elements of the list. For an example let's consider list_1 = [1, 2, 3, 4, 5] the output of the program should be 15. Following are the different types of approaches to find the sum of elements in a list − Iterating through loops Using Recursion Using sum() function By ... Read More

Python Program to find the sum of array

Pavitra
Updated on 26-Sep-2019 14:11:29

922 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array as an input, we need to compute the sum of the given array.Here we may follow the brute-force approach i.e. traversing over a list and adding each element to an empty sum variable. Finally, we display the value of the sum.We can also perform an alternate approach using built-in sum function as discussed below.Example# main arr = [1, 2, 3, 4, 5] ans = sum(arr, n) print ('Sum of the array is ', ans)Output15 All the variables & functions are ... Read More

Python program to find sum of absolute difference between all pairs in a list

Pavitra
Updated on 26-Sep-2019 13:57:48

350 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a list input , we need to find the sum of absolute difference between all pairs in a list.Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object type.In this method, we have a list ‘diffs’ which contains the absolute difference.We use two loops having two variables initialized . One is to iterate through the counter and another for the list element. In every iteration, we check whether the elements are similar or not.If not, ... Read More

Python program to find largest number in a list

Pavitra
Updated on 04-Jul-2020 12:37:12

303 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven list input, we need to find the largest numbers in the given list .Here we will discuss two approachesUsing sorting techniquesUsing built-in max() functionApproach 1 − Using built-in sort() functionExample Live Demolist1 = [18, 65, 78, 89, 90] list1.sort() # main print("Largest element is:", list1[-1])OutputLargest element is: 90Approach 2 − Using built-in max() functionExample Live Demolist1 = [18, 65, 78, 89, 90] # main print("Largest element is:",max(list1))OutputLargest element is: 90ConclusionIn this article, we learnt about the approach to find largest number in a list.

Python program to convert decimal to binary number

Pavitra
Updated on 04-Jul-2020 12:41:01

1K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a number we need to convert into a binary number.Approach 1 − Recursive SolutionDecToBin(num):    if num > 1:       DecToBin(num // 2)       print num % 2Exampledef DecimalToBinary(num):    if num > 1:       DecimalToBinary(num // 2)    print(num % 2, end = '') # main if __name__ == '__main__':    dec_val = 35    DecimalToBinary(dec_val)Output100011All the variables and functions are declared in the global scope as shown below −Approach 2 − Built-in SolutionExample Live Demodef ... Read More

Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array

Pavitra
Updated on 26-Sep-2019 12:33:11

287 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array input of integers, we need to find whether it’s possible to make an integer using all the digits available in these numbers such that it would be divisible by 3.Here we will generate a function that will take two arguments namely the array of integers and the length of the array.The implementation given below works on the concept from the mental mathematics. Here we observe that a number is divisible by 3 if the sum of the digits are divisible ... Read More

Advertisements