Q1). What is a logical operator in Python?

Logical operators are used to combine conditional statements. In Python, the logical operators are `and`, `or`, and `not`. Example:

x = True
y = False
print(x and y)  Output: False
print(x or y)   Output: True
print(not x)    Output: False

Q2). What is the difference between `and` and `or` in Python?

`and` returns `True` if both operands are true, while `or` returns `True` if at least one operand is true. Example:

a = True
b = False
print(a and b)   Output: False
print(a or b)    Output: True

Q3). How do you use `not` in a conditional statement?

`not` is used to reverse the boolean value of a condition. Example:

is_raining = True
if not is_raining:
    print('Go outside')
else:
    print('Stay inside')
Output:
Stay inside

Q4). What is short-circuit evaluation in Python?

Short-circuit evaluation means Python evaluates the second part of an expression only if needed. Example:

x = False
y = True
if x and y:
    print('Both True')
else:
    print('Not Both True')
Output:
Not Both True

Q5). How do you use logical operators with comparisons?

Logical operators can be used to combine multiple comparisons. Example:

age = 20
if age > 18 and age < 30:
    print('Adult')
Output:
Adult

Q6). How do you check if a value is in a list?

Use the `in` keyword to check membership. Example:

fruits = ['apple', 'banana', 'cherry']
if 'banana' in fruits:
    print('Banana is in the list')
Output:
Banana is in the list

Q7). How do you use logical operators with string comparisons?

Logical operators can combine string comparisons. Example:

username = 'admin'
password = 'password123'
if username == 'admin' and password == 'password123':
    print('Logged in successfully')
Output:
Logged in successfully

Q8). What is a truthy value?

In Python, values like non-zero numbers and non-empty strings are considered truthy. Example:

if 5:
    print('This is truthy')
Output:
This is truthy

Q9). What is a falsy value?

Falsy values are `False`, `0`, `None`, and empty sequences or collections. Example:

if 0:
    print('This is truthy')
else:
    print('This is falsy')
Output:
This is falsy

Q10). How do you use logical operators to handle multiple conditions?

Logical operators can handle multiple conditions in if statements. Example:

age = 25
has_ID = True
if age > 18 and has_ID:
    print('Allowed entry')
Output:
Allowed entry

Q11). How do you combine logical operators with lists?

You can combine logical operators with list conditions. Example:

fruits = ['apple', 'banana', 'cherry']
if 'banana' in fruits or 'grapes' in fruits:
    print('At least one fruit found')
Output:
At least one fruit found

Q12). How can you use `if` statements to check for multiple conditions?

Use `if-elif-else` to check multiple conditions. Example:

temperature = 30
if temperature > 25:
    print('Hot')
elif temperature > 15:
    print('Warm')
else:
    print('Cool')
Output:
Hot

Q13). What is the purpose of the `not` operator?

The `not` operator negates a boolean value. Example:

is_raining = True
if not is_raining:
    print('No rain today')
else:
    print('Carry an umbrella')
Output:
Carry an umbrella

Q14). How do you use logical operators in list comprehensions?

Logical operators can filter elements in list comprehensions. Example:

numbers = [1, 2, 3, 4, 5]
even_numbers = [ num for num in numbers if num % 2 == 0 and num > 2 ]
print(even_numbers)
Output:
[4]

Q15). How do you use logical operators with dictionaries?

You can use logical operators to check conditions with dictionary values. Example:

person = {'name': 'John', 'age': 25}
if person['name'] == 'John' and person['age'] > 20:
    print('John is older than 20')
Output:
John is older than 20

Q16). What is the difference between `and` and `or` operators?

`and` requires all conditions to be true, while `or` requires at least one to be true. Example:

x = 5
y = 10
if x > 3 and y < 15:
    print('Both conditions are true')
if x > 3 or y > 15:
    print('At least one condition is true')
Output:
Both conditions are true
At least one condition is true

Q17). How can you use `not` with multiple conditions?

`not` can be used with multiple conditions by combining them with `and` or `or`. Example:

x = 5
y = 10
if not (x > 3 and y < 15):
    print('Conditions are not met')
Output:
Conditions are not met