Skip to content

Boolean literals in Python

  • by

In Python, boolean literals are used to represent the two truth values: True and False. They are part of the core data types in Python and are used in boolean operations, conditional statements, and loops. These literals are case-sensitive, and the first letter must be capitalized for them to be recognized as boolean values.

Here’s how you can use boolean literals in Python:

  1. True: Represents the boolean value “true” or “1” in Python.
  2. False: Represents the boolean value “false” or “0” in Python.

Examples:

# Correct usage of boolean literals
is_sunny = True
is_raining = False

if is_sunny:
    print("It's a sunny day!")
else:
    print("It's not sunny today.")

Python recognizes True as representing “true” or “1” and False as representing “false” or “0.” They are crucial for implementing logic and decision-making in Python programs.

Boolean literals in Python example

Here’s an example demonstrating the use of boolean literals in Python:

# Boolean literals example

# Assigning boolean literals to variables
is_sunny = True
is_raining = False

# Using boolean literals in a conditional statement
if is_sunny:
    print("It's a sunny day!")
else:
    print("It's not sunny today.")

# Using boolean literals in a boolean operation
result = True and False
print(result)  # Output: False

result = True or False
print(result)  # Output: True

result = not True
print(result)  # Output: False

Output:

Boolean literals in Python

You can assign boolean literals to variables, use them in conditional statements, and perform boolean operations with them.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading