Home › Python programs (Hindi)
तीन Numbers में सबसे बड़ा — code, output और dry run
Problem statement
तीन numbers दिए हों, सबसे बड़ा print करना। बराबर values भी आ सकती हैं — जो भी सबसे बड़ी हो, वही जवाब है।Approach 1: simple loop
Compare with an if/elif ladder. Each condition asks "is this number at least as large as both others?" and the first true branch wins.
a, b, c = 12, 45, 23
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print(largest)
# Output: 45Note the >=: with ties like 7, 7, 3, the first branch still fires and the answer is correct.
Approach 2: reusable function
Keep a running champion: start with the first number, replace it whenever a later number beats it. This shape scales to ten numbers without changing its logic.
def largest_of(numbers):
best = numbers[0]
for n in numbers[1:]:
if n > best:
best = n
return best
print(largest_of([12, 45, 23])) # 45
print(largest_of([5, 5, 5])) # 5
print(largest_of([-2, -9, -4])) # -2Only two ideas: remember the best so far, and update on a strict win. The negative-numbers case works because every comparison is relative.
Approach 3: Pythonic alternative
Python has a built-in for "largest": max(). It accepts any number of arguments, or a single list, and even a key function for custom ordering.
a, b, c = 12, 45, 23 print(max(a, b, c)) # 45 print(max([12, 45, 23])) # 45 — same thing via a list
For exams, write the ladder; for code you will keep, max() says exactly what it means.
Dry run
Champion version को[12, 45, 23] पर चलाएँ। शुरुआत: best = 12। 45 > 12 → हाँ, best = 45। 23 > 45 → नहीं, best वही 45। जवाब: 45 — तीन numbers के लिए बस दो comparisons। Ladder version में पहली शर्त (12 >= 45) तुरंत fail, दूसरी शर्त pass — और b print हुआ।Common errors
orकी जगहand— "or" से गलत number भी pass हो जाता है।=बनाम==की अदला-बदली।- सबसे बड़ा मानकर 0 से शुरू करना — सब negative हों तो जवाब गलत; पहले number से शुरू करो।
- Ties भूलना — हर जगह strict
>लिखने से ladder में जवाब set ही नहीं होता। - तीनों branch के results print कर देना।
Practice on PyDebug
Concept समझने के बाद इन free problems को browser में solve करें: