Conditional tests allow you to examine various conditions in programming. They evaluate as either True or False.
These tests are used in if statements to decide what actions to take. If the test evaluates as True, Python executes the code in the if block otherwise, it ignores the code.
Conditional tests are also called Boolean expressions. True and False are Boolean values.
Test for Equality
The equality operator (==) is used for comparison. This is different from the assignment operator (=). It returns True when the value on the left matches the value on the right.
Let’s assign values to different variables and check for equality.
As seen from the output, the values stored in these variables don’t match.
Equality testing is case sensitive. ‘denver’ and ‘Denver’ are different values
A way to counteract this behavior is to use the lower method. This way, every case is handled as long as the values contain the same character combination.
These rules can be enforced in websites that handle user data. Imagine an existing user on a website bears ‘Smith’. Any new user who tries to use ‘smith’ or any other variation of the name would be rejected.
Test for Inequality
The not equal operator (!=) is used to check for inequality. The exclamation symbol indicates not. It returns True when the value on the left does not match the value on the right.
Comparing Numerical Values
Consider the dating app/website from the last article. You might want only 18-year-old visitors to register.
To check if a visitor is 18:
To check if a visitor is not 18:
However, this test does not capture all possible use cases. The test only checks for 18 year olds. But we also want visitors above 18 to be able to register.
Mathematical comparison operators can be used in conditional tests.
< represents less than
> represents greater than
<= represents less than or equal to
>= represents greater than or equal to
>= is suitable for our use case scenario. It captures all users equal to or above 18.
Test for Multiple Conditions
and Keyword
This returns True when all the conditions evaluate to True. We’ll test two conditions for simplicity.
Consider the Dating app/website discussed earlier. We might want to display certain matches to certain people based on location and height.
Let’s say we want to display profiles of users from Denver and height of 6ft and above to a particular user.
Each condition has been nested in parentheses to improve readability.
or Keyword
This returns True when any of the conditions evaluates to True.
in Operator
This is used to check for the presence of a value in a sequence. This sequence can be a string, list, tuple or dictionary.
Say we have a list of existing users of the dating website. When a new user tries to register, we check the list to make sure the username does not exist.
In the next article, we’ll start writing if statements by employing conditional tests.
Summary
- Conditional tests allow you to examine various conditions in programming. They are also called Boolean expressions and they always evaluate to Boolean values, True or False
- The test for equality is case sensitive. ‘Drake’ is not the same as ‘drake’
- All conditions must be True to get a True value when the and keyword is used
- At least one condition must be True to get a True value when the or keyword is used
- The in operator is used to check for the presence of a value in a sequence
No comments: