• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Negate Boolean in Python with not Operator

Negate Boolean in Python with not Operator

July 11, 2022 Leave a Comment

To negate a boolean variable in Python, the easiest way is with the not operator.

bool_var = True

print(not bool_var)

#Output:
False

When working with conditional expressions, the ability to change their values easily is valuable.

One such case is if you want to negate a conditional and negate a boolean value.

In some programming languages, you can use ! to negate a conditional expression, but in Python, the only want to negate a boolean value is with the Python not operator.

Below shows you how to negate a boolean variable with not in Python.

bool_var = True

print(not bool_var)

#Output:
False

Negating Conditional Expressions With not Operator in Python

In Python, when creating conditional expressions, you can use not to create complex expressions.

For example, if you want to create an if statement with multiple conditions and negate it, you can simply use not in the following way.

num = 1

if not num > 3 and num > 0:
    print("num is not greater than 3 but is greater than 0")
else:
    print("num is greater than 3")

#Output:
num is not greater than 3

You can also negate conditional expressions with not by wrapping the entire expression in parentheses.

num = 1

if not (num < 10 and num > 0):
    print("num is not between 0 and 10")
else:
    print("num is between 0 and 10")

#Output:
num is between 0 and 10

Hopefully this article has been useful for you to learn how to negate boolean values and negate conditional expressions in your Python code.

Other Articles You'll Also Like:

  • 1.  How to Split List in Half Using Python
  • 2.  How to Multiply All Elements in List Using Python
  • 3.  Ceiling Division in Python
  • 4.  Using Python to Replace Backslash in String
  • 5.  Python not in – Check if Value is Not Included in Object
  • 6.  Convert True to 1 in Python
  • 7.  Python Logging Timestamp – Print Current Time to Console
  • 8.  Read Pickle Files with pandas read_pickle Function
  • 9.  Change Python Turtle Background Color with screensize() Function
  • 10.  Convert String to Float with float() in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy