Found 10476 Articles for Python

Python program to find the second maximum value in Dictionary

Pavitra
Updated on 11-Jul-2020 11:28:51

703 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given two integers, we need to print the second maximum value in the dictionaryNow let’s observe the concept in the implementation below−Approach 1 − Using sorted() function by negative indexesExample Live Demo#input example_dict ={"tutor":3, "tutorials":15, "point":9, "tutorialspoint":19} # sorting the given list and get the second last element print(list(sorted(example_dict.values()))[-2])Output15Approach 2 − Here we use sort method on the list and then access the second largest elementExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # using built-in sort method list1.sort() # ... Read More

Python program to find the second largest number in a list

Pavitra
Updated on 11-Jul-2020 11:30:42

4K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to display the second largest number in a list.There are three approaches to solve the problem−Approach 1 − We use the set() function & remove() functionExample Live Demolist1 = [11, 22, 1, 2, 5, 67, 21, 32] # to get unique elements new_list = set(list1) # removing the largest element from list1 new_list.remove(max(new_list)) # now computing the max element by built-in method? print(max(new_list))Output32Approach 2 − We use sort() method and negative indexesExample Live Demolist1 = [11, 22, 1, 2, ... Read More

Python program to find occurrence to each character in given string

Pavitra
Updated on 11-Jul-2020 11:31:49

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string, we need to find the occurrence of each character in a given string.Here we will be discussing 3 approaches as discussed below:LApproach 1 − The brute-force approachExample Live Demotest_str = "Tutorialspoint" #count dictionary count_dict = {} for i in test_str:    #for existing characters in the dictionary    if i in count_dict:       count_dict[i] += 1    #for new characters to be added    else:       count_dict[i] = 1 print ("Count of all characters in Tutorialspoint ... Read More

Python program to find the largest number in a list

Pavitra
Updated on 23-Dec-2019 08:09:45

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to calculate the largest element of the list.Here we will take the help of built-in functions to reach the solution of the problem statementUsing sort() functionExample# list list1 = [23, 1, 32, 67, 2, 34, 12] # sorting list1.sort() # printing the last element print("Largest element is:", list1[-1])OutputLargest in given array is 67Using max() functionExample Live Demo# list list1 = [23, 1, 32, 67, 2, 34, 12] # printing the maximum element print("Largest element is:", max(list1))OutputLargest in given ... Read More

Python Program to find the largest element in an array

Pavitra
Updated on 23-Dec-2019 08:07:18

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to calculate the largest element of the array.Here we use the bruteforce approach in which we compute the largest element by traversing the whole loop and get the element.We can observe the implementation below.Example Live Demo# largest function def largest(arr, n):    #maximum element    max = arr[0]    # traverse the whole loop    for i in range(1, n):       if arr[i] > max:          max = arr[i]    return max # ... Read More

Python program to create a dictionary from a string

Pavitra
Updated on 11-Jul-2020 11:32:35

658 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string input, we need to convert it into dictionary typeHere we will discuss two methods to solve the problem without using a built-in dict() function.Method 1 − Using eval() methodEval method is used only when the syntax or the formation of string resembles that of a dictionary. Direct conversion of string to the dictionary can happen in that case as discussed below.Example Live Demo# String string = "{'T':1, 'U':2, 'T':3, 'O':4, 'R':5}" # eval() function dict_string = eval(string) print(dict_string) print(dict_string['T']) print(dict_string['T'])Output{'T': ... Read More

Count words in a sentence in Python program

Pavitra
Updated on 25-Aug-2023 02:03:41

36K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string we need to count the number of words in the stringApproach 1 − Using split() functionThe split() function breaks the string into a list iterable with space as a delimiter. if the split() function is used without specifying the delimiter space is allocated as a default delimiter.Example Live Demotest_string = "Tutorials point is a learning platform" #original string print ("The original string is : " + test_string) # using split() function res = len(test_string.split()) # total no of words print ... Read More

Count upper and lower case characters without using inbuilt functions in Python program

Pavitra
Updated on 23-Dec-2019 07:54:17

705 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a string, we need to count the number of uppercase and lowercase characters present in the string without using the inbuilt functionThis can be easily solved by using islower() and isupper() function available in python. But here there is a constraint to use the inbuilt function. So here we take the help of the ASCII value of the characters.Using the ord() function we compute the ASCII value of each character present in the string and then compare to check for uppercase ... Read More

Count positive and negative numbers in a list in Python program

Pavitra
Updated on 11-Jul-2020 11:24:45

4K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list iterable, we need to count positive and negative numbers in it and display them.Approach 1 − Brute-force approach using iteration construct(for)Here we need to iterate each element in the list using a for loop and check whether num>=0, to filter the positive numbers. If the condition evaluates to be true, then increase pos_count otherwise, increase neg_count.Example Live Demolist1 = [1, -2, -4, 6, 7, -23, 45, -0] pos_count, neg_count = 0, 0 # enhanced for loop   for num in ... Read More

Python program to Count Even and Odd numbers in a List

Pavitra
Updated on 11-Jul-2020 11:25:39

6K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to count even and odd numbers in a list.There are three methods as discussed below−Approach 1 − Using brute-force approachExample Live Demolist1 = [21, 3, 4, 6, 33, 2, 3, 1, 3, 76] even_count, odd_count = 0, 0 # enhanced for loop for num in list1:    #even numbers    if num % 2 == 0:       even_count += 1    #odd numbers    else:       odd_count += 1 print("Even numbers available in the ... Read More

Advertisements