Python learning: if statement and pythonif statement

Source: Internet
Author: User

Python learning: if statement and pythonif statement

Example:
 
age = 20if age >= 18 :    print 'Adult'elif age < 18 :    print 'Nonage' 


A computer can perform a lot of automated tasks because it can perform conditional judgment on its own.

For example, input the user's age and print different content according to the age. In the Python program, useifStatement implementation:

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

According to the indentation rules of Python, ififStatement judgment isTrueAnd the Indented print statement is executed. Otherwise, nothing is done.

You can alsoifAddelseStatement, which means that ififYesFalse, Do not executeifToelseExecuted:

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

Do not write less colons.:.

Of course, the above judgment is very rough and can be used completely.elifMake a more detailed judgment:

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

elifYeselse ifCan have multipleelif, SoifThe complete statement form is:

If <condition judgment 1>: <execution 1> elif <condition Judgment 2>: <execution 2> elif <condition judgment 3 >:< execution 3> else: <execution 4>

ifStatement execution has the following characteristics: it is judged from top to bottom, if it isTrue, Ignore the remainingelifAndelseSo, test and explain why the following program printsteenager:

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

ifThe judgment condition can also be abbreviated, for example, write:

if x:    print 'True'

As longxIt is a non-zero value, non-empty string, non-empty list, etc.TrueOtherwiseFalse.


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.