The `all()`

function in Python returns `True`

if all elements in an iterable are true, and `False`

otherwise. It takes an iterable as its argument and returns a boolean value. The `any()`

function in Python returns `True`

if at least one element in an iterable is true, and `False`

otherwise. It takes an iterable as its argument and returns a boolean value also. This article will tell you how to use these 2 functions with examples.

## 1. Python all() Function.

### 1.1 Python all() Function Syntax.

- Here is the syntax for the
`all()`

function.all(iterable)

- The
`iterable`

argument is any iterable object such as a list, tuple, set, or dictionary.

### 1.2 Python all() Function Examples.

- Here are some examples of using the
`all()`

function. - Example 1: Check if all elements in a list are even.
>>> my_list = [2, 4, 6, 8] >>> >>> result = all(x %2 == 0 for x in my_list) >>> >>> print(result) True >>> >>> my_list = [1, 2, 4, 6, 8] >>> >>> result = all(x %2 == 0 for x in my_list) >>> >>> print(result) False

- Example 2: Check if all elements in a tuple are strings.
>>> my_tuple = ("apple", "banana", "cherry") >>> >>> result = all(isinstance(x, str) for x in my_tuple) >>> >>> print(result) True >>> >>> my_tuple = (1, 2, "apple", "banana", "cherry") >>> >>> result = all(isinstance(x, str) for x in my_tuple) >>> >>> print(result) False

- Example 3: Check if all elements in a dictionary are positive.
>>> my_dict = {"a": 5, "b": 10, "c": -3} >>> >>> result = all(x > 0 for x in my_dict.values()) >>> >>> print(result) False

- Example 4: Check if all elements in a list are non-empty strings.
>>> my_list = ["hello", "world", "", "python"] >>> >>> result = all(my_list) >>> >>> print(result) # Empty strings evaluate to False False >>> result = all(x for x in my_list if x) # Using a filter to remove empty strings >>> >>> print(result) True

- Example 5: Check if all elements in a list of lists have the same length.
>>> my_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] >>> >>> result = all(len(x) == len(my_list[0]) for x in my_list) >>> >>> print(result) False >>> >>> my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> >>> result = all(len(x) == len(my_list[0]) for x in my_list) >>> >>> print(result) True

- Example 6: Check if all elements in a list of tuples satisfy a condition.
>>> my_list = [("apple", 5), ("banana", 10), ("cherry", 3)] >>> >>> result = all(x[1] > 0 for x in my_list) # each x is a tuple in the list, x[1] is the number in the tuple. >>> >>> print(result) True >>> >>> result = all(x[0].startswith("a") for x in my_list) # x[0] is the string in the tuple. >>> >>> print(result) False

## 2. Python any() Function.

### 2.1 Python any() Function Syntax.

- The
`any()`

function in Python returns`True`

if at least one element in an iterable is true, and`False`

otherwise. It takes an iterable as its argument and returns a boolean value. - Here is the syntax for the
`any()`

function.any(iterable)

- The
`iterable`

argument is any iterable object such as a list, tuple, set, or dictionary.

### 2.2 Python any() Function Examples.

- Here are some examples of using the
`any()`

function. - Example 1: Check if any element in a list is even.
>>> my_list = [1, 3, 5, 7, 8, 9] >>> >>> result = any(x % 2 == 0 for x in my_list) >>> >>> print(result) True

- Example 2: Check if any element in a tuple is a string.
>>> my_tuple = (2, True, [1, 2], "hello") >>> >>> result = any(isinstance(x, str) for x in my_tuple) >>> >>> print(result) True

- Example 3: Check if any element in a dictionary is negative.
>>> my_dict = {"a": 5, "b": 10, "c": -3} >>> >>> result = any(x < 0 for x in my_dict.values()) >>> >>> print(result) True

- Example 4: Check if any element in a set is a prime number.
# prime number is the nature number that can only be whole divided by 1 and itself. >>> def is_prime(num): ... if num < 2: ... return False ... for i in range(2, int(num ** 0.5) + 1): ... if num % 2 == 0: ... return False ... return True ... >>> my_set = {4, 7, 10, 13} >>> >>> result = any(is_prime(x) for x in my_set) >>> >>> print(result) True >>> result = all(is_prime(x) for x in my_set) >>> >>> print(result) False

- Example 5: Check if any element in a list of lists is empty.
>>> my_list = [[1, 2, 3], [4, 5], [], [6, 7, 8, 9]] >>> >>> result = any(not x for x in my_list) >>> >>> print(result) True