w3resource
w3resource logo

Python: bool() function

(追記) (追記ここまで)
(追記) (追記ここまで)

bool() function

The bool() function is used to convert a value to a Boolean.

Syntax:

class bool([x])

Python: Built-in function - bool function()

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:

[フレーム]

PREV : bin()
NEXT : bytearray()

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.

(追記) (追記ここまで)


(追記) (追記ここまで)
(追記) (追記ここまで)


AltStyle によって変換されたページ (->オリジナル) /