Python Learning: If statement

Source: Internet
Author: User

First example:
Age = 20if Age >=:    print ' adult ' elif age < 18:    


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

For example, enter the age of the user, print different content according to age, in the Python program, with the if statement implementation:

age = 20if age >= 18:    print ‘your age is‘, age    print ‘adult‘

According to the indentation rules of Python, if the if statement is judged True , the indented two lines of the print statement are executed, otherwise, nothing is done.

can also giveifAdd aelseStatement, meaning that ififJudgment isFalse, do not performifThe content, go to theelseCarried out:

age = 3if age >= 18:    print ‘your age is‘, age    print ‘adult‘else:    print ‘your age is‘, age    print ‘teenager‘

Be careful not to write a colon less : .

Of course, the above judgment is very sketchy, can be used to elif do more detailed judgment:

age = 3if age >= 18:    print ‘adult‘elif age >= 6:    print ‘teenager‘else:    print ‘kid‘

elifIselse ifThe abbreviation, can have more than oneelifSoifThe complete form of the statement is:

if <条件判断1>:    <执行1>elif <条件判断2>:    <执行2>elif <条件判断3>:    <执行3>else:    <执行4>

ifStatement execution has a feature, which is judged from the top down, if in a certain judgment isTrue, after executing the statement of the judgment, it ignores the remainingelifAndelse, so, please test and explain why the following program prints theteenager

age = 20if age >= 6:    print ‘teenager‘elif age >= 18:    print ‘adult‘else:    print ‘kid‘

ifThe judging conditions can also be abbreviated, such as writing:

if x:print ' True '  

As long as a non- x 0 value, a non-empty string, a non-empty list, etc., it is judged True , otherwise False .


Python Learning: 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.