Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negation in Python

People also ask

What is a negation in Python?

Negation: The not operator in Python can be used only in the unary form, which means negation, returning the a result that is the opposite of its operand. Its boolean prototype is not (bool) -> bool.

How do you find the negation in Python?

Use the not Operator to Negate a Boolean in Python Here, the bool() function is used. It returns the boolean value, True or False , of a given variable in Python. The boolean values of the numbers 0 and 1 are set to False and True as default in Python. So, using the not operator on 1 returns False , i.e., 0 .

How do you negate a sentence in Python?

just add "... NOT!" to the end of any statement to negate it ;) Or in an Orwellian/1984 sense, add the "un" prefix to any adjective... "This book is ungood" :-) – Eric J.

Is not VS != In Python?

The != operator compares the value or equality of two objects, whereas the Python is not operator checks whether two variables point to the same object in memory.


The negation operator in Python is not. Therefore just replace your ! with not.

For your example, do this:

if not os.path.exists("/usr/share/sounds/blues") :
    proc = subprocess.Popen(["mkdir", "/usr/share/sounds/blues"])
    proc.wait()

For your specific example (as Neil said in the comments), you don't have to use the subprocess module, you can simply use os.mkdir() to get the result you need, with added exception handling goodness.

Example:

blues_sounds_path = "/usr/share/sounds/blues"
if not os.path.exists(blues_sounds_path):
    try:
        os.mkdir(blues_sounds_path)
    except OSError:
        # Handle the case where the directory could not be created.

Python prefers English keywords to punctuation. Use not x, i.e. not os.path.exists(...). The same thing goes for && and || which are and and or in Python.


try instead:

if not os.path.exists(pathName):
    do this

Combining the input from everyone else (use not, no parens, use os.mkdir) you'd get...

special_path_for_john = "/usr/share/sounds/blues"
if not os.path.exists(special_path_for_john):
    os.mkdir(special_path_for_john)