Skip to content
### Python Logical Operators

**Logical Operators in Python**

**“and” python operator:**

### “or” python operator:

**“not” python operator:**

**Python Logical Operators with conditional statements**

**Conditional statements:**

#### Loops:

#### Boolean algebra:

### Quiz: Logical Operators in Python

#### 1. What does the “and” operator return in Python?

#### 2. What is the output of the following code?

#### 3. Which logical operator returns True if at least one operand is True?

#### 4. What is the purpose of the “not” operator in Python?

#### 5. In the code below, what will be the output?

#### 6. How are logical operators used in conditional statements?

#### 7. What is the output of the following code?

#### 8. What is boolean algebra?

#### 9. What is the result of the following code?

#### 10. How are logical operators used in loops?

#### 11. What is the output of the following code?

#### 12. How can you express “either x is less than 5 or y is greater than 10” using Python logical operators?

#### 13. What does the following code snippet do?

#### 14. In boolean algebra, what is the result of the expression (True and False) or (not True)?

#### 15. How would you rewrite the following code using the “not” operator?

#### 16. What is the purpose of using logical operators in loops?

#### 17. Which logical operator has the highest precedence in Python?

#### 18. What does the following code snippet output?

#### 19. In the code below, how many times will the loop execute?

#### 20. What is the primary use of the “or” operator in Python?

- الرئيسية
- أحدث المقالات
- جافا سكريبت

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”

- In this example, the “while” loop uses the “or” operator to evaluate two conditions: whether x is less than y, and whether the difference between y and x is greater than 3.
- The loop continues as long as at least one of these conditions is True.

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”

- In this example, the “not” operator is used to invert the truth value of x, and the “and” operator is used to combine it with the truth value of y.
- The result is False, because both operands are 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

Main Content