Python if…..else Statement

If…else is nothing but a decision taken by the programmer what to do in the particular condition.i.e nothing but either or situation in general.

In this article we explained in a very simple way so that anybody can understand without any confusion .Just follow the whole article to get full understanding.

Python if…else:

Below are the different  condition in python if.

Syntax of if.. else:

 

if condition: 
    # execute these statements 
else: 
    # execute these statements

Example:

var1 = 10
var2 = 20
if var1 > var2:
    print('var1 is greater')
else:
    print('var2 is greater')

Output:

var2 is greater

Explanation:

  • In above example we declared two variable  var1,var2
  • Then we just check our condition which one is greater by giving > operator
  • we saw the condition fail so else block executed as output.

Now you learn if.. else but there is one more condition called “elif” , suppose you wants to check more than one condition then you can use “elif”.

Lets follow this to know more.

Syntax of if…… elif…….else:
If condition:
    # execute statement
elif condition:
      # execute statement
elif condition:
      # execute statement
else:
    # execute statement

Example:

age = 33

if (age>=18) and (age < 60):
    print('You can vote by using general queue')
elif age >= 60:
    print('You can give a vote by using a special queue')
else:
    print('You are not eligible for vote')

Output:

You can vote by using general queue

Explanation:

  • As you saw above we used two condition in if , you can use multiple condition in one statement like this.
  • Next we used elif condition , if first  condition fail then only it will come to elif.Here our first condition meet so elif and else block is not executed and finish the programm
  • You can play with this code to execute each and every block.


You can use if within if that is called nested if .

It will go step by step inside if block and check where the condition met and execute that.

Syntax of Nested if…

 

if condition:
    # execute statement
    if condition:
      # execute statement
    if condition:
      # execute statement
else:
    # execute statement

 

Example:

age = 29
if age > 18:
    #execute below if
    if age  >= 60:
        print('You can give a vote by using a special queue')
    if age <=30:
        print('Go to  queue number 2')
    if age > 30:
        print('Go to  queue number 3')

else:
    print ('Not eligible to vote')

Output

Go to  queue number 2

Explanation:

  1. first it will check if age > 18:
  2. Then if age >= 60: check this and go to next if.
  3. Then check if age <=30: enter that and stop there .
  4. Then it will go to if age > 30: and condition fail , so it exits from that flow and end our program.

So is not it very simple to understand how it works.

Hope you like the flow how if else works.

Share with your friends and colleagues , comment in the below section to get more.

You May like some useful links in Python:
Join our Python crash course to grow your career.

6 thoughts on “Python if…..else Statement”

  1. You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!

  2. I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.

  3. This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!

  4. I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.

  5. This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!

  6. Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.

  7. This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!

  8. naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

  9. I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.

  10. This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!

  11. I’m often to blogging and i really appreciate your content. The article has actually peaks my interest. I’m going to bookmark your web site and maintain checking for brand spanking new information.

  12. whoah this weblog is excellent i like studying your articles.
    Stay up the great work! You know, a lot of persons are searching round for this
    info, you could help them greatly.

  13. It’s a pity you don’t have a donate button! I’d without a
    doubt donate to this superb blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed
    to my Google account. I look forward to brand new updates and will share this blog with my Facebook group.
    Talk soon!

  14. Your passion for this subject shines through in your writing. It’s clear you care deeply about this issue and want to make a difference.

  15. My spouse and I stumbled over here by a different web
    page and thought I might as well check things out.

    I like what I see so now i am following you. Look forward to exploring your web
    page for a second time.

    Here is my homepage: Local SEO

  16. fantastic submit, very informative. I wonder why the other specialists
    of this sector do not notice this. You should continue your
    writing. I am sure, you have a great readers’ base already!

  17. Your article is a great reminder of why this issue is so important. It’s easy to lose sight of the big picture, but you’ve helped me keep it in focus.

  18. Everything wrote was actually very logical. But, think about this, what if you were to write a
    killer post title? I mean, I don’t want to tell you how to run your blog, but suppose you
    added something that makes people desire more? I mean Python if…..else
    Statement – PyGround is a little boring. You could glance at Yahoo’s home page and see how they write article titles
    to grab people to open the links. You might add a video or a related picture
    or two to get people interested about what you’ve got to say.
    In my opinion, it could make your posts a little bit more interesting.

  19. Hello there, I discovered your web site by means of Google at
    the same time as searching for a similar matter,
    your website came up, it seems to be great.

    I’ve bookmarked it in my google bookmarks.
    Hello there, just turned into alert to your blog through Google, and found that it’s really informative.
    I am gonna watch out for brussels. I’ll be grateful when you
    proceed this in future. A lot of people shall be benefited
    out of your writing. Cheers!

  20. I’m always looking for web sites that sell real bags. I’ve purchased several bags over the last year. as well the bags are always perfect, the shipping time is very prompt, and the communication is excellent. such I highly recommend this site to anyone who’s looking for real bags. no matter what what I especially like it is the information they have on their Release Dates page. This really helps me stay in tune with what’s going on in the world of bags.
    authentic louis vuitton outlet https://www.louisvuittonsoutlet.com/

  21. This site is the truth. I’m always on checking release dates, but I never bought from this site. properly A lot of people said good things, so I ordered a pair and they were delivered to my door before I could even buy clothes to go with the sneakers. similar to the reduction Very fast shipping. you ultimately choose You guys have a loyal customer.
    jordans for cheap https://www.cheapjordanssneakers.com/

  22. This site is the truth. I’m always on checking release dates, but I never bought from this site. also A lot of people said good things, so I ordered a pair and they were delivered to my door before I could even buy clothes to go with the sneakers. choose to Very fast shipping. situation You guys have a loyal customer.
    cheap jordans for sale https://www.airretrojordans.com/

  23. naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

  24. Thanks for the auspicious writeup. It if truth be told
    used to be a enjoyment account it. Glance complex to more brought agreeable from you!
    However, how could we be in contact?

  25. หากคุณมองหา เว็บหวย
    ที่ราคาดี เชื่อถือได้ เราแนะนำ
    หวยนาคา เว็บหวยออนไลน์ ที่จ่ายหนักที่สุด
    3ตัวบาทละ 960
    2ตัวบาทละ 97

  26. naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.

  27. Heya exceptional website! Does running a blog such as this take a massive amount work?
    I have no knowledge of computer programming but I had been hoping to start my own blog in the near future.
    Anyway, if you have any ideas or tips for new blog owners please share.
    I know this is off subject but I just had to ask. Kudos!

  28. Heya just wanted to give you a brief heads up
    and let you know a few of the images aren’t loading correctly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different browsers and both show
    the same outcome.

  29. I’m amazed, I have to admit. Seldom do I come across a blog
    that’s both equally educative and engaging, and let me tell you, you have
    hit the nail on the head. The problem is something that not enough people are speaking intelligently about.
    Now i’m very happy I came across this in my search for something relating to
    this.

  30. I think the content you share is interesting, but for me there is still something missing, because the things discussed above are not important to talk about today.

  31. Renal function, medications, and comorbid conditions such as diabetes with a risk of both insulinopenia and autonomic neuropathy should also be considered to gauge the risk of overcorrection cialis 20mg price 2 weeks or less, with diclofenac potassium tablets, adverse reactions were reported one half to one tenth as frequently as by patients treated for longer periods

  32. I do not even know how I ended up here, but I thought this post was great. I don’t know who you are but definitely you’re going to a famous blogger if you aren’t already 😉 Cheers!

  33. I am a student of BAK College. The recent paper competition gave me a lot of headaches, and I checked a lot of information. Finally, after reading your article, it suddenly dawned on me that I can still have such an idea. grateful. But I still have some questions, hope you can help me.

  34. Your article is a valuable resource for anyone looking to gain a deeper understanding of this topic. Thank you for sharing your knowledge.