Python's If statement

Source: Internet
Author: User

Computers can do a lot of automated tasks, because it can make their own conditions to judge.

For example, to enter user age, print different content according to age, in a Python program, you can use the if statement:

Age = 20if Age >=:    print ' Your-age ', age    print ' adult ' print ' END '

Note: The indentation rules for Python code. Code with the same indentation is treated as a block of code, with 3, 4 lines of print statements constituting a block of code (but excluding print on line 5th). If the IF statement evaluates to True, the code block is executed.

Indent please strictly follow Python's customary notation:4 spaces, do not use tab, do not Mix tab and space, otherwise it is easy to cause syntax errors due to indentation.

Note : The IF statement is followed by an expression, and then begins with ' :‘ representing the code block.

If you hit the code in a Python interactive environment, also pay special attention to indentation, and exit indentation requires more than one line of return:

>>> age = 20>>> if age >=:     ... print ' Your age was ',     age ... print ' adult ' ... your age is 20adult

If the score reaches 60 or more, it is considered passed.

Let's say that Bart's score is 75, use the IF statement to determine if passed can be printed:

Reference code:

Score = 75
If score>=60:
print ' passed '

The If-else of Python

When the if statement determines that the result of the expression is True, the IF-contained code block is executed:

If age >=:    print ' adult '

What if we want to judge the age under 18 and print out ' teenager '?

The method is to write another if:

If age <:    print ' teenager '

Or use the NOT operation:

If not age >=:    print ' teenager '

Careful classmates can find that these two conditions are either "or", or meet the condition 1, or meet the condition 2, so you can use an if ... else ... Statements to unify them together:

If age >=:    print ' adult ' else:    print ' teenager ' 

Use if ... else ... Statement, we can execute an if code block or an else code block, depending on the value of the conditional expression, either True or False.

Note: else there is a ":" later.

If the achievement reaches 60 points or above, treats as passed, otherwise treats as failed.

Let's say that Bart's score is 55, use the IF statement to print out passed or failed:

Reference code:

Score = 55
If score>=60:
print ' passed '
Else
print ' failed '

The If-elif-else of Python

Sometimes, an if ... else ... It's not enough. For example, according to the age of the division:

Condition 1: Age 18 or above: Adult condition 2:6 years old or above: Teenager Condition 3: Under 6 years of age: Kid

We can use an if age >= 18 to determine whether the condition 1, if not, and then through an if to determine the age >= 6来 to determine whether to meet the condition 2, otherwise, the execution condition 3:

If age >=:    print ' adult 'else:    if >= 6: print '        teenager '    else:        print ' Kid ' /c7> 

To write this out, we get a two-layer nested if ... else ... Statement. There is no problem with this logic, but if you continue to add conditions, such as baby under age 3:

If age >=:    print ' adult 'else:    If->= 6:        print ' teenager '    else: If age        &G t;= 3:            print ' Kid '        else:print ' baby '    

This indentation will only get more and more and the code will become more and more ugly.

To avoid nesting structures if ... else ..., we can use the if ... Multiple elif ... else ... Structure, write all the rules at once:

If age >=:    print ' adult 'elif age >= 6:    print ' teenager ' elif age>= 3: Print ' Kid 'else:print ' baby '     

Elif means else if. In this way, we write a series of conditional judgments that are very clear in structure.

Special attention: This series of conditional judgment will be judged from top to bottom, if a certain judgment is True, after executing the corresponding code block, the subsequent condition judgment is ignored and no longer executed.

Consider the following code:
Age = 8if Age >= 6:    print ' teenager ' elif age >=:    print ' adult ' else:    print ' Kid '

When age = 8 o'clock, the result is correct, but Age = 20 o'clock, why is adult not printed?

What should I do if I want to fix it? Because the Elif series of conditions from top to bottom in order to judge, once a certain value is true, then stop judging the following conditions and code block, when Age=20, meet the first loop, direct output ' teenager ', will not output ' adult '. So you can make a change:

Age = 8
If age >= 18:
print ' adult '
Elif Age >= 6:
print ' teenager '
Else
print ' Kid '

If the result is delimited by fractions:

90 points or more: excellent

80 points or more: good

60 points or more: passed

60 min.: Failed

Write your program to print the results based on the score. Refer to the code:

Score = 85
If score>=90:
print ' excellent '
Elif score>=80:
print ' good '
Elif score>=60:
print ' passed '
Else
print ' failed '

Python's If statement

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.