and, or and not¶and¶True with the Boolean and operator. gender = 'Female'
age = 70
if gender == 'Female' and age >= 65:
print('Senior female')
and operator:| expression1 | expression2 | expression1 and expression2 |
|---|---|---|
False |
False |
False |
False |
True |
False |
True |
False |
False |
True |
True |
True |
or¶True with the Boolean or operator.semester_average = 83
final_exam = 95
if semester_average >= 90 or final_exam >= 90:
print('Student gets an A')
or operator:| expression1 | expression2 | expression1 or expression2 |
|---|---|---|
False |
False |
False |
False |
True |
True |
True |
False |
True |
True |
True |
True |
and expression as soon as it knows whether the entire condition is False. or expression as soon as it knows whether the entire condition is True. and, make the condition that’s more likely to be False the leftmost condition. or operator expressions, make the condition that’s more likely to be True the leftmost condition. not¶grade = 87
if not grade == -1:
print('The next grade is', grade)
if grade != -1:
print('The next grade is', grade)
not operator. | expression | not expression |
|---|---|
False |
True |
True |
False |
| Operators | Grouping |
|---|---|
() |
left to right |
** |
right to left |
* / // % |
left to right |
+ - |
left to right |
< <= > >= == != |
left to right |
not |
left to right |
and |
left to right |
or |
left to right |
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 3 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.