Conditions
-
Boolean Operators: And, Or, Not
We can combine boolean values, or expressions like value < 3 or text == “Hello” that evaluate to a boolean value, using boolean operators. and and or are binary operators: the accept two arguments and return a boolean value. not is a unary operator. It accepts only one argument and returns a boolean value. Boolean…
-
Less Than, Greater Than
When working with numbers, we can easily check if one value is greater than another, or less than another. As with the equality test operator (==), these operators return True or False, so they can be used as part of an if statement, for example. We can also check if two things are not equal…
-
If … Elif … Else
The most general form off the Python if statement can check multiple conditions. The first condition to check comes after the if keyword, then we can check one or more further conditions using the elif keyword (short for “else if”). Python will work down the conditions until it finds one that’s true. If none of…
-
If … Else
The if-else construct allows us to execute some block of code if a condition is True, otherwise execute some other block of code. One of the two code blocks will always execute. It’s just a question of which one. Note that the program is case-sensitive. “Bob” must be entered with an uppercase “B” to be…
-
The “if” Keyword: Conditional Code Execution
Often we only want to execute some code if a condition is true. The following program only outputs “Hello Bob” if the user enters the password “hello”. Indentation The indentation here is very important. The statement that prints “Hello Bob” is indented four spaces to indicate that it’s controlled by the if statement. The bit…
-
Testing for Equality
Often we want to check if two things are equal to each other. We can do this with the equality test operator, == Note that we do not use single ‘=’ to test if two things are equal; the single equals sign is called the assignment operator and is only used to assign values to…