Back to Blog
Jan 14, 20232 min read

Booleans In Python

Python
Booleans In Python

In Python, booleans are used to represent true or false values. The two possible boolean values are True and False, which must be written with a capital "T" and "F".

Here are some examples of working with booleans in Python:

# Assign a boolean value to a variable
is_raining = True
is_sunny = False
# Compare values
x = 5
print(x > 3)  # prints True
print(x < 3)  # prints False
# Check equality
name = "Bob"
print(name == "Bob")  # prints True
print(name == "Alice")  # prints False
# Check membership
fruits = ["apple", "banana", "orange"]
print("banana" in fruits)  # prints True
print("grape" in fruits)   # prints False

Boolean variables are used to test conditions, such as in an if statement:

x = 10
if x > 5:
    print("x is greater than 5")

and they are also used as a result of a comparison or logic operation:

a, b = 2, 3
print(a < b) # prints True
print(a == b) # prints False
print(not (a != b)) # prints False

Boolean values, and the operations that work with them, are a fundamental building block of control flow in programming, for example, you could use if-else statements, or while loops that are controlled by a boolean value.

If you Want to Learn Python, You can watch my ultimate Python Course on My Youtube Channel.

You can join there as well to share your Queries and suggestions. Facebook Facebook Group: https://web.facebook.com/groups/890525732087988/?mibextid=HsNCOg

Thanks For Reading.

You can Also Follow Me on My Social Media Platforms:

  1. Facebook
  2. Youtube
  3. Twitter & Instagram
  4. GitHub & Replit
  5. Upwork

💬 Got questions? Ask me anything!