Python any Function with Examples - Python Tutorial | Logic Practice
Python Course / Other / Python any Function with Examples

Python any Function with Examples

BLUF: This lesson on Python any Function with Examples provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python any Function with Examples

Mastering Python any Function with Examples is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

The Python function any evaluates an iterable and returns True if at least one element within it is true; if no elements are true, it will return False.

Note : If the iterable is empty, then it returns False.

Python any Function Syntax

It has the following syntax:

Example

any(iterable)

Parameters

Iterable: It accepts an iterable entity like a list, dictionary, and so forth.

Return

It yields true if at least one item within an iterable evaluates to true.

Different Examples for Python any Function

In this section, we will explore multiple examples of the Python any function.

Python any Function Example 1

Let's see how any works for lists?

Example

l = [4, 3, 2, 0]

print(any(l))

l = [0, False]

print(any(l))

l = [0, False, 5]

print(any(l))

l = []

print(any(l))

Output:

Output

True

False

True

False

Clarification: In the preceding illustration, we utilize a list (l) that includes several elements, followed by an examination of the code's output. In the initial scenario, the list consists entirely of truthy values, resulting in a return value of TRUE.

In the second scenario, both elements hold a false value. Therefore, it yields FALSE.

In the third scenario, there are two elements that are false and one element that is true. Consequently, it yields TRUE.

In the previous scenario, the list is devoid of any elements. Therefore, it yields a result of FALSE.

Python any Function Example 2

The following illustration demonstrates the functionality of any when applied to strings.

Example

s = "This is awesome"

print(any(s))

# 0 is False

# '0' is True

s = '000'

print(any(s))

s = ''

print(any(s))

Output:

Output

True

True

False

Clarification: In the example provided, a string evaluates to a True value.

In the second scenario, '000' functions as a string, which results in a return value of True.

In the third scenario, when a string is devoid of any characters, it yields a value of False.

Python any Function Example 3

The following example demonstrates the functioning of any when applied to dictionaries.

Example

d = {0: 'False'}

print(any(d))

d = {0: 'False', 1: 'True'}

print(any(d))

d = {0: 'False', False: 0}

print(any(d))

d = {}

print(any(d))

# 0 is False

# '0' is True

d = {'0': 'False'}

print(any(d))

Output:

Output

False

True

False

False

True

Clarification: In the example provided above, we utilize several dictionaries that hold various items. In the initial scenario, the value 0 yields a False outcome.

In the second scenario, one of the values evaluates to false while the other evaluates to true. Consequently, it yields the true value.

In the third scenario, since both values are considered false, the outcome is also a false value.

In the fourth scenario, the dictionary is devoid of any entries. Consequently, it yields a False result.

In the fifth scenario, the character '0' functions as a string. Consequently, it yields a True value.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience