Logic Operators

And Operator


We use the and operator to execute certain code if more than one thing is true. For example:

screenshot-repl-it-2016-11-05-22-48-40If  neither or only one of these statements is true then the next line will not be run:

screenshot-repl-it-2016-11-05-22-51-43

If both statements are true then the next line can be executed:

screenshot-repl-it-2016-11-05-22-52-09

 

Or Operator


We use the or operator to execute certain code if one of several statements is true. For example:

screenshot-repl-it-2016-11-05-23-02-37

It is important that you re-write the variable each time you use the or operator (it will not work if you just write if vowel == “a” or “e” or “i” or “o” or “u”). Below is the program with two possible outputs:

screenshot-repl-it-2016-11-05-23-03-21 screenshot-repl-it-2016-11-05-23-02-56

Practice Task 1

Operators not working? Tick off these common errors:

Correct colon placement?
Have you indented properly?
Correct or statement

 < Back to Section Four