Operators that are used to set logical conditions (Logical Operators) in Python
| the name of the employee | his code | Example | Explanation of the code |
|---|---|---|---|
| Logical AND | and | a and b | Are value a and b equal True ? Here, the two conditions must be met to return True |
| Logical OR | or | a or b | Is the value a or b or both equal True ? Here it is sufficient for one of the two conditions to be fulfilled to return True |
| Logical NOT | not | not a | Is the value a not worth True ? If the answer is yes, it will return True |
Here we put an example for each factor in the table
Explain operators that are used to set Boolean conditions in Python
Here is a set of examples of each operator that is used to set Boolean conditions in Python.
Boolean operator and (Logical AND Operator)
The operator and is used to execute a specific code if the first condition and the second condition are met.
That is, if the result of the first condition is equal
Trueand the result of the second condition is equal, theTruecode will be executed.If the result of both conditions is not equal
True, the code will not run.
first example
a = 10
b = 20
# equals 20 The print command b will be executed equal to 10, and the value of the variable a The next condition means that if the value of the variable
if a == 10 and b == 20:
print('The first and the second conditions return True')
• We will get the following result when running.
• We note that the printing command was executed because the answer to the two conditions in the sentence if is True.
second example
a = 10
b = 20
# equals 50 The print command b will be executed equal to 10, and the value of the variable a The next condition means that if the value of the variable
if a == 10 and b == 50:
print('The first and the second conditions return True')
• We will get the following result when running.
• We note that the print command was not executed because the answer to the second condition in the sentence if is False.
Boolean operator or (Logical OR Operator)
An operator or is used to execute a specific code if at least one of the conditions set is met.
So here it is sufficient for one of the two conditions to return the value True in order for the placed orders to be executed.
first example
a = 10
b = 20
# equals 50 will execute the print command b equals 10, or the value of the variable a The following condition means that if the value of the variable
if a == 10 or b == 50:
print('One of the conditions return True')
• We will get the following result when running.
• Execute the print command because the answer to the first condition in the sentence if is True.
second example
a = 10
b = 20
# equals 50 The print command b will be executed equal to 50, or the value of the variable a The next condition means that if the value of the variable
if a == 50 or b == 50:
print('One of the conditions return True')
• We will get the following result when running.
• We note that the printing command was not executed because the answer to the first and second conditions in the sentence if is False.
Boolean operator not (Logical NOT Operator)
An operator not is used to execute a specific code if any condition that is set is not met.
That is, if one or all of the placed conditions return the value False , the placed orders will be executed.
first example
a = 10
# Not equal to 10 The print command a will be executed The following condition means that if the value of the variable is
if not a == 10:
print('The condition return False')
• We will get the following result when running.
• The print order has not been executed because the answer to the condition is True.
second example
a = 10
# not equal to 20 The print command a will be executed The following condition means that if the value of the variable is
if not a == 20:
print('The condition return False')
• We will get the following result when running.
• Execute the print command because the answer to the condition is False.
third example
a = 30
# Between 0 and 20 the print command a will be executed The following condition means that if a value is not
if not 0 >= a <= 20:
print('the condition return False')
• We will get the following result when running.
• Execute the print command because the answer to the condition is False.
note
If you want to apply the not to more than one variable, you must put the conditions in parentheses. That is how you should use it not( ).
Fourth example
user = 'mhamad'
code = 0000
# The following condition means that if no specified condition is met, the print command will be executed
if not(user == 'harmash' or code == 1234):
print('Both conditions return False')
• We will get the following result when running.
• Execute the print command because the answer to both conditions is False.