Home > Backend Development > Python Tutorial > Detailed introduction to Python's built-in bool function

Detailed introduction to Python's built-in bool function

高洛峰
Release: 2017-03-21 11:30:34
Original
2072 people have browsed it

English document:

class bool([x])

Return a Boolean value, i.e. one of True or False. x is converted using the standard truth testing procedure. If x is false or omitted, this returns False; otherwise it returns True. The bool class is a subclass of int (see Numeric Types — int, float, complex). It cannot be subclassed further. Its only instances are False and True (see Boolean Values).

Instructions:

1. The return value is a Boolean value of True or False

2. If the parameter is default, False will be returned

>>> bool() #未传入参数
False
Copy after login

3. Parameter conversion uses standard logical test expressions

3.1 When the Boolean type is passed in, the original value will be used Return

>>> bool(True)
True
>>> bool(False)
False
Copy after login

3.2 When a string is passed in, the empty string returns False, otherwise True

>>> bool('')
False
>>> bool('0')
True
Copy after login

3.3 Pass in a value When the value is 0, False is returned, otherwise True is returned Otherwise return True

>>> bool(0)
False
>>> bool(1)
True
>>> bool(-1.0)
True
Copy after login

The above is the detailed content of Detailed introduction to Python's built-in bool function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template