Friday, 13 January 2017

How to find the largest number from a list of numbers in Python?

Below script will take the list of integers and find and print the largest number. Here we taking the input as a space separated integers and then using split() method to convert them to a list of integers.

Code:


def findLargest(numList):
 max = numList[0]
 for num in numList:
  if num > max:
   max = num
 return max

print 'Enter the list of numbers Seperated by Space:'
userInput = raw_input()

numlist = [int(n) for n in userInput.split()]

print 'Largest Number : {0}'.format(findLargest(numlist))

Execution: 

No comments:

Post a Comment