Skip to main content

How to find the minimum of two numbers in Python

How to find the minimum of two numbers in Python.

Here's a detailed step-by-step tutorial on how to find the minimum of two numbers in Python:

Step 1: Define the two numbers

  • Start by defining the two numbers that you want to compare. You can assign them to variables for easier use. For example:

    number1 = 10
    number2 = 5

Step 2: Use the built-in min() function

  • Python provides a built-in function called min() that can be used to find the minimum value among two or more values. Pass the two numbers as arguments to the min() function. For example:

    minimum = min(number1, number2)
  • The min() function will return the minimum value between number1 and number2 and assign it to the variable minimum.

Step 3: Compare the numbers using if-else conditional statements (optional)

  • If you don't want to use the min() function, you can manually compare the numbers using if-else conditional statements. For example:

    if number1 < number2:
    minimum = number1
    else:
    minimum = number2
  • In this case, if number1 is less than number2, it assigns number1 to the variable minimum. Otherwise, it assigns number2 to minimum.

Step 4: Print the minimum value

  • Finally, you can print the minimum value using the print() function. For example:

    print("The minimum value is:", minimum)
  • This will display the minimum value on the console.

Here's the complete code combining all the steps:

number1 = 10
number2 = 5

minimum = min(number1, number2)
print("The minimum value is:", minimum)

Or using if-else conditional statements:

number1 = 10
number2 = 5

if number1 < number2:
minimum = number1
else:
minimum = number2

print("The minimum value is:", minimum)

You can replace the values of number1 and number2 with any numbers you want to compare.