Open In App

Minimum of two numbers in Python

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two numbers, write a Python code to find the Minimum of these two numbers. Examples:

Input: a = 2, b = 4
Output: 2

Input: a = -1, b = -4
Output: -4

Method #1: This is the naive approach where we will compare the numbers using if-else statement and will print the output accordingly. Example: 

Python3




# Python program to find the
# minimum of two numbers
 
 
def minimum(a, b):
     
    if a <= b:
        return a
    else:
        return b
     
# Driver code
a = 2
b = 4
print(minimum(a, b))


Output:

2

Time Complexity: O(1)
Auxiliary Space: O(1)

Method #2: Using min() function This function is used to find the minimum of the values passed as its arguments. Example: 

Python3




# Python program to find the
# minimum of two numbers
 
 
a = 2
b = 4
 
minimum = min(a, b)
print(minimum)


Output

2

Time Complexity: O(1)
Auxiliary Space: O(1)

METHOD 3: Using sorted() function

APPROACH:

The code snippet sorts the given two numbers in ascending order and returns the first element, which is the minimum of the two.

ALGORITHM:

1.Initialize variables a and b with the given two numbers.
2.Create a list with the two numbers and sort the list in ascending order using the sorted() function.
3.Retrieve the first element of the sorted list using indexing and assign it to the minimum variable.
4.Print the minimum variable.

Python3




a = 2
b = 4
print(sorted([a, b])[0])


Output

2

Time complexity:
The time complexity of the code is O(1) due to the use of the sorted() function.

Space complexity:
The space complexity of the code is O(1) due to the use of the list to store the two numbers

METHOD 4:Using reduce function.

APPROACH:

This program finds the minimum of two numbers using the reduce function of functools module.

ALGORITHM:

1. Import the reduce function from the functools module.
2. Define the two numbers, a and b.
3. Apply the reduce function with a lambda function that compares the two numbers and returns the minimum.
4. Print the result.

Python3




from functools import reduce
 
a = 2
b = 4
minimum = reduce(lambda x, y: x if x < y else y, [a, b])
print(minimum)


Output

2

Time Complexity: O(1), as it only involves the comparison of two numbers.
Space Complexity: O(1), as it only uses constant amount of memory for the two numbers and the minimum value.



Similar Reads

Using Counter() in Python to find minimum character removal to make two strings anagram
Given two strings in lowercase, the task is to make them Anagram. The only allowed operation is to remove a character from any string. Find minimum number of characters to be deleted to make both the strings anagram? If two strings contains same data set in any order then strings are called Anagrams. Examples: Input : str1 = "bcadeh" str2 = "hea" O
3 min read
Python | Maximum and Minimum value from two lists
The problem of finding maximum and minimum values in a list is quite common. But sometimes this problem can be extended in two lists and hence becomes a modified problem. This article discusses shorthands by which this task can be performed easily. Let's discuss certain ways in which this problem can be solved. Method #1: Using max() + min() + "+"
7 min read
Convert Strings to Numbers and Numbers to Strings in Python
In Python, strings or numbers can be converted to a number of strings using various inbuilt functions like str(), int(), float(), etc. Let's see how to use each of them. Example 1: Converting a Python String to an int: C/C++ Code # code # gfg contains string 10 gfg = &quot;10&quot; # using the int(), string is auto converted to int print(int(gfg)+2
2 min read
Python Program for GCD of more than two (or array) numbers
The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) Python Code # GCD of more than two (or array) numbers # This function implements the Euclidean #
1 min read
Python Program for Common Divisors of Two Numbers
Given two integer numbers, the task is to find the count of all common divisors of given numbers? Input : a = 12, b = 24Output: 6Explanation: all common divisors are 1, 2, 3, 4, 6 and 12Input : a = 3, b = 17Output: 1Explanation: all common divisors are 1Input : a = 20, b = 36Output: 3Explanation: all common divisors are 1, 2, 4 Python Code # Python
1 min read
Python Dictionary | Check if binary representations of two numbers are anagram
Given two numbers you are required to check whether they are anagrams of each other or not in binary representation. Examples: Input : a = 8, b = 4 Output : YesBinary representations of bothnumbers have same 0s and 1s.Input : a = 4, b = 5Output : NoCheck if binary representations of two numbersWe have existing solution for this problem please refer
3 min read
Python Program to swap two numbers without using third variable
Given two variables n1 and n2. The task is to swap the values of both the variables without using third variable.Examples: X : 10 Y : 20 After swapping X and Y, we get : X : 20 Y : 10 A : 'Hello' B : 'World' After swapping A and B, we get : A : 'World' B : 'Hello' Method 1 :- Using simple built-in method left , right = right , left This method work
4 min read
Python Program to Find the Gcd of Two Numbers
Given two numbers, The task is to find the GCD of the two numbers. Python Program to Find the Gcd of Two Numbers Using STL In Python, the math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments. Syntax
4 min read
Find Maximum of two numbers in Python
Given two numbers, write a Python code to find the Maximum of these two numbers. Examples: Input: a = 2, b = 4Output: 4 Input: a = -1, b = -4Output: -1 Find Maximum of two numbers in PythonThis is the naive approach where we will compare two numbers using if-else statement and will print the output accordingly. Example: C/C++ Code # Python program
2 min read
Python Program to Multiply Two Binary Numbers
Given two binary numbers, and the task is to write a Python program to multiply both numbers. Example: firstnumber = 110 secondnumber = 10 Multiplication Result = 1100 We can multiply two binary numbers in two ways using python, and these are: Using bin() functions andWithout using pre-defined functionsMethod 1: Using bin Functions Now, Let's write
2 min read
Practice Tags :