How to write conditional statements in Python 02: Simultaneous judgment with a simple statement group

Source: Internet
Author: User
In the previous article we explained the condition of a single condition and multiple conditions in a conditional statement. For today's article, let's take a look at some of the remaining scenarios in the Python conditional statement.

if it is judged that multiple conditions need to be judged at the same time, the or (or) can be used to indicate that two conditions have a successful condition, and when using and (with), it means that only two conditions have been established and the condition is successful . Examples of simultaneous judgments are as follows:

#!/usr/bin/python#-*-coding:utf-8-*-num = 9if num >= 0 and num <=:    # Determine if the value is between 0~10    print ' hello ' # output knot Result: Hello num = 10if num < 0 or num >:    # Determine if the value is less than 0 or greater than the    print ' hello ' else:    print ' undefine ' # output: und Efine num = 8# Determine if the value is between 0~5 or 10~15 if (num >= 0 and Num <= 5) or (Num >= and num <=):        print ' Hello ' els E:    print ' undefine ' # output: undefine

If there are multiple conditions can use parentheses to distinguish the order of judgment, the judgment in parentheses precedence execution, and and and or priority is lower than > (greater than), < (less than), such as the judgment symbol, that is greater than and less than in the case without parentheses will be compared with or priority to judge .

In addition to the multiple conditions mentioned above, and the two cases mentioned in the previous article, there is a simple statement group . You can use the IF condition judgment statement on the same line, as in the following example:

#!/usr/bin/python #-*-Coding:utf-8-*-var =  if (var  = =): print "Variable var value is"  print "good bye! "

Like this, the output will be as follows:

The value of variable var is 100Good bye!

(If statements can be judged with > (greater than), < (less than), = = (equals), >= (greater than or equal), <= (less than equals) to indicate their relationship. )

The above is the Python conditional statement of the other two cases, combined with two of the previous article, a total of four cases of Python conditional statements for example and description, I hope you have just learned Python help you, to help you learn python easier.

"Recommended"

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

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.