Functions और if-elif-else
Function क्या है?
Function code का नाम वाला block है जो एक ख़ास काम करता है। आप उसे inputs (arguments) देते हैं, वह काम करता है और result (return value) वापस दे सकता है। Juice machine सोचिए: संतरे डालो (input), button दबाओ (call), juice पाओ (output)।
Built-in functions
prices = [120, 45, 300, 75]
print(len(prices), max(prices), min(prices), sum(prices))
print(abs(-15), round(7.468, 1), pow(2, 5))
print(int('80') + 20, float('2.5') * 4, type(3.0))| Function | Use |
|---|---|
| print(), input() | Output और input |
| int(), float(), str(), bool() | Type conversion |
| len(), max(), min(), sum(), sorted() | Sequences पर काम |
| abs(), round(), pow(), divmod() | Number helpers |
| type(), id() | Object का type और identity |
Modules के functions
Module ready functions की file है। Use से पहले import करें।
import math import statistics as st print(math.sqrt(625), math.ceil(4.01), math.floor(4.99)) marks = [56, 72, 72, 90, 64] print(st.mean(marks), st.median(marks), st.mode(marks))
User-defined functions
def function_name(parameter1, parameter2=default):
statements
return valuedef gst_amount(price, rate=18):
"""Return the GST on a price."""
return price * rate / 100
def final_price(price, rate=18):
return price + gst_amount(price, rate)
print(gst_amount(1000)) # default rate 18%
print(gst_amount(1000, 5)) # 5% rate
print(final_price(2500, 12))final_price gst_amount को call करता है — functions दूसरे functions use कर सकते हैं, जिससे हर एक छोटा और test करने में आसान रहता है।
return vs print
def show_line(ch, n):
print(ch * n) # this function only prints
show_line('=', 20)
result = show_line('-', 10)
print(result)बिना return वाला function None देता है। जब result आगे calculation में चाहिए तब return use करें; सिर्फ़ दिखाने के लिए print।
if से decision making
Programs को अक्सर actions में से चुनना पड़ता है। if statement condition check करता है; True हो तो indented block चलता है। Optional else block condition False होने पर चलता है।
temp = float(input('Room temperature: '))
if temp > 30:
print('Switch on the AC')
else:
print('Fan is enough')if-elif-else
दो से ज़्यादा choices के लिए elif use करें। Python हर condition क्रम से check करता है और सिर्फ़ पहला True block चलाता है। Example: Air Quality Index categories।
aqi = int(input('Enter AQI: '))
if aqi <= 50:
status = 'Good'
elif aqi <= 100:
status = 'Satisfactory'
elif aqi <= 200:
status = 'Moderate'
elif aqi <= 300:
status = 'Poor'
else:
status = 'Very Poor / Severe'
print('Air quality:', status)Nested if
age = int(input('Age: '))
has_licence = input('Licence (y/n): ')
if age >= 18:
if has_licence == 'y':
print('You can drive')
else:
print('Get a licence first')
else:
print('Too young to drive')Functions + conditions साथ में
def ticket_price(age):
if age < 5:
return 0
elif age < 12 or age >= 60:
return 50
else:
return 100
for a in (3, 8, 25, 65):
print('Age', a, '-> Rs.', ticket_price(a))सारांश और practice
- Built-in functions (len, max, round…), module functions (math, statistics) और user-defined functions (def)।
returnvalue वापस भेजता है; return नहीं → None।if,if-else,if-elif-else, nested if; colon और indentation ज़रूरी।
1) Function bmi(weight, height) लिखें जो BMI = weight ÷ height² return करे, और "Underweight" (< 18.5), "Normal" (18.5–24.9) या "Overweight" (≥ 25) print करें। 2) max() के बिना दो numbers में बड़ा return करने वाला function लिखें।
