# Complex Conditionals (and, or, and not)¶

We can also use `and`

and `or`

to join several logical expressions together as shown in the table below. An `or`

joining two expressions means that if *either* of the expressions is true, the whole expression is true. An `and`

used to join two expressions is only true if *both* expressions are true. A `not`

negates the logical value that follows it. If it was true, then a `not`

changes the result to false. If it was false, the `not`

changes the result to true.

Expression |
Meaning |
---|---|

(a < b) or (c < d) |
The whole expression is true if a is less than b or c is less than d. |

(a < b) and (c < d) |
The whole expression is true if a is less than b |

not a < b |
Only true if a is actually greater than or equal to b. The logical expression |

A common use of `and`

is to check that a value is in a range between a minimum value and a maximum value. For example, if you have asked a person to pick a number between 1 and 10 you can check for this using the following.

- 1 to 10
- This would be true if the second expression was x <= 10.
- 0 to 9
- This would be true if the first logical expression was x >= 0.
- 1 to 9
- The "condition true" will only be printed when x is greater than 0 and less than 10 so this is the range from 1 to 9.

csp-12-6-2: Given the code below, what describes the values of x that will cause the code to print “condition true”?

1if x > 0 and x < 10: 2 print ("condition true") 3print ("All done")

A common use of `or`

is to check if either one of two conditions are true. For example, a parent has told a teen that she can go out if she has cleaned her room or finished her homework. If either of these is true she can go out. In Python a value of `0`

means false and any non-zero value is true, but `1`

is often used for true.

- all values of x
- This will be true if x is greater than 0 or less than 10. This covers all possible values of x.
- 1 to 9
- This would be true if the logical expressions were joined with and instead of or.
- 0 to 9
- This would be true if the logical expressions were jointed with and instead of or and if the first logical expression was x >= 0.

csp-12-6-4: Given the code below, what describes the values of x that will cause the code to print “condition true”?

1if x > 0 or x < 10: 2 print ("condition true") 3print ("All done")