Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🐍 Unit 2 – Python Programming  ·  Lesson 06

Functions और if-elif-else

Function क्या है?

Function code का नाम वाला block है जो एक ख़ास काम करता है। आप उसे inputs (arguments) देते हैं, वह काम करता है और result (return value) वापस दे सकता है। Juice machine सोचिए: संतरे डालो (input), button दबाओ (call), juice पाओ (output)।

Argumentsprice = 1000, rate = 18Function bodyprice * rate / 100Return value180.0
Function input लेकर output return करता है

Built-in functions

Python
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))
4 300 45 540 15 7.5 32 100 10.0 <class 'float'>
FunctionUse
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 करें।

Python
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))
25.0 5 4 70.8 72 72

User-defined functions

Syntax
def function_name(parameter1, parameter2=default):
    statements
    return value
Python
def 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))
180.0 50.0 2800.0

final_price gst_amount को call करता है — functions दूसरे functions use कर सकते हैं, जिससे हर एक छोटा और test करने में आसान रहता है।

return vs print

Python
def show_line(ch, n):
    print(ch * n)            # this function only prints

show_line('=', 20)
result = show_line('-', 10)
print(result)
==================== ---------- None

बिना 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 होने पर चलता है।

StartInput temptemp > 30 ?YesNoPrint "Fan is enough"Print "Switch on the AC"Stop
Flowchart के रूप में if-else
Python
temp = float(input('Room temperature: '))
if temp > 30:
    print('Switch on the AC')
else:
    print('Fan is enough')
Room temperature: 33.5 Switch on the AC

if-elif-else

दो से ज़्यादा choices के लिए elif use करें। Python हर condition क्रम से check करता है और सिर्फ़ पहला True block चलाता है। Example: Air Quality Index categories।

Python
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)
Enter AQI: 187 Air quality: Moderate
Program में use हुई AQI category limits0100200300400500GoodSatisf.ModeratePoorV.Poor50100200300400
AQI 187 Moderate bar में आता है

Nested if

Python
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')
Age: 19 Licence (y/n): n Get a licence first

Functions + conditions साथ में

Python
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))
Age 3 -> Rs. 0 Age 8 -> Rs. 50 Age 25 -> Rs. 100 Age 65 -> Rs. 50

सारांश और practice

  • Built-in functions (len, max, round…), module functions (math, statistics) और user-defined functions (def)।
  • return value वापस भेजता है; return नहीं → None।
  • if, if-else, if-elif-else, nested if; colon और indentation ज़रूरी।
✅ Practice

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 लिखें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Built-in और user-defined function में क्या फ़र्क है?
len(), max() और print() जैसे built-in functions Python के साथ आते हैं। User-defined functions programmer def keyword से लिखता है।
if statements में indentation क्यों ज़रूरी है?
Indentation बताता है कि कौन से statements if या else block के हैं। ग़लत indentation से IndentationError आती है या ग़लत statements चलते हैं।
क्या function में default parameter हो सकता है?
हाँ, जैसे def gst_amount(price, rate=18)। Caller rate न भेजे तो 18 use होता है।
← Class 11 Informatics Practices
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicfor Loop, range() और Nested Loops आपकी प्रगति: 6 / 20 (30%) · Lesson 7 / 20 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें