Python Logical Operators are used to evaluate the truth value of a set of Boolean expressions. These operators include “and”, “or”, and “not”. The “and” operator returns True only if both operands are True, the “or” operator returns True if at least one operand is True, and the “not” operator returns the opposite of the operand’s value. Logical operators are often used in conditional statements and loops to make decisions based on the truth value of multiple expressions. Understanding logical operators is essential for writing effective and efficient Python code.
Here are some examples of Python Logical Operators:
x = 5 y = 10 z = 15 if x < y and z > y: print("Both conditions are True")
Output: “Both conditions are True”
In this example, the “and” operator evaluates both conditions and returns True only if both are True.
x = 5 y = 10 z = 15 if x > y or z > y: print("At least one condition is True")
Output: “At least one condition is True”
In this example, the “or” operator evaluates both conditions and returns True if at least one is True.
x = True if not x: print("x is False") else: print("x is True")
Output: “x is True”
In this example, the “not” operator returns the opposite of the value of the operand, which is True. Therefore, the output is “x is True”.
Here are some additional uses of Python Logical Operators:
Logical operators are often used in conditional statements to make decisions based on the truth value of multiple expressions.
For example:
x = 5 y = 10 if x > y: print("x is greater than y") elif x < y: print("y is greater than x") else: print("x and y are equal")
Output: “y is greater than x”
In this example, the “if” statement uses the “greater than” operator to compare the values of x and y, and the “elif” statement uses the “less than” operator to make a different decision based on the opposite truth value of the first expression.
Logical operators are also useful in loops to control the flow of the program based on multiple conditions.
For example:
x = 5 y = 10 while x < y or y - x > 3: print(x) x += 1
Output: “5 6 7 8 9”
Logical operators can be used to perform boolean algebra, which is a branch of mathematics that deals with logical statements and their truth values.
For example:
x = True
y = False
z = not x and y
print(z)
Output: “False”
a. True if at least one operand is True
b. True only if both operands are True
c. The opposite of the operand’s value
x = 5
y = 10
z = 15
if x < y and z > y:
print(“Both conditions are True”)
a. Both conditions are True
b. No output
c. Error
a. and
b. or
c. not
a. Returns True only if both operands are True
b. Returns the opposite of the operand’s value
c. Returns True if at least one operand is True
x = 5
y = 10
if not x:
print(“x is False”)
else:
print(“x is True”)
a. x is False
b. x is True
c. Error
a. To perform mathematical calculations
b. To control the flow of the program based on truth values
c. To print messages to the console
x = 5
y = 10
if x > y or y – x > 3:
print(x)
a. 5
b. No output
c. Error
a. A branch of mathematics dealing with logical statements and their truth values
b. A programming language in Python
c. A method for printing boolean values
x = True
y = False
z = not x and y
print(z)
a. True
b. False
c. Error
a. To define variables
b. To control the loop based on truth values of conditions
c. To perform string operations
Answers:
1-b
2-a
3-b
4-b
5-b
6-b
7-a
8-a
9-b
10-b
python x = True y = False if x and not y: print(“Condition is True”) else: print(“Condition is False”) a. Condition is True
b. Condition is False
c. Error
a. x < 5 and y > 10
b. x > 5 or y < 10
c. not x and not y
python x = 5 y = 10 z = x > 3 or y < 20 print(z) a. Prints True
b. Prints False
c. Prints x > 3 or y < 20
a. True
b. False
c. Error
python x = 10 y = 5 if not (x > y): print(“x is not greater than y”) a. if x <= y:
b. if x < y:
c. if x >= y:
a. To define loop counters
b. To control the loop’s execution based on multiple conditions
c. To calculate loop increments
a. and
b. or
c. not
python x = True y = False z = x and (not y) print(z) a. True
b. False
c. x and not y
python x = 5 y = 10 while x < y and y – x > 2: print(x) x += 1 a. 3
b. 4
c. 5
a. To combine two conditions and return True if both are True
b. To evaluate two conditions and return True if at least one is True
c. To invert the truth value of an operand
Answers:
11. a
12-a
13-a
14-b
15-a
16-b
17-c
18-a
19-b
20-b