Quick answer: Use max(first, second) when two values are comparable and the larger value is what the program needs. A conditional expression can make tie behavior explicit, while validation protects code that receives mixed or unexpected types.

The simplest way to get the maximum of two numbers in Python is max(a, b). It is short, readable, and works with integers, floats, and other comparable values. You can also use an if/else statement or a conditional expression when you need custom behavior.
For most code, prefer max() because it directly says what you want. Use if/else when you need extra work around the comparison, such as logging, validation, or returning a custom message. The official Python max() documentation covers the built-in function in detail.
All examples below return the larger value. The difference is readability and control. If your code is part of a calculation, use the compact built-in. If your code is part of a teaching example or a branch with side effects, use the explicit branch.
Use max() for Two Numbers
max() accepts two or more positional arguments and returns the largest value. For two numbers, pass both numbers directly.
a = 42
b = 73
largest = max(a, b)
print(largest)
This is the cleanest answer when you only need the larger value. It also handles equal numbers naturally by returning that same value. If you later need the maximum item from a list and its position, see the Python list max index guide.
Use if-else for Explicit Logic
An if/else statement is longer, but it is useful when you want to make each branch explicit. This can be clearer for beginners and for code that does more than return the value.
a = 42
b = 73
if a >= b:
largest = a
else:
largest = b
print(largest)
The >= condition means the first number wins when the values are equal. That choice rarely matters for plain numbers, but it can matter when you compare custom objects or values that carry extra context. If you want the second value to win ties, change the condition to a > b.
Use a Ternary Conditional Expression
A conditional expression puts the same idea on one line. It is useful for small assignments, but it should stay simple. If the condition becomes hard to read, use a normal if/else block.
a = 42
b = 73
largest = a if a >= b else b
print(largest)
The official Python reference calls this a conditional expression. For more examples, see the Python ternary operator guide. Ternary expressions are best when both outcomes are short and the condition is obvious at a glance.

Compare User Input Safely
User input arrives as strings. Convert values to numbers before comparing them; otherwise, Python compares text lexicographically, which can produce surprising results. For example, the string "9" can compare greater than "12" because the first character is compared first.
first = float("9.5")
second = float("12.25")
largest = max(first, second)
print(largest)
If you are reading input with input(), validate it before conversion in production code. For simple scripts, int() or float() is enough once you know the input format. Use int() for whole numbers and float() when decimal values are allowed.
Use a Lambda Only When Passing a Function
You may see lambda examples for this task, but a lambda is not necessary just to compare two numbers. It is useful only when you need to pass a comparison helper around as a function.
larger = lambda a, b: a if a >= b else b
print(larger(42, 73))
The official tutorial covers lambda expressions. In normal application code, a named function is usually clearer if the logic is reused in more than one place. For a one-off comparison, max(a, b) remains easier to read.

Get the Maximum of Three or More Numbers
max() is not limited to two values. You can pass several numbers directly or pass an iterable such as a list or tuple. This makes it easy to grow from two values to a larger set without rewriting the comparison logic.
a = 42
b = 73
c = 61
print(max(a, b, c))
print(max([a, b, c]))
This is cleaner than nesting comparisons manually. If you are comparing many values as part of a larger calculation, related guides such as Python average of list and Python max function cover adjacent list workflows.
Best Practice
Use max(a, b) when you simply need the larger of two numbers. Use an if/else block when the comparison controls multiple actions. Use a ternary expression for short, obvious assignments. Avoid lambda for this task unless you specifically need a callable object. If you are comparing custom objects, the comparison methods behind sorting and max values are related to sorting lists of tuples and other ordering patterns.
For beginner-friendly code, write the form that future readers can understand fastest. In most cases that means max(); in teaching material, the explicit branch can help show how the comparison works.
Use max For Comparable Values
The built-in max function communicates a comparison directly and works for numeric values that share a meaningful ordering. Keep the input types consistent at the boundary of the function.

Write An Explicit Comparison
first if first >= second else second is useful when the code needs to choose the first object on a tie or explain the rule inline. Use > instead when the second object should win equal cases.
Handle Numeric Types
Integers and floats can be compared in normal numeric workflows, but Decimal, Fraction, NaN, and custom numeric classes deserve tests. Do not assume every numeric-looking value has identical ordering semantics.

Validate Inputs
If values arrive from a form, file, or API, parse them before comparison and reject malformed data. A TypeError is preferable to silently converting a value with an unintended rule.
Do Not Sort To Find Two
Sorting a two-item collection works but adds ceremony and can hide the intent. max is linear in the number of candidates and is the direct operation for this problem.
Test Ties And Boundaries
Test positive and negative values, equal objects, zero, decimals, invalid inputs, and custom comparable objects. Assert the selected object as well as its numeric value when identity matters.
The official max() documentation covers comparison and key behavior. Related Python Pool references include tests and dictionaries.
For related comparisons, compare sequence values, mapped values, and edge-case tests when selecting a maximum.
Frequently Asked Questions
How do I find the maximum of two numbers in Python?
Call max(first, second) when both values are comparable and the result should be the larger value.
Can I use a conditional expression instead?
Yes. first if first >= second else second is explicit and lets you define the tie behavior.
What happens when the values are equal?
max returns the equal value, while a conditional expression can choose the first or second object deliberately.
Why does max raise a TypeError?
The values may not be mutually comparable, or the input may contain a type that does not define the ordering you expect.
There is a 5th method:
a=50;b=30;max=[a,b][b>a] ;print(f'{max} is a maximum number’)
Great, that’s one way of doing it!