×

any() Keyword in python

“any” keyword in python

This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is?

Python any () function is used to perform operations on iterable. Here, iterables can be Lists, dictionaries, Tuples, sets, etc...

It returns true if there are any true values in the iterables else, it will return false.

It might be a bit difficult to understand, but you can understand it clearly with some examples.

Syntax:

any(iterables)

Iterable parameter: This can be any iterable object like List, Tuple, Set, Dictionary etc.

Working of any () Function with LISTS

Example:

lst = [1, 4, 3, 4, 4, 5, 1]
# All values in the list are true values.
print(any(lst))
lst = [False, 0, 0, False]
# All values in the list are true.
print(any( lst ))
lst= [ 6, 4, 5, 1, 0, 6, 7, False]
# Some elements in the list are true, while other values are false.
print(any( lst ))
l = []
# List is empty.
print(any( l ))

Output

“any” keyword in python

From the above code, you can see the following:

  1. Lst= [1, 4,3,4,4,5,1] when you perform any () function on this list, you will get True because the list contains true iterable values. Hence you will get the output as TRUE.
  2. Lst = [False, 0, 0, False] when you perform any () function on this list, you will get False because the list contains only false values (False, 0). Hence, you will get the output as FALSE.
  3. When you perform any () function on an iterable with the combination of both true values and false values, you will output True.

Ex: [6, 4, 5, 1, 0, 6, 7, False]

  • If you perform any () function on an empty iterable object, then you will output false because the null value is treated as false.

Working of any () Function with TUPLES.

Example:

t = (1, 4, 3, 4, 4, 5, 1)
# All values in the tuple are true values.
print(any(t))
t = (False, 0, 0, False)
# All elements in the tuple are false values.
print(any( t ))
t= (6, 4, 5, 1, 0, 6, 7, False)
# Some elements in the tuple are true, while other values are false.
print(any( t ))
T = ()
# Tuple is empty.
print(any( l ))

Output

“any” keyword in python

Working of any () Function with SETS.

Example:

Set = {1, 4, 3, 4, 4, 5, 1}
# All elements in the set are true values.
print (any (Set))
Set = {False, 0, 0, False}
# All elements in the set are false values.
print (any (Set))
Set= {6, 4, 5, 1, 0, 6, 7, False}
# Some elements in the set are true, while other values are false.
print (any (Set))
Set = {}
# Set is empty.
print (any (T))

Working of any () function with Dictionaries.

While working with dictionaries function of any () is a bit different. In a dictionary, if the dictionary is empty or all keys of a dictionary are false, any () function returns false. Otherwise, it will return true.   

Example:

# All elements of dictionary are true
di = {1: "Java", 2: "code”}
print(any(di))
# All elements of dictionary are false
di = {0: "Java", False: "code"}
print(any(di))
# Some elements of dictionary are true while others are false
di = {0: "java",1: "code"}
print(any(di))
# Empty dictionary
di = {}
print(any(di))
“any” keyword in python

Working of any () function with Strings.

Example:

# Non-Empty String
s = "Javaprogram !"
print(any(s))
# Non-Empty String
s = "000"
print(any(s))
# Empty string
s = ""
print(any(s))

“any ()” function can also be used with looping and conditional statements.

You can refer to the below code to understand the usage of the "any ()" function with loops.

Working of any () function with Loops.

Example:

def any(lst):
    for i in list:
        if i:
            return True
    return False
 
x = [6,3,0,4,9,3,9,8]
print(any(x))

If you pass an empty list in the above code, then the output will be False because the null value is considered false and iterating null value outputs false.

This is the usage of any () function in python, where you can check true or false for iterables.


Related Topics

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Conditional Expressions in Python

In Python conditional expression are sometimes referred to operator called as ternary operator. Not only Python supports ternary operator but many other programming languages supports it. Ternary operator are the...

2 minutes read.

Ternary operators in python

Starting from Python version 2.5, ternary operators are also known as Conditional operators. Using these operators, we can evaluate any problem based on a condition. It is an alternative and...

4 minutes read.

How to develop a game in python

Fun always makes a task interesting and easy to execute. Learning in the same theoretical way at some point reaches the boring spot. In this article, using some Python knowledge,...

13 minutes read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

Exponentiation in Python

What is an Exponent in Python? Exponent is a fundamental mathematical concept that is used in many different areas, such as engineering, physics, and finance. In mathematics, an exponent is a...

4 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Insertion Sort using Python

Insertion sort is a type of sorting technique that is used for sorting an array with random elements. Using sorting methods, any unsorted array can be sorted into ascending or...

3 minutes read.

Tensorflow Angular in Python

TensorFlow is an open-source, Python-compatible toolkit for numerical computation that accelerates and simplifies the creation of neural networks and machine learning algorithms. They make the interesting notion that models can be...

6 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.