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
`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
`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
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
Logical operators can be used to combine multiple comparisons. Example:
age = 20 if age > 18 and age < 30: print('Adult') Output: Adult
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
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
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
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
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
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
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
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
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]
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
`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
`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