Placement Prep

Greatest of Two Numbers in Python: 4 Methods with Code

Learn 4 Python methods to find the greatest of two numbers: max(), if-else, ternary operator, and arithmetic subtraction. Code, output, and when to use each.

By FACE Prep Team 6 min read
python comparison-operators if-else built-in-functions placement-prep python-programs coding-interview

Finding the greatest of two numbers in Python has four standard approaches, each testing a different layer of language knowledge. The methods are the built-in max() function, an if-else block, a ternary expression, and arithmetic subtraction.

All four appear in Python coding-round questions at Indian campus placements. The difference is not which answer they produce but which construct they demonstrate.

Prerequisites and Setup

This program uses three Python building blocks:

  • Variables and data types: int stores whole numbers; float stores decimals. The examples below use int.
  • The input() function: reads user input as a string. Wrapping it with int() converts it to an integer for arithmetic and comparison.
  • Conditional statements: the if, else, and elif constructs control which code block runs. Methods 2 and 4 depend on these entirely.

Python 3.x is assumed throughout. No additional libraries or imports are needed for any of the four methods.

Input Format and Problem Setup

The standard version of this problem takes two integers as input and prints the larger one.

Sample Input and Output

  • Input: 45 and 86
  • Expected output: 86 is greater

Equal Numbers Case

  • Input: 50 and 50
  • Expected output: 50 is greater (when >= is used in the condition)

Three questions to settle before writing the first line of code:

  • What type are the inputs? For placement test questions, assume int unless told otherwise.
  • What should the program print when both numbers are equal? The sample above uses >=, which prints the first number.
  • Are built-in functions allowed? Some coding platforms restrict this. Know which method to reach for before the test.

Method 1: Using the Built-in max() Function

The max() function is part of Python’s standard built-in library. No import is needed.

Algorithm

  • Step 1: Accept two integers from the user.
  • Step 2: Pass both to max().
  • Step 3: Print the result.

Python Code

# Find the greatest of two numbers using max()
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print(max(num1, num2), "is greater")

Sample Run

  • Input: 23, then 45
  • Output: 45 is greater

max() handles the equal case without any extra condition: max(50, 50) returns 50.

Use this method when you want concise, idiomatic code and Python’s standard library is available.

Method 2: Using if-else Statements

This is the most explicit approach and the one most placement questions expect when they specify “do not use built-in functions.”

Algorithm

  • Step 1: Accept two integers.
  • Step 2: Compare them using an if-else block.
  • Step 3: Print the greater one.

Python Code

# Find the greatest of two numbers using if-else
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a >= b:
    print(a, "is greater")
else:
    print(b, "is greater")

Sample Run

  • Input: 50, then 45
  • Output: 50 is greater

The >= condition covers the equal case: when a == b, the if block runs and prints a. Switching to > would route equal inputs to the else block and print b instead. The number printed is the same, but the code path differs.

Use this method for placement tests that restrict built-in functions, and for beginner exercises where control flow is the primary learning objective.

Method 3: Using the Ternary Operator

Python’s conditional expression (informally called the ternary operator) collapses the if-else into one line.

Syntax: value_if_true if condition else value_if_false

Python Code

# Find the greatest of two numbers using the ternary operator
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
greater = a if a >= b else b
print(greater, "is greater")

Sample Run

  • Input: 70, then 30
  • Output: 70 is greater

This construct appears directly in placement multiple-choice questions. A common format:

  • Q: What does x = a if a > b else b assign to x when a = 5 and b = 5?
  • Answer: b, which is 5. The condition 5 > 5 is False, so the else branch evaluates. Using >= instead of > would return a.

Use this method for one-liner solutions, list comprehensions, or anywhere you need to assign a value based on a condition without writing a full if-else block.

Method 4: Using Arithmetic Subtraction

This method uses the sign of the difference to determine which number is larger. No comparison operator is needed.

Algorithm

  • Step 1: Accept two integers.
  • Step 2: Subtract b from a.
  • Step 3: If the result is greater than 0, a is larger. Otherwise, b is larger or they are equal.
  • Step 4: Print the result.

Python Code

# Find the greatest of two numbers using arithmetic subtraction
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a - b > 0:
    print(a, "is greater")
else:
    print(b, "is greater")

Sample Run

  • Input: 90, then 100
  • Output: 100 is greater

Verification step by step:

  • a - b = 90 - 100 = -10
  • -10 > 0 is False
  • The else branch executes: prints 100 is greater

One edge case this method handles poorly: equal inputs. When a == b, the difference is 0, which fails > 0. The else branch runs and prints b is greater, which is misleading when both values are identical. Fix: change > 0 to >= 0.

Use this method rarely in production code, but know it exists for concept questions in placement aptitude sections: it demonstrates that arithmetic operations can substitute for explicit comparison operators.

Comparing the Four Methods

MethodLines of codeHandles equal caseNeeds built-inBest for
max()1Yes, nativelyYesConcise, idiomatic Python
if-else4Yes, with >=NoBeginner exercises, restricted environments
Ternary2Yes, with >=NoInline assignments, one-liners
Arithmetic4Partially (needs >= 0)NoConcept demonstrations

The max() approach is idiomatic Python. The if-else approach is what most placement tests ask you to write manually. The ternary is useful when you need an expression rather than a statement, such as inside a list comprehension or a function argument.

Edge Cases and Extended Variants

Negative Numbers

All four methods work correctly with negative integers. Python’s comparison operators and max() handle negatives without modification.

  • max(-5, -12) returns -5. The less-negative number is the greater one.
  • With if-else, a = -5 and b = -12: -5 >= -12 is True, so -5 is greater prints correctly.

Floating-Point Numbers

Replace int(input()) with float(input()) to accept decimal inputs. The comparison logic stays identical.

a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
print(max(a, b), "is greater")

For exact equality checks with floats, note that floating-point arithmetic means 0.1 + 0.2 does not equal 0.3 precisely in Python. For finding the greater of two floats, however, this does not affect the correctness of the comparison.

Extending to Three or More Numbers

The greatest of three numbers in Python uses nested if-elif-else blocks or max(a, b, c). The pattern is the same as what you have here, extended one comparison at a time. For N numbers stored in a list, max() handles it directly:

numbers = [12, 45, 7, 89, 33]
print(max(numbers))  # Output: 89

More list-level programs are covered in Python basic programs and practice examples.

Applying These Patterns in Real Code

The if-else and max() patterns from this article appear in any program that chooses between two competing values. Selecting the higher result, branching on a threshold, or picking between two outputs: all three are the same comparison.

In Python programs that interact with LLMs, that same logic applies. At TinkerLLM (₹299), the if-else and max() work practised here connects directly to querying and comparing model outputs.

The Python calculator program shows these building blocks in a different context, combining arithmetic and conditionals into a working interactive tool.

Primary sources

Frequently asked questions

What happens when both numbers are equal in Python?

Using >= in the condition returns the first number. Using > would skip the if-block and return the second instead. Both give the same numeric result; the difference only matters if the two values are distinct objects.

Which method should I use in placement coding tests?

Use max() for one-liners when Python's standard library is available. Use if-else when the question asks you to avoid built-in functions, which many placement coding platforms enforce.

Can these methods compare floating-point numbers?

Yes. All four methods work with floats. Note that floating-point representation means 0.1 + 0.2 does not equal 0.3 exactly in Python, but for standard greater-of-two comparisons this does not affect correctness.

Is the ternary operator faster than if-else in Python?

Performance is essentially identical. The ternary operator compiles to the same bytecode as the equivalent if-else block in CPython. Choose based on readability, not speed.

What is the ternary operator syntax in Python?

Python's ternary (conditional) expression is: value_if_true if condition else value_if_false. To find the greater of a and b, write: a if a >= b else b.

Does Python have a built-in function to find the greatest of two numbers?

Yes. The built-in max() function accepts two or more arguments and returns the largest. max(a, b) returns the greater of the two with no imports needed.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF