Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 7: Boolean Operators

0 Replies, 688 Views

[Image: images?q=tbn:ANd9GcTmzA9Nu4_GpHpUbXTwYC3...c&usqp=CAU]


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 Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,227 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,607 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,646 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,826 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,591 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,825 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,775 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,805 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,807 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,801 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)