PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
  • Quizzes
  • Code Editor
Home » Python » Programs and Examples » Python Program to Find Smallest and Largest Number

Python Program to Find Smallest and Largest Number

Updated on: March 31, 2025 | Leave a Comment

This article covers the Python program for finding the smallest and largest numbers among the given list of numbers with explanations and examples.

Table of contents

  • 1. Using min() and max() Functions
  • 2. Using Conditional Statements (if-else)
  • 3. Using sorting

1. Using min() and max() Functions

The min() and max() functions are built-in Python functions used to find the smallest and largest values from a given set of numbers, lists, or iterables.

min() and max() functions work for both integer and float numbers.

min() function:

Syntax:

min(iterable) # Returns the smallest value from an iterable
min(a, b, c, …) # Returns the smallest among multiple argumentsCode language: Python (python)

For Example:

print(min(5, 10, 2, 8)) # Output: 2
print(min([4, 1, 7, 3])) # Output: 1
print(min("apple", "banana", "cherry")) # Output: "apple" (based on alphabetical order)Code language: Python (python)

max() function:

Syntax:

max(iterable) # Returns the largest value from an iterable
max(a, b, c, …) # Returns the largest among multiple argumentsCode language: Python (python)

For Example:

print(max(5, 10, 2, 8)) # Output: 10
print(max([4, 1, 7, 3])) # Output: 7
print(max("apple", "banana", "cherry")) # Output: "cherry" (alphabetical order)Code language: Python (python)

Code Example

num1 = 30.5
num2 = 10
num3 = -20

smallest = min(num1, num2, num3)
largest = max(num1, num2, num3)

print(f"Smallest: {smallest}, Largest: {largest}")

# from list of numbers

numbers = [5, 10, 75, 2, 225]

smallest = min(numbers)
largest = max(numbers)

print(f"Smallest: {smallest}, Largest: {largest}")Code language: Python (python)

Output:

Smallest: -20, Largest: 30.5
Smallest: 2, Largest: 225

2. Using Conditional Statements (if-else)

Conditional statements in Python control flow of execution based on given conditions. They allow the program to make decisions and execute different blocks of code depending on whether a condition is True or False.

Syntax:

if condition1:
    # this Code executes if condition1 is True
elif condition2:
    # this Code executes <em>if condition2 is True
</em>else:
    # this Code executes if none of the above conditions are TrueCode language: Python (python)

Code Example

num1 = 30.5
num2 = 10
num3 = -20

def find_smallest(num1, num2, num3):
    # Finding the smallest
    if num1 < num2 and num1 < num3:
        smallest = num1
    elif num2 < num3:
        smallest = num2
    else:
        smallest = num3
    return smallest

def find_largest(num1, num2, num3):
    # Finding the largest
    if num1 > num2 and num1 > num3:
        largest = num1
    elif num2 > num3:
        largest = num2
    else:
        largest = num3
    return largest

smallest = find_smallest(num1, num2, num3)
largest = find_largest(num1, num2, num3)

print(f"Smallest: {smallest}, Largest: {largest}")

# Output:
# Smallest: -20, Largest: 30.5Code language: Python (python)

Explanation

  • We have defined two functions, find_smallest() function to find the minimum value and find_largest() function to find the maximum value.
  • find_smallest() function:
    • Compares num1 with num2 and num3.
    • If num1 is the smallest, it is returned.
    • Otherwise, it checks if num2 is smaller than num3, then num2 is returned.
    • If neither of those conditions is met, num3 is the smallest.
  • find_largest() function:
    • Checks if num1 is greater than both num2 and num3, then it is the largest.
    • Otherwise, if num2 is greater than num3, num2 is the largest.
    • Otherwise, num3 is the largest.

3. Using sorting

Sorting is the process of arranging elements in a specific order, typically ascending (smallest to largest) or descending (largest to smallest). Sorting is done using sort() function to arrange the list of numbers in ascending order, allowing easy retrieval of the smallest (numbers[0]) and largest (numbers[-1]) values.

The sort() function is a built-in list method in Python that sorts the elements of a list in place, meaning it modifies the original list instead of creating a new one.

Syntax: list.sort(key=None, reverse=False)

  • key (Optional) → A function that specifies sorting criteria (e.g., length, absolute value).
  • reverse (Optional) → If True, sorts in descending order; default is False (ascending).

Code Example

num1 = 30.5
num2 = 10
num3 = -20

numbers = [num1, num2, num3]    # Add numbers into list
numbers.sort()  # Sorting the list in ascending order
smallest = numbers[0]
largest = numbers[-1]

print(f"Smallest: {smallest}, Largest: {largest}")

# Output:
# Smallest: -20, Largest: 30.5Code language: Python (python)

Explanation

  • Here, we add the numbers to the list to find the smallest and largest among them.
  • Then, we sort that list in ascending order using sort() function on the list.
  • The first element after sorting (numbers[0]) is the smallest, and the last element (numbers[-1]) is the largest.
  • This method is easy to understand but slightly slower due to the overhead of sorting.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Image

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2025 pynative.com

Advertisement