I don't understand how true and false works

1
2

Like the title says I don't get how they work. I sort of understand them in while loops in that the while loop will only go on if it is true. I really didn't get how the print all links video answer works and the video didn't help. I also don't understand how if url: worked in that video.

asked 05 Mar '12, 20:31

Ashley-2's gravatar image

Ashley-2
1812
accept rate: 0%


One Answer:
11

It's actually easy (as everything, you already understand, of course, but this one really is):
True and False are the only two values you can get from any logical expression, e.g.:

  • Your name is Ashley. -> True
  • It is the year 1912. -> False
  • It is half past 2 AM here and I should really go to bed. -> True
  • 1 == 1 -> True
  • 1 + 1 == 3 -> False
  • etc.

Now, if statement takes an logical expression and allows its block to be executed only in case that expression evaluates to True:

if <Your name is Ashley>:
  <This block gets executed.>

The same applies to while loops. It will loop over and over again "while the condition (i.e. the logical expression) is evaluated as True" - that is why it is called "while"-loop. The same sentence in other words: While loop will stop right at the moment when the expression will evaluate as False - in case that expression will be always True, while will loop until the end of the Universe (or until the program crashes):

while <Your name is Ashley>:
    <This block will be executed>
    <over and over again>
    <until you will eventually get renamed.>

And the last thing is for if url:. It is just a shorthand notation for if url != "" (i.e. if url is not an empty string), given that url is a string. There is such shorthand for each data type:

  • boolean: if variable != False
  • number: if variable != 0
  • string: if variable != ""
  • list: if variable != []
  • etc.
link

answered 05 Mar '12, 20:45

Marek%20B%C3%A1lint's gravatar image

Marek Bálint
4.7k164285

edited 05 Mar '12, 20:49

Very solid answer, Mareq. I just wanted to add how important the concept of boolean (binary, on/off, true/false) is in computer science. It is the foundation of all digital machines, including computers and is esential for understanding all programming languages. Fortunately, it is pretty easy to understand.

(05 Mar '12, 21:01) Mark Brownstein Mark%20Brownstein's gravatar image

Thanks a lot, that was a really clear and useful explanation. I think I get it now

(06 Mar '12, 08:03) Ashley-2 Ashley-2's gravatar image

The last bit is sometimes referred to "Truthiness" - For example, a nonzero number is Truthy.

(06 Mar '12, 08:16) Thom Blake Thom%20Blake'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,276
×16

Asked: 05 Mar '12, 20:31

Seen: 236 times

Last updated: 06 Mar '12, 08:16