# The usage of the if, else, And elif statements in Python is concise, and pythonelif

Source: Internet
Author: User

The usage of the if, else, And elif statements in Python is concise, and pythonelif

Next, we will learn the if statement and enter the following code to ensure proper running.

`people = 20 cats = 30 dogs = 15   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 are greater than or equal to dogs."   if people <= dogs:   print "People are less than or equal to dogs."   if people == dogs:   print "People are dogs." `

Running result

`root@he-desktop:~/mystuff# python ex29.py `
`Too many cats! The world is doomed!The world is dry!People are greater than or equal to dogs.People are less than or equal to dogs.People are dogs.`

Extra score exercise
Through the above exercises, we can guess the role of the if statement and use our own words to answer the following questions.
1. What do you think if has done to the code below it?
If it is True, the following code is executed; otherwise, the Code is not executed.

2. Why should the code below if be indented with four spaces?
To indicate that the code is included in the if judgment.

3. What will happen if it is not indented?
An indent error is prompted.

4. Can you use some boolean expressions in section 27th to determine if?

5. Change the value of the people, dogs, and cats variables to see what will happen?

1. The code below the if statement is a branch of the if statement. Just like a chapter in the book, you can skip this chapter to read it. This if statement is like saying: "if Boolean is True, execute the following code; otherwise, skip the code ".

2. End a statement with a colon to tell python that I want to start a new code segment. Four spaces are indented, that is, the code is included in this code segment, which is the same as the use of functions.

3. If it is not indented, an error is returned. python requires that the statement after the colon must be indented.

4. Yes, and it can be complex statements.

5. After the value of the variable is modified, the True or False statements are determined accordingly, and different statements are output.

Compare my answers with your own answers to ensure that you can understand the concept of code blocks, because this is very important for the following exercises.

Enter the following code and run it:

`people = 30 cars = 40 buses = 15   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." else:   print "We still can't decide."   if people > buses:   print "Alright, let's just take the buses." else:   print "Fine, let's stay home then." `

Running result

`root@he-desktop:~/mystuff# python ex30.py `
`We should take the cars.Maybe we could take the buses.Alright, let's just take the buses.`
Articles you may be interested in:
• Detailed explanation of condition statements and operator priority in Python
• Simple usage of condition judgment statements in Python
• Explain the condition judgment statements in Python
• True and False conditions in Python determine instance analysis
• Conditions and loops for getting started with Python
• Analysis of conditions and cyclic control instances of Python3
• Python array filter function example
• Python dictionary multi-condition sorting method example
• How to Use python conditions and loops
• Introduction to Condition Selection and circular statement usage in Python
• Abbreviation of condition judgment in Python

Related Keywords:
Related Article

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.