
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:
- List - to store the elements of the list. Python Doc: https://docs.python.org/3/tutorial/introduction.html#lists
- map() function - to convert the input string to a list of integers. Python Doc: https://docs.python.org/3/library/functions.html#map
- split() method - to split the input string into a list of substrings. Python Doc: https://docs.python.org/3/library/stdtypes.html#str.split
- for loop - to traverse through the list. Python Doc: https://docs.python.org/3/reference/compound_stmts.html#for
- if statement - to compare each element with the max_num variable. Python Doc: https://docs.python.org/3/reference/compound_stmts.html#if
- Program 11: Calculate Percentage - 1000+ Python Programs
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 1: Print Hello World! - 1000+ Python Programs
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Program 15: Find String is a Palindrome or Not - 1000+ Python Programs
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 13: Reverse a String - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 14: Sum of Even Numbers from 1 to 100 - 1000+ Python Programs
- Program 10: Modulo of Two Numbers - 1000+ Python Programs
- 16: Find the largest element in a List - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- Program 2: Print your name using print() function - 1000+ Python Programs
- 19: Simple Calculator using built-in functions - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- Program 4: Input Name and Age and Print - 1000+ Python Programs
- Program 3: Print the name of user as input - 1000+ Python Programs
- How to change Git Default Author and Committer details in Eclipse - Git
- Users experience call quality issue, voice distortion, disconnection with Microsoft Teams call and meeting - Teams
- Installing Home-brew on Ubuntu - Ubuntu
- [Solution] Exception in thread main java.util.EmptyStackException - Java
- Remove Trailing zeros BigDecimal Java - Java
- Java JDBC IN Clause Example with PreparedStatement MySQL - Java
- Go to Specific file path using Mac Finder - MacOS
- Java 8 java.util.Function and BiFunction Examples - Java
Have Questions? Post them here!