Python: bool() function
bool() function
The bool() function is used to convert a value to a Boolean.
Syntax:
class bool([x])
Version:
(Python 3.2.5)
If x is false or omitted, this returns False; otherwise it returns True. bool is also a class, which is a subclass of int. Class bool cannot be subclassed further. Its only instances are False and True.
Return value:
Boolean Values (True/False).
Example: Python bool() function
val = True
print("val = ", bool(val))
val = False
print("val = ", bool(val))
val = 5
print("val = ", bool(val))
val = 0
print("val = ", bool(val))
val = bool()
print("val = ", bool(val))
Output:
val = True val = False val = True val = False val = False
Check if a list is empty or not ?
l = []
if not l:
print("List is empty.")
else:
print("List is not empty.")
l = [1, 2, 3]
if not l:
print("List is empty.")
else:
print("List is not empty.")
Output:
List is empty. List is not empty.
Python Code Editor:
Previous: bin()
Next: bytearray()
Test your Python skills with w3resource's quiz
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python/built-in-function/bool.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics