Write a Python Program to Test if List contains elements in Range


This program is checking if the elements in the list 'a' are within a certain range. The range is defined by the variables 'i' and 'j'. The variables 'i' and 'j' are assigned the values 3 and 10 respectively. The program then uses a for loop to iterate over each element in the list 'a'. Inside the for loop, the program uses an if statement to check if the current element, 'e', is less than 'i' or greater than or equal to 'j'. If either of these conditions are true, the variable 'res' is set to False and the loop is broken. After the loop completes, the program prints "Does list contain all elements in range : " followed by the value of 'res', which is either True or False. If the variable 'res' is True, that means all elements in the list 'a' are within the range defined by 'i' and 'j'. If 'res' is False, that means at least one element in the list 'a' is outside of that range.

Source Code

a = [4, 5, 6, 7, 3, 9]
 
print("Original list is : " + str(a))
 
i, j = 3, 10
 
res = True
for e in a:
	if e < i or e >= j :
		res = False
		break
 
print ("Does list contain all elements in range : " + str(res))

Output

Original list is : [4, 5, 6, 7, 3, 9]
Does list contain all elements in range : True


Example Programs