Home > Backend Development > Python Tutorial > What does bool mean in python

What does bool mean in python

silencement
Release: 2019-07-27 17:41:57
Original
35997 people have browsed it

What does bool mean in python

bool is the abbreviation of Boolean, which has only two values: true (True) and false (False).
The bool function has only one parameter, and returns true or false based on the value of this parameter. Fake.
1. When using the bool function on numbers, 0 returns False, and any other value returns True.

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

2. When using the bool function on a string, it returns False for a string with no value (that is, None or an empty string), otherwise it returns True.

>>> bool('')
False
>>> bool(None)
False
>>> bool('asd')
True
>>> bool('hello')
True
Copy after login

3. The bool function returns False for empty lists, dictionaries and ancestors, otherwise it returns True.

>>> a = []
>>> bool(a)
False
>>> a.append(1)
>>> bool(a)
True
Copy after login

4. Use the bool function to determine whether a value has been set.

>>> x = raw_input('Please enter a number :')
Please enter a number :
>>> bool(x.strip())
False
>>> x = raw_input('Please enter a number :')
Please enter a number :4
>>> bool(x.strip())
True
Copy after login

The above is the detailed content of What does bool mean in python. 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