# Define variables a = 10 b = 20 # Using the or operator in an if statement if a > 15 or b > 15: print("At least one condition is True") else: print("Both conditions are False")
At least one condition is True
# Define variables user_input = "" # Empty string default_value = "default" # Using the or operator to provide a default value value = user_input or default_value print(value) # Output: default
default
def first_condition(): print("Evaluating first condition...") return True def second_condition(): print("Evaluating second condition...") return False # Using the or operator with short-circuit evaluation if first_condition() or second_condition(): print("At least one condition is True")
Evaluating first condition... At least one condition is True
# Define a list of numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Use list comprehension with the or operator filtered_numbers = [num for num in numbers if num % 2 == 0 or num > 5] print(filtered_numbers) # Output: [2, 4, 6, 7, 8, 9, 10]
[2, 4, 6, 7, 8, 9, 10]
# Define variables x = 5 y = 10 z = 15 # Using the or and and operators together if (x < y and y < z) or (x > z): print("Complex condition is True") else: print("Complex condition is False")
Complex condition is True
# Define variables a = None b = "Python" # Using the or operator for a conditional assignment result = a or b print(result) # Output: Python
Python
def greet(name=None): # Provide a default value using the or operator name = name or "Guest" print(f"Hello, {name}") # Example usage greet("Alice") # Output: Hello, Alice greet() # Output: Hello, Guest
Hello, Alice Hello, Guest