- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python all() Function
The all() function of Python is used where we need to check whether all the element of a list, tuple, dictionary, set etc. is True or not. That is, if any element is False, then the function all() returns False. Otherwise returns True. For example:
mylist = [True, True, True, True] print(all(mylist))
produces True on output as shown in the snapshot given below:
and the following code, also uses all() function in Python:
mylist = [True, True, False, True] print(all(mylist))
produces False, because one of its element is False. Here is the snapshot of the output:
Note: All elements are referred as True by all() function, except the False keyword itself, and the value 0 as key of dictionary. Even empty list, tuple, dictionary, set etc. also returned as True by all() function.
all() Function with List
Here is an example uses, all() function of Python with list:
a = [1, 2, 3, 4, 5] print(all(a)) b = ['p', 'y', 't', 'h', 'o', 'n'] print(all(b)) c = ["codes", "cracker"] print(all(c)) d = [1, 3, True, 'p', "codes"] print(all(d)) e = [1, 3, False, 'p', "codes"] print(all(e)) f = [] print(all(f))
The output produced by above program are all True except the last statement's output as shown in the snapshot given below:
Note: Empty list, tuple, dictionary, set etc. is also considered as True, by all() function.
all() Function with Tuple
To create and understand the use of all() function with tuple, using the same program as created above. Then only replace [ with ( and ] with ). Here is the complete program:
a = (1, 2, 3, 4, 5) print(all(a)) b = ('p', 'y', 't', 'h', 'o', 'n') print(all(b)) c = ("codes", "cracker") print(all(c)) d = (1, 3, True, 'p', "codes") print(all(d)) e = (1, 3, False, 'p', "codes") print(all(e)) f = () print(all(f))
This program produces exactly same output as of previous program, that is, the program created in the section "all() function with list".
all() Function with Set
As done in previous program, to create a program that uses and demonstrates all() function with Set, using the same program as given above. We need to replace ( with { and ) with } as shown in the program given below:
a = {1, 2, 3, 4, 5} print(all(a)) b = {'p', 'y', 't', 'h', 'o', 'n'} print(all(b)) c = {"codes", "cracker"} print(all(c)) d = {1, 3, True, 'p', "codes"} print(all(d)) e = {1, 3, False, 'p', "codes"} print(all(e)) f = {} print(all(f))
This program too produces same output that will be
True True True True False True
all() Function with Dictionary
Because, in case of dictionary, elements are in the form of key and value pair. Therefore, the function all() returns True/False only based on key's value. That is, if the value of all keys, does not contains False, then all() returns True. Otherwise, returns False. Here is an example:
a = {1: "one", 2: "two", 3: "three"} print(all(a)) b = {'p': "python", "c": "codescracker"} print(all(b)) c = {0: "name", 1: "address", 3: "contact"} print(all(c)) d = {False: "Hi", True: "Hello"} print(all(d)) e = {} print(all(e))
Here is the output produced by above program:
Note: The key equals to 0 also referred as False by all().
« Previous Function Next Function »