
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Python – Test for Word construction from character list
- Python – Test if elements of list are in Min/Max range from other list
- Python Program for Number of elements with odd factors in the given range
- Python – Test for desired String Lengths
- Python – Check if elements index are equal for list elements
- Python – Rows with all List elements
- Python – Reorder for consecutive elements
- Python – Find the distance between first and last even elements in a List
- Python – Strings with all given List characters
- Python – Check if elements in a specific index are equal for list elements
- XOR of all the elements in the given range [L, R] in C++
- Python Program – Strings with all given List characters
- Program to find kpr sum for all queries for a given list of numbers in Python
- Python – Random range in a List
- Python – Test if list is Palindrome
Advertisements