
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Test for all Even elements in the List for the given Range
When it is required to test all even elements in the list for the given range, a simple iteration and the modulus operator is used.
Below is a demonstration of the same −
Example
my_list = [32, 12, 42, 61, 58, 60, 19, 16] print("The list is :") print(my_list) i, j = 2, 7 my_result = True for index in range(i, j + 1): if my_list[index] % 2 : my_result = False break print("The result is :") if(my_result == True): print("All The elements are in the given range") else: print("All The elements are not in the given range")
Output
The list is : [32, 12, 42, 61, 58, 60, 19, 16] The result is : All The elements are not in the given range
Explanation
A list is defined and displayed on the console.
The value for ‘i’ and ‘j’ are defined.
The value for a variable is set to Boolean ‘True’.
The list is iterated over, and the modulus operator is used on each element to check if it is even or odd.
If it is even, the Boolean value is set to ‘False’ and the control breaks out of the loop.
Based on the Boolean value, relevant message is displayed on the console.
- Related Articles
- Python Program for Number of elements with odd factors in the given range
- Python – Test if elements of list are in Min/Max range from other list
- XOR of all the elements in the given range [L, R] in C++
- Python – Test for Word construction from character list
- Accessing all elements at given Python list of indexes
- Program to find kpr sum for all queries for a given list of numbers in Python
- Python program to print all even numbers in a range
- Check if an array contains all elements of a given range in Python
- Assign range of elements to List in Python
- List consisting of all the alternate elements in Python
- Python Program to replace list elements within a range with a given number
- Queries for counts of array elements with values in given range in C++
- Python – Check if elements index are equal for list elements
- Python – Find the distance between first and last even elements in a List
- Find a range that covers all the elements of given N ranges in C++

Advertisements