Python Fundamentals: Booleans


Python Booleans are one of Python's built-in data types. They are used to represent truth values (True or False).

a. Creating a boolean: Booleans in Python can be created by assigning True or False to a variable.

Code

bool1 = True
bool2 = False
print(bool1)
print(bool2)

Output

True
False

b. Boolean operations: Python includes the following boolean operations - and, or, not.

Code

print(True and False)
print(True or False)
print(not True)

Output

False
True
False

c. Boolean in conditional statements: Booleans are often used in conditional statements to decide which action Python should execute.

Code

x = 10
if x > 5:
    print('x is greater than 5')
else:
    print('x is not greater than 5')

Output

x is greater than 5

Enquiries

[email protected]

Copyright © 2023 - slash-root.com