Found 10784 Articles for Python

Python program to print even length words in a string

Harshit Sachan
Updated on 13-Oct-2022 11:58:05

5K+ Views

In Python Programming Language, Length is the concept of calculating the entire range of the string that begins from the first character of the string till the last character of the string. Printing and calculating even length in a string is a basic exercise in the programing language. In this tutorial, we will learn about the solution and approach to find out all words in a string having even length. A number is considered odd if it divides by 2 evenly, i.e. leaving no remainder. This property should hold for the length of the words we need. So the ... Read More

Python program to print all odd numbers in a range

Pavitra
Updated on 27-Sep-2019 08:05:27

749 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 odd 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 odd numbers is applied to filter all the even 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 print all even numbers in a range

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

1K+ 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

156 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

408 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

2K+ 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

Pavitra
Updated on 04-Jul-2020 12:45:28

433 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an list as an input, we need to compute the sum of the given list.Here we have two approaches to consider i.e. using built-in function & using the brute-force approach.Approach 1 − Using built-in functionExample Live Demo# main arr = [1, 2, 3, 4, 5] ans = sum(arr) print ('Sum of the array is ', ans)Output15All the variables & functions are declared in the global scope and are shown below.Approach 2 − Using brute-force methodExample Live Demototal = 0 # creating a list list1 ... Read More

Python Program to find the sum of array

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

772 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

263 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

234 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.

Advertisements