python-Boolean expression

Source: Internet
Author: User

1. Boolean expressions
Both conditional and circular statements use Boolean expressions as criteria

Boolean value true or FALSE, denoted by false and true, Front often uses a Boolean expression to compare two values, such as: while  X>=0 

2, Boolean operator
(1) Boolean operator: And,or and not

the Boolean operators and and or are used to combine two Boolean expressions and produce a A Boolean result

<expr> <expr>

<expr> or <expr>

The NOT operator is a unary operator used to evaluate a Boolean expression type of anti-

Not <expr>

(2)the precedence of Boolean operators in Python, in order from high to low, is not , and the lowest is or.

so the above formula equals the following version of parentheses

Squash Match Scoring Example

A and B represent the score of two squash players

Rule 1: As long as a player reaches 15 points, the game is over; if one party hits seven points and the other has no points, the game will end.

if or b==15)orand or and b==7):    print('  end of game ')

Rule 2: Requires a team to win at least two points to win, that is, one teams have reached 15 points, and the score difference is at least 2 o'clock at the end of the match

if or b==15)orandor and b==7):    print(' The game continues . ')

(3) Boolean algebra

(4) Boolean expression as a decision

continue as long as the user responds to a "Y" program. For The user to enter an uppercase or lowercase, the following loops can be used:

For sequence types, an empty sequence is interpreted as false, and any non-empty sequence be instructed to be true

1>>>bool (0)2 False3>>> BOOL (1)4 True5>>> BOOL (32)6 True7>>>bool (y)8 Traceback (most recent):9File"<pyshell#6>", Line 1,inch<module>Ten bool (y) OneNameerror:name'y'  is  notdefined A>>> BOOL ('y') - True ->>> BOOL ("') the False ->>>bool ([]) -False

Here it can be explained that the following procedure is to judge response[0] equals y, or y, because Y is always true in the bool type, so always meet the conditions, forming a dead loop. Note the splitting and grouping of Boolean conditions.

3. Boolean expression-concise representation

(1) If the user simply knocks the ENTER key, you can use the value in square brackets as the default recognition value  

1 ans=input ("What sports does youWant[football]:")2if ans!="":3     sport=ans4Else:5      sport='football'6print(sport)

(2) Boolean statement simplification

1 ans=input ("What sports does youWant[football]:")2if ans: 3     sport=ans4Else:5     sport='Football  '6print(sport)

Remove! "=" after, as long as the judgment of the BOOL statement is true, it executes.

(3) Simplified

1 ans=input ("What sports does youWant[football]:")2or ' Football ' 3 Print (sport)

python-Boolean expression

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.