Learning the path to Python: conditional statements

Source: Internet
Author: User

Conditional statements in Python, the IF statement is used for conditional judgment, in the following format:If you want to judge the condition:things to do when the conditions are set...It is important to indent in Python, to indent 4 spaces in the run content of the IF condition statement Walkthrough-Judging age Requirements
    1. Define an integer variable to record age
    2. Determine if you are over 18 years old (>=)
    3. If you are over 18 years old, allow access to Internet cafes

Output Results

If the else processing condition is not satisfiedIf you want to judge the condition:things to do when the conditions are set...Else:things to do when the conditions are not setThat is, execute else when the if decision is not met, and execute the code output in else. The contents of the preceding input (), and the assignment variable are used here. Oh, you can look at the previous content without understanding.

Supplement:
    • When the numeric data is judged directly, the non-0 number is true and 0 is false
    • Do you remember the bool type? 1 is true,0 is false yo!
Elif
    • A pair of if and else allows the code to execute two different results
    • But in development, you might want to perform multiple results in parallel, and you can use Elif
    • The syntax format is as follows:
if condition 1:condition 1 code that satisfies execution...elif Condition 2:code to execute when condition 2 is met...elif Condition 3:code to execute when condition 3 is met...Else:code that executes when none of the above conditions are metWhen multi-conditional judgment is used,

Note that both elif and else must be used in conjunction with if and cannot be used alone

Learning the path to Python: conditional statements

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.