If Else statement किसी भी programming language का सबसे important feature है , Python में if else का use condition के according अलग अलग Code Of Block को run करने के लिए किया जाता है। Means जब हमें Condition True होने पर कोई दूसरा code run करना हो Condition गलत होने पर कुछ और तब हम If else का use करते हैं।

Python if else Syntax

if condition :
    #do whatever you want
else :
    #do whatever you want

हालाँकि condition को आप parenthesis ( if(condition) ) में रखें या न रखें उससे फर्क नहीं पड़ता है।

Python if else Example

Copy Fullscreen Close Fullscreen Run
age = 18
if age >= 18 :
	print("You are eligible to vote")
else :
	print("You are not eligible to vote") 
Output
C:\Users\Rahulkumar\Desktop\python>python if_else.py
You are eligible to vote

तो ऊपर दिए गए example में आप देख सकते हैं की Python में If Else किस तरह से use करते हैं।

चूंकि Python case sensitive language इसलिए predefined keywords करते समय यह ध्यान रखें कि if / else ही लिखे न कि If / Else, IF / ELSE.


if() condition में आप कोई भी Python valid value like (number , string , etc ) दे सकते हैं। Python उन्हें कुशः इस तरह से True / False treat करता है।

Condition / value Value
0 False
-1, -2 .... 1,2 except 0True
empty string like : "" or ' ' (without space)False
non empty string like : 'me', 'rahul' or '   ' (with at least one space)True
empty objectFalse
non empty objectTrue

See Example

Copy Fullscreen Close Fullscreen Run
if  -4:
	print('-4 :',True)
else :
	print(False)

if  5:
	print('5 :', True)
else :
	print(False)

if  0:
	print(True)
else :
	print('0 :',False)

if  '    ':
	print('empty string with spaces :',True)
else :
	print(False)

if  "":
	print(True)
else :
	print('empty string without space :',False)

if  'hi':
	print('hi :', True)
else :
	print(False)
 
Output
C:\Users\Rahulkumar\Desktop\python>python if_else.py
-4 : True
5 : True
0 : False
empty string with spaces : True
empty string without space : False
hi : True

Pytohn Short Hand if / else

अभी तक आपने जो if / else पढ़ा वो multiple lines of code के लिए था , अब अगर आप कोई single line apply करना चाहते हो तो इसे आप एक single लाइन में ही लिख सकते हैं।
For Example -

Short Hand if

Copy Fullscreen Close Fullscreen Run
age = 89
if age >= 18 : print("You are eligible to vote")
Output
C:\Users\Rahulkumar\Desktop\python>python if_else.py
You are eligible to vote

Short Hand if else

Copy Fullscreen Close Fullscreen Run
age = 12
print("You are eligible to vote") if age >= 18 else print("You are not eligible to vote")
Output
C:\Users\Rahulkumar\Desktop\python>python if_else.py
You are not eligible to vote

Python if elif

ऊपर दिए गए दोनों statements (if और if else) को हम तब use करते हैं जब कोई single condition के accordingly code of block run करना हो , एक से ज्यादा condition के accordingly code of block run के लिए Python में If elif Statement use करते हैं।

Python if elif Example
Copy Fullscreen Close Fullscreen Run
marks  = 86
if(marks >= 90) :
  print('Grade : A+')
elif(marks >= 80) :
  print('Grade : A')
elif(marks >= 70):
  print('Grade : B')
else :
  print('Grade : C')
Output
C:\Users\Rahulkumar\Desktop\python>python if_elif.py
Grade : A

Other programming languages like : PHP , JavaScript , Java, C में एक से ज्यादा condition के according code of block run के लिए if-else if या if-elseif का use किया जाता है लेकिन python में If-elif का use किया जाता है।

I Hope,अब आप Python में if else के बारे में अच्छे से समझ गए होंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers