16: Find the largest element in a List - 1000+ Python Programs


Python Find the Largest Number in the List

In this program, we will see how to find the largest element in a list of numbers.

Pseudo Logic:

- We take a list of numbers as input from the user.

- Next we initialize a variable max_number to the first element of the list.

- Then we traverse through the list using a loop.

- Next we, compare each element of the list with the max_number variable.

- If the element is greater than max_number, we update the max_number 
  variable with the new value.

- After traversing the entire list, we print the value of max_number 
  as this is the max number we were looking for.

Python Code

# Python Program no. 16
# Find Largest Number from List
# 1000+ Python Programs by Code2care.org

# User input as List
input_numers_list = list(map(int, input("Enter a List of Numbers to find Largest: ").split()))

# Let's assume 1st number is max
max_number = input_numers_list[0]

# Traverse the List
for num in input_numers_list:
    if num > max_number:
        max_number = num

# Print result
print("The Largest Number in the List is:", max_number)
Sample Run Result:

Enter a List of Numbers to find Largest: 19 8 2 4 11 44
The Largest Number in the List is: 44


Python Methods/Concepts used:

  1. List - to store the elements of the list. Python Doc: https://docs.python.org/3/tutorial/introduction.html#lists
  2. map() function - to convert the input string to a list of integers. Python Doc: https://docs.python.org/3/library/functions.html#map
  3. split() method - to split the input string into a list of substrings. Python Doc: https://docs.python.org/3/library/stdtypes.html#str.split
  4. for loop - to traverse through the list. Python Doc: https://docs.python.org/3/reference/compound_stmts.html#for
  5. if statement - to compare each element with the max_num variable. Python Doc: https://docs.python.org/3/reference/compound_stmts.html#if
  6. Facing issues? Have Questions? Post them here! I am happy to answer!

    Author Info:

    Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

    Follow him on: X

    You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap