A concise explanation of if, else, and elif statement usage in Python

Source: Internet
Author: User
Let's learn the IF statement and enter the following code to make sure it works correctly.

People = cats = Dogs =   If people < cats:   print "Too many cats! The world is doomed! "   If people > Cats:   print "Not many cats! The world is saved! "   If people < dogs:   print "The World is drooled on!"   If people > Dogs:   print "The World is dry!"   Dogs + = 5   If people >= dogs:   print "People is greater than or equal to dogs."   If people <= dogs:   print "People is less than or equal to dogs."   if people = = Dogs:   


Run results

Too many cats! The world is doomed! The world is dry! People is greater than or equal to dogs. People is less than or equal to dogs. People is dogs.

Extra points Exercise
Through the above exercises, we ourselves guess the function of the IF statement, and answer the following question in our own words.
1. What do you think if you do the code below it?
Judge to True to execute the code below it, otherwise it will not execute.

2. Why should the code below indent 4 spaces?
To indicate that the code is included under if judgment.

3. What happens if I do not indent?
An indentation error is prompted.

4. Can you take some Boolean expressions from the 27th section to make an if judgment?

5. Change the value of the people,dogs,cats variable to see what happens?

Answer:
1. The code below the IF statement is a branch of IF. Like a chapter in a book, you choose this chapter to jump here to read. This if statement is like: "If Boolean evaluates to True, execute the following code, otherwise skip the code".

2. Ending a statement with a colon is to tell Python that I'm going to start a new code snippet. Indent 4 spaces that is, the code is contained in this code snippet, and the function is used.

3. Do not indent error, Python stipulates that the statement after the colon must be indented.

4. Yes, and can be complex statements.

5. After modifying the value of the variable, the judgment statement will change to true or false, and then output a different statement.

Compare my answers with your own answers and make sure you understand the concept of code blocks, as this is important for the following exercises.

Enter the following code to run it:

People = cars = Buses =   If cars > People:   print "We should take the cars." Elif Cars < people:   Print "We should not take the cars." Else:   print "We can ' t dicide."   If buses > Cars:   print "That's too many buses." Elif Buses < cars:   print "Maybe we could take the buses." El SE:   print "We still can ' t decide."   If people > buses:   print "Alright, let's just take the buses." Else:   


Run results

We should take the cars. Maybe we could take the buses. Alright, let's just take the buses.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.