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