"Not" in an if statement

What exactly does "not" mean in an an if statement? Does it mean to proceed to the next step only if the text expression is False instead of True?

asked 04 Mar '12, 16:26

Elizabeth%20A.%20Pressler-1's gravatar image

Elizabeth A....
1.1k2471107
accept rate: 4%


4 Answers:

Not changes True to False or False to True. If not True means If False, If not False - means If True.

link

answered 04 Mar '12, 16:29

Andrius%20Simanynas's gravatar image

Andrius Sima...
6051621

This has to do with boolean variables. 'not', as you point out, is the opposite. For example

if (day is hot):    # 'day is hot' is True: go surfing
    go surfing
else:               # 'day is hot' is False: go snowboarding
    go snowboarding

is exactly the same as

if not (day is hot):  # 'not (day is hot)' is True -> day is cold: snowboard
    go snowboarding
else:
    go surfing       # it is NOT True that 'day is not hot' -> day is hot: go surfing

You can combine the 3 boolean operators - not, and and or - to make very complex expressions. For example:

if not(sick or busy) and not(is_raining or is_hailing or is_snowing) and ( (have money for gas) or (can get a ride)):
    go surfing
    

In this case, the expressions are evaluated like in math.. first evaluate the most inner parenthesis and work your way outwards.

link

answered 04 Mar '12, 17:34

Goldsong's gravatar image

Goldsong
19.5k29111104

edited 04 Mar '12, 17:40

When we say

if a or b:
     print "Hello!"

then Hello! will be printed if a is true or b is true (or both are true).

When we say

if not a:
    print "Hello!"

Then Hello! will be printed if a is not true.

link

answered 04 Mar '12, 16:30

Anton%20Golov's gravatar image

Anton Golov ♦
13.3k2174174

edited 04 Mar '12, 17:18

Format this differently please, it is unreadable, specially for beginners! -- thanks.

(04 Mar '12, 17:16) Angel Angel's gravatar image

@LizP In essence, you got it right.


if not a == b:
print 'ok'

would be the same as:

if a != b:
print 'ok'

In both, 'ok' would be printed
Furthermore, Python is one of those languages considering True a variable being assigned (I love this):

x = 'toto'
if not x:
print 'ok'

Here, 'ok' would not be printed as x is a string and is considered True, but 'not' revert it to False.

link

answered 04 Mar '12, 17:18

Angel's gravatar image

Angel
7.0k632112

edited 04 Mar '12, 17:26

Uhm, not really. Python just has a bunch of "Truth-ish" and "False-ish" values. You'll find an empty list to be false, for example. An unassigned variable is, on the other hand, an exception.

(04 Mar '12, 17:28) Anton Golov ♦ Anton%20Golov's gravatar image

An unassigned variable is an error, is it not?

(04 Mar '12, 17:29) Angel Angel's gravatar image

Yeah, sorry; an exception is just a fancy word for that. :)

(04 Mar '12, 17:43) Anton Golov ♦ Anton%20Golov's gravatar image
Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,294
×27
×24
×16
×11

Asked: 04 Mar '12, 16:26

Seen: 335 times

Last updated: 04 Mar '12, 17:43