Python conditional judgment

Source: Internet
Author: User

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 a Python program, with an IF statement:

Age = Theif age >=:    print('your-is ', age)     Print('adult')

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

You can also add an else statement to if, meaning if the If judgment is false, do not execute the IF content, go to the Else execution

Age = 3if age >=:    print('your-is ', age)     Print('adult')else:      Print(' YourAge was', age)    print(' teenager ')

Be careful not to write a colon less:.

Of course, the above judgment is very rough, can use Elif to do more detailed judgment

Age = 3ifAge >= 18:    Print('your age is', age)Print('Adult')elifAge >=6:    Print('teenager')Else:    Print('Kid')

Elif is an abbreviation for else if, there can be more than one elif, so the complete form of the IF statement is:

if (conditional judgment 1):     < execution 1>elif(conditional judgment 2):    < execution 2>elif(conditional judgment 3):    < execute 3>  else:    < execution 4>

If statement execution has a feature, it is judged from the top, if it is true on a certain judgment, the corresponding statement of the judgment is executed, ignoring the elif and else under the body, so, please test and explain why the following program printed teenager:

Age =If age >= 6:    print('teenager')  Elif Age >=18:    print('adult')  else:    print('kid')

If the condition can also be abbreviated, such as writing:

if x:     Print ('true')

As long as X is a non-0 value, a nonempty string, a non-empty list, and so on, it evaluates to true, otherwise false.

Further discussion on input

Finally see a problematic condition to judge, many students will use input () to read the user input, so that you can input, the program runs more interesting:

Birth = input ('Birth:')if birth <    :Print ('00 ago ') Else :     Print ('after 00 ')

Input 1982, result error.

This is because the data type returned by input () is STR,STR cannot be compared directly to an integer, and Str must first be converted to integers. Python provides the int () function to do the job:

s = input ('Birth:'= Int (s)if birth <:     Print ('00 ago ') Else :     Print ('after 00 ')

Run again to get the right results. But what if I enter ABC? And you get an error message.

The original int () function found that a string is not a valid number when the error occurs, and the program exits.

Python conditional judgment

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.