Python if any is false

Any & All in Python?

Python provides two built-ins functions for “AND” and “OR” operations are All and Any functions.

Python any() function

The any() function returns True if any item in an iterable are true, otherwise it returns False. However, if the iterable object is empty, the any () function will return False.

Syntax

The iterable object can be a list, tuple or dictionary.

Example 1

>>> mylst = [ False, True, False] >>> x = any(mylst) >>> x True
Output is True because the second item is True.

Example 2

Tuple – check if any item is True

>>> #Tuple - check if any item is True >>> mytuple = (0, 1, 0, False) >>> x = any(mytuple) >>> print(x) True

Example 3

Set – Check if any item is True

>>> myset = >>> x = any(myset) >>> print(x) True

Example 4

Dictionary – check if any item is true in dictionary

>>> mydict = < 0 : "Apple", 1: "Banana">>>> x = any(mydict) >>> print(x) True

Return Value from any()

any() returns:

  • True – if atleast one item of the iterable is True.
  • False – if all the items are False or if an iterable is empty.
Читайте также:  Html input javascript functions

Python all() function

The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable object is empty, the all() function all returns True.

Syntax

The iterable object can be list, tuple or dictionary.

Example1 List- Check if all items are True

>>> mylst = [True, True, False] >>> x = all(mylst) >>> print(x) False

Above result shows False, as one of the item in the list is False.

Example 2 Tuple – Check if all items are True in tuple

>>> mytuple = (0, True, False) >>> x = all(mytuple) >>> print(x) False

Example 3: Set – check if all items are True in Set.

>>> myset = >>> x = all(myset) >>> print(x) True

Example 4: Dictionary – check if all item are true in dictionary

>>> mydict = >>> x = all(mydict) >>> print(x) False

Return value from all()

The all() method returns

Источник

Python Any | [Explained] any() Function in Python

Python any

There are many built-in functions in Python. Which helps us in increasing our productivity and efficiency in coding. One of the built-in function in Python interpreter is python any().

Python any() takes iterable as an argument and returns True if any of the element in the iterable is true. If iterable is empty, then the Python any() Function returns False.

Different Iterables Which Can Be Used With any() Function

While using the Python any() the iterable can be of different types. Some of the iterable’s are that can be used with Python any() function are the following:

  • list
  • generator
  • string
  • dictionary
  • tuples
  • boolean
  • empty iterables
  • custom objects

Python any() Syntax:

Parameters

The iterable object can be a list, tuple, boolean, dictionary etc.

The return type of any()

Python any() returns true if any of the items is True. It returns False if empty or all are false. Any can be thought of as a sequence of OR operations on the provided iterables.
Its short circuit the execution i.e. stop the execution as soon as the result is known.

Truth Table of Python any() Function

Python any() Function Truth Table

Sample Frame / Blueprint of Python Function()

def any(iterable): for element in iterable: if element: return True return False

Now, Let’s see some examples of Python any() with different iterables.

any() Function With List

A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ].

Let’s try to use any() with a list :

# If all values are true list1 = [9, 5, 11, True] print(any(list1)) # If all values are false list2 = [0, False] print(any(list2)) # If one value is false, others are true list3 = [5, 16, 3, 15] print(any(list3)) # If one value is true, others are false list4 = [10, 0, False, 19] print(any(list4)) # empty iterable list5 = [] print(any(list5))
True False True True False

any() Function With Tuples

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses (), whereas lists use square brackets []. Creating a tuple is as simple as putting different comma-separated values.

Let’s try to use any() with a tuple :

# If all values are true tuple1 = (9, 5, 11, True) print(any(tuple1)) # If all values are false tuple2 = (0, False) print(any(tuple2)) # If one value is false, others are true tuple3 = (5, 16, 3, 15) print(any(tuple3)) # If one value is true, others are false tuple4 = (10, 0, False, 19) print(any(tuple4)) # empty iterable tuple5 = () print(any(tuple5))
True False True True False

any() Function With String

A string in Python is a sequence of characters. It is a derived data type. Strings are immutable. This means that once defined, they cannot be changed.

A string is also iterable and we can use Python any() on a string object as well.
Let’s try to use any() with strings:

#First String string1 = "Python Pool" print(any(string1)) #Second String string2 = 'Karan Singh Bhakuni' print(any(string2)) #Third String string3 = "55 56" print(any(string3)) #Fourth String string4 = '26 Python Pool the best place to learn python' print(any(string4)) #Fifth String string5 = () print(any(string5))
True True True True False

As you can see that only for the empty string, it results False. For non-empty strings, the result is always True. This is another way to check if a string is empty or not.

any() Function With Dictionary

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds the key: value pair. Key-value is provided in the dictionary to make it more optimized.

Python any() Function will check only the keys,i.e. if anyone of the keys is true, it will result True. Else, False.
Let’s try to use any() with strings:

# When all keys are true dict1 = print(any(dict1)) # When all keys are false dict2 = print(any(dict2)) # When one key is true, others are false dict3 = print(any(dict3)) # When one key is false, others are true dict4 = print(any(dict4)) # When dictionary is empty dict5 = <> print(any(dict5))
True False True True False

Note:

In case of dictionaries, if all keys (not values) are false, any() returns False . If at least one key is true, any() returns True .

Python any() Function With Boolean

In programming, you often need to know if an expression is True or False . You can evaluate any expression in Python, and get one of two answers, True or False . When you compare two values, the expression is evaluated and returns the Boolean answer.

Let’s try to use any() with booleans:

# If all values are true bool1 = [True, True] print(any(bool1)) # If all values are false bool2 = [False, False] print(any(bool2)) # If one value is false, others are true bool3 = [False, True, True, True] print(any(bool3)) # If one value is true, others are false bool4 = [True, False, False] print(any(bool4)) # empty iterable bool5 = [] print(any(bool5))
True False True True False

Python any() Function With Sets

A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Python’s set class represents the mathematical notion of a set. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. This is based on a data structure known as a hash table.

A set is a collection which is unordered and unindexed. In Python, sets are written with curly brackets.
Let’s try to use any() with sets:

# If all values are true set1 = print(any(set1)) # If all values are false set2 = print(any(set2)) # If one value is false, others are true set3 = print(any(set3)) # If one value is true, others are false set4 = print(any(set4)) # empty iterable set5 = <> print(any(set5))
True False True True False

Using any() Method to Check If List Contains Any Element of Other List

In this example, we are taking two lists. In both of the list, we are taking city names as the elements of the list.

# Program to check the list contains elements of another list using any() # List1 List1 = ['Newyork' , 'London', 'Sidney', 'Chicago', 'Madrid', 'Paris'] # List2 List2 = ['Seattle' , 'Chicago', 'Prague'] #Using any function to check check = any(item in List1 for item in List2) if check is True: print("The list <> contains some elements of the list <>".format(List1, List2)) else : print("No, List1 doesn't have any elements of the List2.")
The list ['Newyork', 'London', 'Sidney', 'Chicago', 'Madrid', 'Paris'] contains some elements of the list ['Seattle', 'Chicago', 'Prague']

Here in list 1 and list 2, we checked for the common elements. And as you can see there is a common element (Chicago) which is present in both of the list. So the Output says list1 contains some elements of lsit2.

Must Read:

Conclusion

You can use Python any() in any iterable to check quickly if all values are False or not. Any Returns true if any of the items is True. It returns False if empty or all are false. Any can be thought of as a sequence of OR operations on the provided iterables.

Try to run the programs on your side and let me know if you have any queries.

Happy Coding!

Источник

Python any() Function with examples

Python any() function accepts iterable (list, tuple, dictionary etc.) as an argument and return true if any of the element in iterable is true, else it returns false. If iterable is empty then any() method returns false.

Python any() function example with lists

The any() function returns true if any of the element in the passed list is true. Here we are taking various lists with different items to demonstrate the output of any() function in different situations.

# all values are true lis1 = [10, 20, 30, 40] print(any(lis1)) # all values are false lis2 = [0, False] print(any(lis2)) # one value is false, others are true lis3 = [0, 10, 5, 15] print(any(lis3)) # one value is true, others are false lis4 = [10, 0, False] print(any(lis4)) # empty iterable lis5 = [] print(any(lis5))

Python any() function example

Output:

Python any() function example with Dictionaries

The any() function only checks the keys (not values) in the passed dictionary. If any of the key is true in the dictionary then any() function returns true, else it returns false. The any() function returns false for empty dictionary as well.

# all keys are true dict1 = print(any(dict1)) # all keys are false dict2 = print(any(dict2)) # one key is false, other is true dict3 = print(any(dict3)) # One key is true, '0' is not false its True # because '0' is a string not a number # if it is 0 without quotes then its false dict4 = print(any(dict4)) # empty dictionary dict5 = <> print(any(dict5))

Python any function dictionary example

Output:

Python any() function example with Tuples

Python any() function returns true if any of the item in tuple is true else it returns false. The any function returns false if the passed tuple is empty.

# all elements are true t1 = (1, "BeginnersBook", 22) print(any(t1)) # all elements are false t2 = (0, False, 0) print(any(t2)) # one element is true others are false t3 = (1, 0, False) print(any(t3)) # one element is false others are true t4 = (False, 1, 2, 3) print(any(t4)) # empty tuple t5 = () print(any(t5))

Python any function tuple example

Output:

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Оцените статью