Write a Python program to find sum of largest two numbers in the given list

Python program to find largest number in a list

Given a list of numbers, the task is to write a Python program to find the largest number in given list.

Examples:

Input : list1 = [10, 20, 4] Output : 20 Input : list2 = [20, 10, 20, 4, 100] Output : 100

Method 1 : Sort the list in ascending order and print the last element in the list.




# Python program to find largest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the last element
print("Largest element is:", list1[-1])

Output:

Largest element is: 99

Method 2 : Using max() method






# Python program to find largest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# printing the maximum element
print("Largest element is:", max(list1))

Output:

Largest element is: 99

Method 3 : Find max list element on inputs provided by user




# Python program to find largest
# number in a list
# creating empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
# print maximum element
print("Largest element is:", max(list1))

Output:

Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 1 Enter elements: 99 Largest element is: 99

Method 4 : Without using built in functions in python:




# Python program to find largest
# number in a list
def myMax(list1):
# Assume first number in list is largest
# initially and assign it to variable "max"
max = list1[0]
# Now traverse through the list and compare
# each number with "max" value. Whichever is
# largest assign that value to "max'.
for x in list1:
if x > max :
max = x
# after complete traversing the list
# return the "max" value
return max
# Driver code
list1 = [10, 20, 4, 45, 99]
print("Largest element is:", myMax(list1))

Output:

Largest element is: 99

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
Python list-programs
python-list
Practice Tags :
python-list

Python program to find second largest number in a list

Given a list of numbers, the task is to write a Python program to find the second largest number in the given list.
Examples:

Input: list1 = [10, 20, 4] Output: 10 Input: list2 = [70, 11, 20, 4, 100] Output: 70

Method 1: Sorting is an easier but less optimal method. Given below is an O(n) algorithm to do the same.




# Python program to find second largest
# number in a list
# list of numbers - length of
# list should be at least 2
list1 = [10, 20, 4, 45, 99]
mx=max(list1[0],list1[1])
secondmax=min(list1[0],list1[1])
n =len(list1)
for i in range(2,n):
if list1[i]>mx:
secondmax=mx
mx=list1[i]
elif list1[i]>secondmax and \
mx != list1[i]:
secondmax=list1[i]
print("Second highest number is : ",\
str(secondmax))
Output Second highest number is : 45

Method 2: Sort the list in ascending order and print the second last element in the list.




# Python program to find largest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# sorting the list
list1.sort()
# printing the second last element
print("Second largest element is:", list1[-2])
Output

Second largest element is: 45

Method 3: By removing the max element from the list




# Python program to find second largest
# number in a list
# list of numbers
list1 = [10, 20, 4, 45, 99]
# new_list is a set of list1
new_list = set(list1)
# removing the largest element from temp list
new_list.remove(max(new_list))
# elements in original list are not changed
# print(list1)
print(max(new_list))
Output 45

Method 4: Find max list element on inputs provided by the user




# Python program to find second largest
# number in a list
# creating empty list
list1 = []
# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
'''
# sort the list
list1.sort()
# print second maximum element
print("Second largest element is:", list1[-2])
'''
# print second maximum element using sorted() method
print("Second largest element is:", sorted(list1)[-2])

Output:

Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 1 Enter elements: 99 Second Largest element is: 19

Method 5: Traverse once to find the largest and then once again to find the second largest.




def findLargest(arr):
secondLargest = arr[0]
largest = arr[0]
for i in range(len(arr)):
if arr[i] > largest:
largest = arr[i]
for i in range(len(arr)):
if arr[i] > secondLargest and arr[i] != largest:
secondLargest = arr[i]
return secondLargest
print(findLargest([10, 20, 4, 45, 99]))

Output:

45

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python Programs
Python list-programs
python-list
Practice Tags :
python-list

Max Function in Python

Given these assumptions, my first thought is the max function in Python, which will return the maximum value in a list.

integers = [1, 16, 3, 39, 26, 4, 8, 16] largest_integer = max(integers) # 39

Max is a very handy function for finding the largest integer in the list, but I also need to find the second largest integer. If I remove the largest integer from the list after finding it, then the second largest integer must now be the new largest integer. I can call max on the list a second time and get the next largest integer.

integers = [1, 16, 3, 39, 26, 4, 8, 16] largest_integer = max(integers) # 39 integers.remove(largest_integer) second_largest_integer = max(integers) # 26

If mutating the list isn't an option and assuming this is a small list of integers for the sake of computer memory, I can always clone the list and then perform the operation on the new list. This would keep the original list, integers, unmodified.

integers = [1, 16, 3, 39, 26, 4, 8, 16] # clone the list by slicing copy_of_integers = integers[:] # [1, 16, 3, 39, 26, 4, 8, 16] largest_integer = max(copy_of_integers) # 39 copy_of_integers.remove(largest_integer) second_largest_integer = max(copy_of_integers) # 26

Using the max function to find the largest and second largest integers in a list seems like an ideal solution.

Python Program to find Largest of Two Numbers using Elif Statement

Although there are many approaches to find the largest number among the two numbers, we discuss a few of them. Thispython programfor the largest of two numbers, helps the user to enter two different values. Next,the Python programfinds the largest number among those two numbers usingElif Statement.

# Python Program to find Largest of Two Numbers a = float(input(" Please Enter the First Value a: ")) b = float(input(" Please Enter the Second Value b: ")) if(a > b): print("{0} is Greater than {1}".format(a, b)) elif(b > a): print("{0} is Greater than {1}".format(b, a)) else: print("Both a and b are Equal")

In this Python Program to find Largest of Two Numbers output, First, we entered the values a = 10, b = 20

Please Enter the First Value a: 10 Please Enter the Second Value b: 20 20.0 is Greater than 10.0

Next, we entered the values a = 10, and b = 10

Please Enter the First Value a: 10 Please Enter the Second Value b: 10 Both a and b are Equal

At last, we entered the values a = 25, b = 15

Python Program to find Largest of Two Numbers 3

Within this python program to find Largest of Two Numbers example, the following statements ask users to enter two numbers and stores them in variables a, and b

a = float(input(" Please Enter the First Value a: ")) b = float(input(" Please Enter the Second Value b: "))

The Elif Statement is

if(a > b): print("{0} is Greater than {1}".format(a, b)) elif(b > a): print("{0} is Greater than {1}".format(b, a)) else: print("Both a and b are Equal")
  • The first if condition checks whether a is greater than b. If True, then a is greater than b printed
  • Elif statement checks whether b is greater than a. If True, then b is higher than a printed
  • If all the above conditions fail, they are equal.

Python program to find largest number in a list

In this tutorial, you will learn how to find the largest number in a list. List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. Previously, we learned how to find the smallest number in a list, we will be following similar approaches in this tutorial.

For a given list of numbers, the task is to find the largest number in the list.

Input: [10, 3, 20, 9, 11, 15, 23, 6]

Output: 23

Python Program to Add Two Numbers

In this program, you will learn to add two numbers and display it using print() function.

To understand this example, you should have the knowledge of the following Python programming topics:

  • Python Input, Output and Import
  • Python Data Types
  • Python Operators

In the program below, we've used the + operator to add two numbers.

Python Program to Find Largest of Two Numbers



This article is created to cover some programs in Python, that find and prints largest or greatest between two numbers entered by user. Here are the list of approaches to do the job:

  • Find Largest of Two Numbers using if-else
  • Using if only
  • Using user-defined Function