Skip to main content

Boolean

Boolean is a value that represents a true or false value.

We usually use Boolean values to represent conditions in our programs.

userNumber = int(input("Random Number: "))
print(userNumber > 5)

We can also save boolean values into variables.

a = 5 > 6
In Python alone, boolean values needs to be capitalized.

Other programming languages use values such as true and false.

Python uses True and False.

Python will read true and false like a variable name.

not operator

not operator flips the value of boolean into the opposite value.

not True

assert() method

assert method is commonly used to check a value of a boolean.

If that value is false, the program will stop and print an error message.

Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
assert False
AssertionError

If it is true, then the program will continue.

all() and any()

If a list is given (or a tuple), then all() will return true if all the values are true.

all([True, True, True])

any() will return true if any of the values is true.

any([False, False, False])

Python Quiz 7

1 Questions
boolean