How to write conditional statements in Python 01: Single and multiple condition judgments

Source: Internet
Author: User
Python is a high-level scripting language that combines explanatory, compiled, interactive, and object-oriented. Python's design is highly readable, with English keywords often used in other languages, some punctuation in other languages, and it has a more distinctive grammatical structure than other languages. The conditional statement in Python, however, determines the code block to execute by executing the result of one or more statements (true or false). How do I write conditional statements in Python? Beginners who have just touched Python can use the following Figure (1.1)To understand the execution process of a conditional statement:

(Fig. 1.1)

(the Python program language specifies that any non-0 and non-null (NULL) values are true,0 or NULL is false.)

If statements are used in Python programming for control program execution , the basic form is as follows:

If judgment condition:         Execute statement ... else:         EXECUTE statement ....

Where the "judging condition" is established (not 0), the following statements are executed, and the execution content can be multiple lines, in order to be indented to differentiate the same range. Else is an optional statement that executes when the content needs to be executed when the condition is not established.

conditional statement notation in Python specific examples are as follows:

#!/usr/bin/python#-*-coding:utf-8-*-flag = falsename = ' Luren ' if name = = ' Python ':         # Judgment variable no is ' python '    flag = Tru E          # Set the flag to true    print ' welcome boss ' # and output the welcome message when the condition is established    else: the              output variable name when the print name # condition is not true

The above example outputs the following results:

Luren      #输出结果

and when you need to judge more than one value , you can use the following form:

If judgment condition 1:    EXECUTE statement 1......elif judgment Condition 2:    EXECUTE statement 2......elif judgment Condition 3: Execute statement    3......else:    EXECUTE statement 4 ...

Let me give you an example:

#!/usr/bin/python#-*-Coding:utf-8-*-num = 5     If num = = 3:            # Determine NUM's value    print ' boss '        elif num = = 2:    pri  NT ' user ' elif num = = 1:    print ' worker ' Elif num < 0:           # value less than zero output    print ' error ' else:    print ' Roadman '     # Output when conditions are not true

The output value of this example is as follows:

Roadman      #输出结果

( since Python does not support switch statements, multiple criteria can be determined, only with elif )

The above example can be understood as the output of the variable will be output "Boss", and if the first condition is not satisfied then will enter the second condition to be judged, so as to do the analogy, when all the conditions are not satisfied when the output "Roadman".

Today's article is about a single condition in a python conditional statement and multiple conditions, and hopefully the above code will give you something to gain. In addition to these two situations, there is another situation in the conditional statement, and I will explain the other things in the next article.


"Recommended"

python 02: Simultaneous judgment with a simple statement group

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.