Minimum of two numbers in Python (original) (raw)
Last Updated : 29 Nov, 2024
In this article, we will explore various methods to find minimum of two numbers in Python. The simplest way to find minimum of two numbers in Python is by using built-in **min()function.
Python `
a = 7 b = 3 print(min(a, b))
`
**Explanation:
- **min() function compares the two numbers **a and **b.
- So, it returns 3 as the smaller value
**Let’s explore other different method to find minimum of two numbers:
Table of Content
Using Conditional Statements
Another way to find the minimum of two numbers is by using conditional statements like **if and **else. This approach gives us more control over the logic and is useful when we need to implement custom rules.
Python `
a = 5 b = 10
if a < b: print(a) else: print(b)
`
**Explanation:
- The **if statement checks whether **a is greater than **b.
- If the condition is **true then it prints **a otherwise it prints **b.
- Since **b is greater so, it prints **5.
Using Ternary Operator
Python also supports a shorthand version of conditional statements known as the ternary operator. It allows us to write concise conditional expressions on a single line.
Python `
a = 7 b = 2 res = a if a < b else b print(res)
`
**Explanation:
- The **ternary operator evaluates the condition **a > b.
- If **true then it returns **a, otherwise it returns **b.
- Here, since **a is greater so the result is **2.
Similar Reads
- Python Program to Find LCM of Two Numbers We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python. Example: Input: a = 12, b = 15Output: 60Explanation: LCM of 12 and 15 is 60Python Program to Find LCM of Two NumbersBelo 3 min read
- Python - Find minimum of non zero groups Many times we need to get the minimum of not the whole list but just a part of it and at regular intervals. These intervals are sometimes decided statically before traversal, but sometimes, the constraint is more dynamic and hence we require to handle it in more complex way. Criteria discussed here 4 min read
- Python | List of tuples Minimum Sometimes, while working with Python records, we can have a problem in which we need to perform cross minimum of list of tuples. This kind of application is popular in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + zip() 4 min read
- How to Add Two Numbers in Python The task of adding two numbers in Python involves taking two input values and computing their sum using various techniques . For example, if a = 5 and b = 7 then after addition, the result will be 12. Using the "+" Operator+ operator is the simplest and most direct way to add two numbers . It perfor 5 min read
- Subtract Two Numbers in Python Subtracting two numbers in Python can be done in multiple ways. The most common methods include using the minus operator (-), defining a function, using a lambda function, or applying bitwise operations. Each method has its use cases depending on simplicity, readability and performance needs. Using 2 min read
- Working with Negative Numbers in Python Negative numbers play a crucial role in various mathematical and programming scenarios. In Python, handling negative numbers is a fundamental skill for developers. In this article, we will explore some commonly used methods for working with negative numbers in Python, along with examples to illustra 3 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 funct 2 min read
- Find Square Root Of Given Number - Python Given an integer X, find its square root. If X is not a perfect square, then return floor(√x). For example, if X = 11, the output should be 3, as it is the largest integer less than or equal to the square root of 11. Using built-in functionsWe can also find the floor of the square root using Python’ 3 min read
- Python Program to Find the Gcd of Two Numbers The task of finding the GCD (Greatest Common Divisor) of two numbers in Python involves determining the largest number that divides both input values without leaving a remainder. For example, if a = 60 and b = 48, the GCD is 12, as 12 is the largest number that divides both 60 and 48 evenly. Using e 2 min read
- Number System in Python The arithmetic value that is used for representing the quantity and used in making calculations is defined as NUMBERS. The writing system for denoting numbers logically using digits or symbols is defined as a Number system. Number System is a system that defines numbers in different ways to represen 6 min read