Python: Test whether all numbers of a list is greater than a certain number
Python Basic: Exercise-83 with Solution
Write a Python program to test whether all numbers in a list are greater than a certain number.
Pictorial Presentation:
Sample Solution-1:
Python Code:
# Create a list 'num' containing integer values.
num = [2, 3, 4, 5]
# Print a blank line for better readability.
print()
# Check if all elements in the 'num' list are greater than 1 and print the result.
print(all(x > 1 for x in num))
# Check if all elements in the 'num' list are greater than 4 and print the result.
print(all(x > 4 for x in num))
# Print a blank line for better readability.
print()
Sample Output:
True False
Sample Solution-2:
Python Code:
# Define a function 'test' that takes a list 'nums' and an integer 'n' as arguments.
# The function checks if all elements in the 'nums' list are greater than 'n' and returns the result.
def test(nums, n):
return(all(x > n for x in nums) )
# Create a list 'nums' containing integer values.
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# Print the original list of numbers.
print("Original list numbers:")
print(nums)
# Set the value of 'n' to 12.
n = 12
# Print a message indicating that we're checking if all numbers in the list are greater than 'n'.
print("\nCheck whether all numbers of the said list are greater than", n)
# Call the 'test' function to check if all numbers are greater than 'n' and print the result.
print(test(nums, n))
# Set the value of 'n' to 5.
n = 5
# Print a message indicating that we're checking if all numbers in the list are greater than 'n'.
print("\nCheck whether all numbers of the said list are greater than", n)
# Call the 'test' function again with the updated 'n' value and print the result.
print(test(nums, n))
Sample Output:
Original list numbers: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Check whether all numbers of the said list greater than 12 False Check whether all numbers of the said list greater than 5 True
Flowchart:
Python Code Editor:
Previous: Write a Python program to calculate the sum of all items of a container (tuple, list, set, dictionary).
Next: Write a Python program to count the number of occurrence of a specific character in a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/python-basic-exercise-83.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics