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.