# Define a Boolean variable is_available = True # Use the not operator to invert the value print(not is_available) # Output: False
False
# Define a Boolean variable is_raining = False # Use the not operator in an if statement if not is_raining: print("Let's go for a walk!") else: print("Better stay indoors.")
Let's go for a walk!
# Define variables temperature = 25 # Use the not operator with a comparison if not temperature > 30: print("The temperature is not above 30 degrees.") else: print("The temperature is above 30 degrees.")
The temperature is not above 30 degrees.
# Define variables age = 20 has_permission = False # Use the not operator with and and or operators if not (age < 18 or has_permission): print("Access granted.") else: print("Access denied.")
Access granted.
# Without using not response = "no" if response == "no" or response == "n": print("Negative response received.") else: print("Positive response received.") # Using not response = "no" if not (response != "no" and response != "n"): print("Negative response received.") else: print("Positive response received.")
Negative response received. Negative response received.
# Define a list of numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Use the not operator in a list comprehension to filter out even numbers odd_numbers = [num for num in numbers if not num % 2 == 0] print(odd_numbers) # Output: [1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
# Define a function that checks for an empty list def is_empty(lst): return len(lst) == 0 # Example usage my_list = [1, 2, 3] if not is_empty(my_list): print("The list is not empty.") else: print("The list is empty.")
The list is not empty.
# Define a list of fruits fruits = ["apple", "banana", "cherry"] # Use the not operator to check for non-membership fruit_to_check = "mango" if fruit_to_check not in fruits: print(f"{fruit_to_check} is not in the list.") else: print(f"{fruit_to_check} is in the list.")
mango is not in the list.