Python Program to Find the Smallest Among Three Numbers

2 min read

Problem Definition

Create a Python program to find the smallest among the three given numbers, taking runtime inputs from the user.

In this article, we will show you two methods to address this same problem one being the conventional way and the other being the Pythonic way.

Program To Find The Smallest Number Using Conditional Statements

  1. Take inputs from the user and store them in variables
  2. Compare the numbers
  3. Print the final result
  4. End

To understand the program you need to have an understanding of following Python concept.

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

smallest = num1
if (num2 < num1) and (num2 < num3):
    smallest = num2
elif (num3 < num1):
    smallest = num3

print("The smallest number is", smallest)

Runtime Test Cases

Enter first number: 90
Enter second number: 34
Enter third number: 69
The smallest number is 34.0

Explanation

First, the program is taking input from the keyboard using the built-in input() function. Although we do need to change the datatype of the inputs to either integer or float, we are converting the input to float for more accuracy.

Next, there are 3 conditional statements which will store the smallest number into the variable smallest which later gets printed out.

Program To Find The Smallest Number Using List Method

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

my_list = [num1, num2, num3]
smallest = min(my_list)
print("The smallest number is", smallest)

Runtime Test Cases

Enter first number: 90
Enter second number: 34
Enter third number: 69
The smallest number is 34.0

In this approach, we are making a list of all the inputs and using the min() method which returns the item with the lowest value in the list.


PROGRAMS
author's image
Abhijeet Pal Author and Editor in Chief @djangocentral

Abhijeet is a full-stack software developer from India with a strong focus on backend and system design. He is driven by the need to create impactful solutions that add value to the internet in any way possible.

LinkedIn Twitter Github

Latest Articles

Latest from djangocentral

Django 4.1 adds async-compatible interface to QuerySet

The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - https://github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…
Read more →

3 min read

Making Django Admin Jazzy With django-jazzmin

Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
Read more →

4 min read