In python you can write an if statement as follows
var = True
if var:
print 'I\'m here'
is there any way to do the opposite without the ==, eg
var = False
if !var:
print 'learnt stuff'
Use the strict equality (===) operator to check if a variable is equal to false - myVar === false . The strict equality operator will return true if the variable is equal to false , otherwise it will return false . Copied!
The Boolean value of undefined is false.
You can check if a value is either truthy or falsy with the built-in bool() function. According to the Python Documentation, this function: Returns a Boolean value, i.e. one of True or False . x (the argument) is converted using the standard truth testing procedure.
The Javascript standard defines true and false values as a unique data type called a Javascript boolean. Javascript booleans may be true , false , or (in certain contexts) a value that evaluates to either true or false .
Use not
var = False
if not var:
print 'learnt stuff'
Since Python evaluates also the data type NoneType as False during the check, a more precise answer is:
var = False
if var is False:
print('learnt stuff')
This prevents potentially unwanted behaviour such as:
var = [] # or None
if not var:
print('learnt stuff') # is printed what may or may not be wanted
But if you want to check all cases where var will be evaluated to False, then doing it by using logical not keyword is the right thing to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With