Boolean logical operators:
Boolean logical operators are used in decision making and control the flow of a program. There are three boolean functions in python i.e AND, OR, NOT. The resultant of these evaluates to true on the following condition.
AND evaluates to true when both of the statement evaluates to true
Keeps this table in your mind for ‘and’ operator
Condition1 Condition2 Result
True True True
True False False
False True False
False False Flase
Let’s code an example for ‘and’ to better understand.
Code:
Program:
# Program to check whether the login information is correct or not
username = "Covid"
password = "password"
userInfo = input("Enter Your username: ")
pw = input("Enter your password: ")
if username == userInfo and password == pw:
print("Access Granted")
else:
print("Email or password is incorrect please try again ")
So in this program if any of these statements evaluates to false the program will print the else's statement. The statements inside ‘if’ will run only when both of these conditions are true.
OR evaluates to true when one of the statements is true.
Keep this table in your mind for 'or' operator.
Condition1 Condition2 Result
True True True
True False False
False True False
False False Flase
Let’s code an example for ‘and’ to better understand.
Code:
# Program to demonstrate ‘or’
a = 10
b = -10
c = 0
if a > 0 or b > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
if b > 0 or c > 0:
print("Either of the number is greater than 0")
else:
print("No number is greater than 0")
NOT operator alters the resultant if it is true it makes it false and vice versa. If the condition is true the result is false and if the condition is false the result is true
Let's write a code for not operator..
Program:
Code:
# Program to verify your age for adult sites
age = int(input("what is your age? "))
if not age >= 18:
print("You are not able to enter this site")
else:
print("Welcome to ****")
Hope You understand each and every line take good care of your health.
("Allah Hafiz" and "Good Bye" Till the next post )
"Keep Learning and Keep exploring"
Love me like you do