Python में If Statement क्या होता है?
Programming में कई बार हमें किसी condition (शर्त) को check करना पड़ता है। अगर condition सही हो तो एक काम करना है और गलत हो तो दूसरा काम करना है। इसी काम के लिए Python में if statement का उपयोग किया जाता है।
Chapter 5 – If Statements
Python का if statement program को decision लेने की power देता है। जैसे:
- अगर password सही है → Login करो
- अगर age 18 से ज्यादा है → Access दो
- अगर internet connected है → Website open करो
यानी Python condition के हिसाब से action लेता है।
If Statement का Basic Syntax
if condition:
code
Example
age = 18
if age >= 18:
print(“You are eligible to vote”)
Output
You are eligible
Explanation
यहाँ Python check कर रहा है:
age >= 18
अगर condition True है तो print चलेगा।
If Else Statement
अगर condition गलत हो जाए तो else use करते हैं।
Syntax
if condition:
code
else:
code
Example
age = 15
if age >= 18:
print(“Adult”)
else:
print(“Minor”)
Output
Minor
Explanation
क्योंकि age 18 से कम है इसलिए else वाला code run हुआ।
Real Life Example – Login System
password = “cyber123”
if password == “cyber123”:
print(“Access Granted”)
else:
print(“Wrong Password”)
Output
Access Granted
Multiple Conditions with elif
जब कई conditions हों तब elif use करते हैं।
Example
marks = 75
if marks >= 90:
print(“Grade A”)
elif marks >= 70:
print(“Grade B”)
elif marks >= 50:
print(“Grade C”)
else:
print(“Fail”)
Output
Grade B
Book वाला Example Explained
यह example list में मौजूद car names को print करता है। लेकिन अगर car का नाम bmw हो तो उसे uppercase में print किया जाता है।
Code
cars = [‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
for car in cars:
if car == ‘bmw’:
print(car.upper())
else:
print(car.title())
Output
Audi
BMW
Subaru
Toyota
Step-by-Step Explanation
1. List बनाई गई
cars = [‘audi’, ‘bmw’, ‘subaru’, ‘toyota’]
यहाँ 4 car names store किए गए हैं।
2. Loop Start हुआ
for car in cars:
Loop list के हर item को एक-एक करके check करेगा।
3. Condition Check हुई
if car == ‘bmw’:
#अगर current car bmw है तो:
print(car.upper())
यानि BMW बड़े letters में print होगा।
4. बाकी Cars
else:
print(car.title())
बाकी cars को title format में print किया जाएगा।
Equality Check in Python
Python में == operator equality check करने के लिए use होता है।
Example
car = ‘bmw’
print(car == ‘bmw’)
Output
True
Explanation
यहाँ Python check कर रहा है कि:
car == ‘bmw’
अगर दोनों values same हैं तो result True आता है।
Important Comparison Operators
| Operator | Meaning |
| == | Equal |
| != | Not Equal |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than Equal |
| <= | Less Than Equal |
Practice Questions
Question 1
num = 10
if num > 5:
print(“Number is greater”)
Output
Number is greater
Question 2
username = “admin”
if username == “admin”:
print(“Welcome Admin”)
else:
print(“Access Denied”)
Output
Welcome Admin
Common Mistakes Beginners करते हैं
1. Colon भूल जाना
❌ Wrong
if age > 18
✅ Correct
if age > 18:
2. Indentation Error
❌ Wrong
if age > 18:
print(“Adult”)
✅ Correct
if age > 18:
print(“Adult”)
Multiple if vs Multiple elif in Python
Python में if और elif दोनों conditions check करने के लिए use होते हैं, लेकिन इनके काम करने का तरीका अलग होता है।
1. Multiple if Statement
जब आप कई if statements अलग-अलग लिखते हैं, तब Python हर condition को check करता है — चाहे पहले वाली True हो चुकी हो।
Example
age = 20
if age >= 18:
print(“Adult”)
if age >= 15:
print(“Teenager”)
if age >= 10:
print(“Kid”)
Output
Adult
Teenager
Kid
Explanation
यहाँ age = 20 है।
Python क्या कर रहा है?
- age >= 18 → True
- age >= 15 → True
- age >= 10 → True
इसलिए तीनों print चले।
2. Multiple elif Statement
elif chain में Python ऊपर से नीचे condition check करता है।
जैसे ही पहली True condition मिलती है, बाकी conditions check नहीं होतीं।
Example
age = 20
if age >= 18:
print(“Adult”)
elif age >= 15:
print(“Teenager”)
elif age >= 10:
print(“Kid”)
Output
Adult
Explanation
यहाँ पहली condition ही True हो गई:
age >= 18
तो Python नीचे वाले elif को check ही नहीं करेगा।
Main Difference
| Multiple if | if-elif |
| सभी conditions check होती हैं | पहली True condition के बाद stop |
| कई outputs आ सकते हैं | सिर्फ एक output आता है |
| Independent conditions | Connected conditions |
| ज्यादा processing | Faster |
Real Life Example
Multiple if
marks = 95
if marks >= 90:
print(“A Grade”)
if marks >= 80:
print(“Good Marks”)
Output
A Grade
Good Marks
क्योंकि दोनों conditions True हैं।
if-elif
marks = 95
if marks >= 90:
print(“A Grade”)
elif marks >= 80:
print(“Good Marks”)
Output
A Grade
यहाँ पहली condition True होते ही program रुक गया।
कब कौन use करें?
Multiple if
जब हर condition अलग-अलग important हो।
Example:
- Login check
- Internet check
- Battery check
सबको check करना जरूरी है।
if-elif
जब सिर्फ एक option choose करना हो।
Example:
- Grade system
- Menu selection
- Traffic signal
एक बार answer मिल गया तो बाकी check करने की जरूरत नहीं।
Easy Trick
if
👉 “सबको check करो”
elif
👉 “पहला सही मिले तो रुक जाओ”
Visual Flow
Multiple if
Check 1 ✔
Check 2 ✔
Check 3 ✔
if-elif
Check 1 ✔
STOP
Final Summary
Python का if statement conditions को check करने के लिए use होता है। यह program को smart बनाता है ताकि वह situation के हिसाब से अलग-अलग actions ले सके।
इस chapter में आपने सीखा:
- if statement
- if else
- elif
- equality check
- comparison operators
- loops के साथ if condition
Cyber-Teck Tips
✅ Hacking Tools में conditions बहुत use होती हैं
✅ Login systems, bots, automation scripts सब में if statements का उपयोग होता है
✅ Python सीखने का सबसे important step है conditions को समझना

