Find Largest and Smallest Number in Python Without List

Finding the largest and smallest numbers in a sequence of numbers can be a useful task in many programming projects. However, what if you don’t have a list or array, but instead have a sequence of numbers that you want to find the largest and smallest of?

In this Python tutorial, we will explore a few different ways to find the largest and smallest numbers in a sequence of numbers without using a list in Python.

  • Largest and Smallest Number in Python Without List For Loop
  • Largest and Smallest Number in Python Without List min and max
  • Largest and Smallest Number in Python Without List sorted()

Largest and Smallest Number in Python Without List For Loop

One way to find the largest and smallest numbers in a sequence is to use a for loop to iterate through the numbers, keeping track of the current largest and smallest numbers as you go.

Let’s take an example by following the below steps:

Define variable numbers that contain the sequence of numbers you want to find the largest and smallest of.

numbers = [5, 2, 8, 1, 9]

Create two variables largest and smallest and initialize them with the first number in the numbers sequence.

largest = numbers[0]
smallest = numbers[0]

Use a for loop to iterate through the numbers sequence.

for number in numbers:
    if number > largest:
        largest = number
    elif number < smallest:
        smallest = number

In the for loop, use an if-else statement to check if the current number is greater than the current value of the largest. If it is, update the largest variable with the current number.
In the same if-else statement, check if the current number is less than the current value of the smallest. If it is, update the smallest variable with the current number.

Print the values of largest and smallest after the for loop has been completed.

print("Largest number:", largest)
print("Smallest number:", smallest)
Find Largest and Smallest Number in Python Without List
Find Largest and Smallest Number in Python Without List

Read: How to add two numbers in Python

Largest and Smallest Number in Python Without List min and max

Another way to find the largest and smallest numbers in a sequence is to use the built-in max() and min() functions. Follow the below steps:

Define variable numbers that contain the sequence of numbers you want to find the largest and smallest of.

numbers = [5, 2, 8, 1, 9]

Use the built-in max() function to find the largest number in the numbers sequence, and assign the result to a variable largest.

largest = max(numbers)

Use the built-in min() function to find the smallest number in the numbers sequence, and assign the result to the variable smallest.

smallest = min(numbers)

Print the values of the largest and smallest.

print("Largest number:", largest)
print("Smallest number:", smallest)
Find Largest and Smallest Number in Python Without List Using min and max
Find Largest and Smallest Number in Python Without List Using min and max

Read: How to add two variables in Python

Largest and Smallest Number in Python Without List sorted()

You can also use the built-in sorted() function to sort the numbers in ascending or descending order, and then use the first or last element of the sorted list to find the largest or smallest number. Follow the below steps:

Define variable numbers that contain the sequence of numbers you want to find the largest and smallest of.

numbers = [5, 2, 8, 1, 9]

Use the built-in sorted() function to sort the numbers sequence in ascending order.

numbers_sorted = sorted(numbers)

Assign the first element of the sorted list to the variable smallest.

largest = numbers_sorted[-1]

Assign the last element of the sorted list to the variable largest.

smallest = numbers_sorted[0]

Print the values of the largest and smallest.

print("Largest number:", largest)
print("Smallest number:", smallest)
Find Largest and Smallest Number in Python Without List Using sorted
Find Largest and Smallest Number in Python Without List Using sorted

You may also like to read the following Python tutorials.

Conclusion

In this Python tutorial, we have seen how to find the largest and smallest numbers in a sequence of numbers without using a list in Python. There are several other ways to find the largest and smallest numbers in a sequence of numbers in Python, and the best method will depend on your specific use case.

However, the examples mentioned in the above tutorial should provide a good starting point for finding the largest and smallest numbers in a sequence without using a list in Python.

  • Largest and Smallest Number in Python Without List For Loop
  • Largest and Smallest Number in Python Without List min and max
  • Largest and Smallest Number in Python Without List sorted()