-2

Do Python have logical inversion mark like exclamation mark in Java?

In Java,

bool myBool = true; System.out.println(!myBool); should give us the output "false".

But what about Python? In Python, is there any mark?

edit: I know the keyword "not". I am asking for a mark.

Talha D.
  • 41
  • 1
  • 3

2 Answers2

7

There is no !-like prefix operator for booleans in Python, for this we use not.

>>> not True
False
>>> not False
True
>>> not 1
False
>>> not 0
True

There is a prefix (unary) bitwise not, ~, but since Python's integers are signed integers that grow in size instead of rolling over, it's harder to demonstrate it.

>>> ~0b00000001
-2

But you can see it if you use a mask of all ones (for the number of bits in your integer) and format it correctly:

>>> f'{~(-2) & 0b11111111:08b}'
'00000001'
>>> f'{~(~(-2)) & 0b11111111:08b}'
'11111110'

This is generally only useful for bitwise operations, though.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
2

Python does not have any such mark. As you seem to already know, the not keyword is what is used in Python. You can use != to mean "not equal to" but that's it.

Nobozarb
  • 344
  • 1
  • 12