Home › Python programs (Hindi)
Celsius को Fahrenheit में बदलना Python में — code और dry run
Problem statement
Temperature को Celsius और Fahrenheit के बीच बदलना: F = C × 9/5 + 32 से, और वापस C = (F − 32) × 5/9 से।Approach 1: simple loop
Apply the formula directly. The order matters exactly as written: multiply first, then add 32 — put brackets around anything you want done earlier.
c = 37 f = c * 9 / 5 + 32 print(f) # Output: 98.6
Check the anchors as you go: 0°C must give 32°F (freezing point) and 100°C must give 212°F (boiling point). If those two pass, the formula is right.
Approach 2: reusable function
Wrap both directions in functions so the conversion is reusable and testable — and each formula lives in exactly one place.
def to_fahrenheit(c):
return c * 9 / 5 + 32
def to_celsius(f):
return (f - 32) * 5 / 9
print(to_fahrenheit(37)) # 98.6
print(to_celsius(98.6)) # 37.0
print(to_fahrenheit(100)) # 212.0Notice the asymmetry in the brackets: the reverse formula needs (f - 32) grouped because subtraction happens before multiplication would not give the right order.
Approach 3: Pythonic alternative
Real programs convert ranges, not single values — and present them in a tidy table with f-string formatting.
def to_fahrenheit(c):
return c * 9 / 5 + 32
print(f"{'C':>5} {'F':>7}")
for c in range(-2, 3):
print(f"{c:>5} {to_fahrenheit(c):>7.1f}")
# Output:
# -2 28.4
# -1 30.2
# 0 32.0
# 1 33.8
# 2 35.6{c:>5} right-aligns in 5 characters and :.1f shows exactly one decimal — a two-character change that turns output into a report.
Dry run
C = 37 trace करें: 37 × 9 = 333। 333 / 5 = 66.6। 66.6 + 32 = 98.6 — शरीर का तापमान। Anchors भी check करें: 0°C → 32°F (जमना ✓), 100°C → 212°F (उबलना ✓)। और अपने ही जवाब को उल्टा घुमाओ: (98.6 − 32) × 5/9 = 37 — शुरुआती पर वापस आ गए, यही conversion का सबसे पक्का verification है।Common errors
9 // 5लिखना — integer division से यह 1 बन जाता है; हर जवाब C + 32 आएगा।+ 32भूलना — हर जवाब ठीक 32 degree कम या ज़्यादा।- आगे की direction में 5/9 लगा देना — वह उल्टे का formula है।
- Reverse formula में brackets न लगाना — बिना bracket गणित ही बदल जाता है।
input()कोfloat()में न बदलना — temperature दशमलव हो सकता है।
Practice on PyDebug
Concept समझने के बाद इन free problems को browser में solve करें: