Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
my_list = [1, 2, 3, 4, 5]
return element > 0
result = True
for element in my_list:
if not check_condition(element):
result = False
In this example, we have a list of numbers called “my_list”. We also have a function called “check_condition” that takes an element as an argument and returns True if the element meets a certain condition (in this case, if it’s greater than 0).
We then create a variable called “result” and set it to True. We use a for loop to iterate over each element in the list. Inside the loop, we use an if statement to check if the element meets the condition. If it doesn’t, we set the “result” variable to False and break out of the loop.
Finally, we print the value of the “result” variable, which will be True if every element in the list meets the condition, and False otherwise.