Python branch--if

Source: Internet
Author: User

Single Branch judgment

Age = 16
If age >=:                                 judge the sentence, determine whether the age is greater than or equal to 18, note if you want to add a space after the condition is written to add:
Print ("You are an adult")
Print ("Can go to the Internet")

Print ("Hurry Home for Dinner")

The above code is a single branch of judgment, an if is enough to meet the conditions on the printing, do not meet the directly run the following code.

Double-branch judgment

Age = 16
If age >= 18:
# if adult, print: You are already an adult and can go online
Print ("You are an adult, you can go online")

# if underage, print: Hurry home and eat, grow up fast
else: How else, pay attention to add:
Print ("Hurry home and eat, grow up fast")

And the difference between a single branch is more than one else (otherwise), the entire code means that if the conditions are met on the print "You have ... "Not satisfied on the print" hurried home ... ”

Multi-branch judgment

Score = 80
If <= score <= 100:
Print ("excellent")
Elif <= Score <: If you have if nested, you can write this form elif is equivalent to else+if abbreviation
Print ("good")
Elif <= Score < 70:
Print ("Pass")
else: There is no need to judge down here, the above is not satisfied is the failure of the
Print ("Failed")

Pay attention to the syntax structure, and later design to if nesting is this form

Forced indentation, which branch the member's code block belongs to, and the shortcut key for the retraction is Shift+tab



Python branch--if

Related Article

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.