Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson

Python Booleans

In Python, Boolean values represent truth values, indicating whether an expression is true or false. Understanding how to work with Booleans is crucial for implementing logical conditions and controlling the flow of your Python programs.

The Boolean data type in Python has two possible values:

– `True`: Represents a true condition.
– `False`: Represents a false condition.

“`python
x = True
y = False

print(x) # Output: True
print(y) # Output: False
“`

Boolean Operations

Python provides several operators for working with Boolean values:

– `and`: Returns `True` if both operands are `True`.
– `or`: Returns `True` if at least one of the operands is `True`.
– `not`: Returns the opposite Boolean value of the operand.

“`python
x = True
y = False

print(x and y) # Output: False
print(x or y) # Output: True
print(not x) # Output: False
“`

Note:

Python is case-sensitive. It’s important to use True and False with the correct capitalization. Using lowercase true or false will result in errors.

Comparison Operators

Comparison operators are used to compare two values and return a Boolean result. Here are the common comparison operators in Python:

– `==`: Checks if two values are equal.
– `!=`: Checks if two values are not equal.
– `>`: Checks if the left operand is greater than the right operand.
– `<`: Checks if the left operand is less than the right operand.
– `>=`: Checks if the left operand is greater than or equal to the right operand.
– `<=`: Checks if the left operand is less than or equal to the right operand.

“`python
x = “hello”
y = “world”

print(x == y) # Output: False
print(x != y) # Output: True
print(x < y) # Output: True
“`

Boolean Context with Strings

In Python, strings can be evaluated as `True` or `False` in a Boolean context:

– Non-empty strings are considered `True`.
– Empty strings are considered `False`.

“`python
x = “hello”
y = “”

print(bool(x)) # Output: True
print(bool(y)) # Output: False
“`

Understanding Python Booleans and how they interact with logical expressions and conditional statements is fundamental to writing effective Python programs. By leveraging Boolean operations and comparison operators, you can create robust and efficient code that handles various scenarios with ease.

 

Examples demonstrating logical operations using the AND and OR operators in Python:

1. Logical AND (`and`):

The `and` operator returns `True` if both operands are `True`, otherwise it returns `False`.

“`python
# Example 1
x = True
y = False
z = True

result = x and y
print(result) # Output: False

# Example 2
result = x and z
print(result) # Output: True
“`

2. Logical OR (`or`):

The `or` operator returns `True` if at least one of the operands is `True`, otherwise it returns `False`.

“`python
# Example 1
x = True
y = False
z = True

result = x or y
print(result) # Output: True

# Example 2
result = y or z
print(result) # Output: True
“`

3. Combining AND and OR:

You can also combine logical AND and OR operations in Python.

“`python
# Example 1
a = True
b = False
c = True

result = a and b or c
print(result) # Output: True
“`

The above expression evaluates `a and b` first, which is `False`, then it evaluates `False or c`, which is `True`, hence the final result is `True`.

These examples demonstrate how the logical AND and OR operators work in Python to evaluate expressions and produce boolean results based on the conditions provided.

For all possibilities of logical operations using the AND and OR operators in Python:

1. Logical AND (`and`):

The `and` operator returns `True` if both operands are `True`, otherwise it returns `False`.

“`python
# True and True
result = True and True
print(result) # Output: True

# True and False
result = True and False
print(result) # Output: False

# False and True
result = False and True
print(result) # Output: False

# False and False
result = False and False
print(result) # Output: False
“`

2. Logical OR (`or`):

The `or` operator returns `True` if at least one of the operands is `True`, otherwise it returns `False`.

“`python
# True or True
result = True or True
print(result) # Output: True

# True or False
result = True or False
print(result) # Output: True

# False or True
result = False or True
print(result) # Output: True

# False or False
result = False or False
print(result) # Output: False
“`

These examples cover all possible combinations of logical AND and OR operations in Python, helping to understand how they evaluate expressions and produce boolean results based on the conditions provided.

EXERCISES

Age Checker: Write a program that determines whether a person is eligible to vote based on their age. Prompt the user to input their age, and check if they are 18 years old or older. Display whether they are eligible to vote or not.

Temperature Checker: Develop a program that checks if the temperature is within a comfortable range. Prompt the user to input the current temperature, and check if it is between 20°C and 25°C (inclusive). Display whether the temperature is comfortable or not.

Username and Password Validation: Create a program that validates a username and password combination. Define a correct username and password in the program, and prompt the user to input their username and password. Check if the entered username and password match the correct ones. Display whether the login attempt is successful or not.

Membership Checker: Write a program that checks if a user is a premium member of a website. Prompt the user to input their membership status (either “premium” or “standard”). Check if the user is a premium member. Display whether the user has access to premium features or not.

Divisibility Checker: Develop a program that checks if a given number is divisible by both 2 and 3. Prompt the user to input a number, and check if it is divisible by both 2 and 3 without any remainder. Display whether the number is divisible by both 2 and 3.

Leap Year Checker: Create a program that determines if a given year is a leap year. Prompt the user to input a year, and check if it is divisible by 4 and not divisible by 100, or if it is divisible by 400. Display whether the year is a leap year or not.

Boolean Logic Gates: Implement a program that simulates boolean logic gates. Prompt the user to input two boolean values (either True or False) and choose a logic gate operation (AND, OR, NOT, XOR). Perform the selected logic gate operation on the input boolean values and display the result.

Email Validation: Write a program that validates an email address. Prompt the user to input an email address, and check if it follows the correct format (e.g., contains “@” and “.” symbols). Display whether the email address is valid or not.

Weather Adviser: Develop a program that advises users on whether to carry an umbrella based on the weather forecast. Prompt the user to input the weather forecast (e.g., “sunny”, “rainy”, “cloudy”). Check if the forecast indicates rain, and advise the user to carry an umbrella accordingly.

Health Status Checker: Create a program that checks a person’s health status based on their temperature and symptoms. Prompt the user to input their temperature and symptoms (e.g., fever, cough). Check if the temperature is above 37.5°C and if the person has any symptoms. Display whether they need to consult a doctor or not.

Join the conversation