Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if var == False

Tags:

python

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'
like image 838
Phedg1 Avatar asked May 13 '13 00:05

Phedg1


People also ask

How do you know if a variable is false?

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!

Is undefined === false?

The Boolean value of undefined is false.

How do I check if Python is true or 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.

Is true or false in JavaScript?

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 .


2 Answers

Use not

var = False
if not var:
    print 'learnt stuff'
like image 80
Tamil Selvan C Avatar answered Oct 23 '22 05:10

Tamil Selvan C


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.

like image 63
colidyre Avatar answered Oct 23 '22 04:10

colidyre