Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110356 > unrolled thread
| Started by | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| First post | 2016-06-22 21:17 -0700 |
| Last post | 2016-06-28 23:32 +0000 |
| Articles | 20 — 13 participants |
Back to article view | Back to comp.lang.python
Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-22 21:17 -0700
Re: Break and Continue: While Loops Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-22 21:49 -0700
Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:08 -0700
Re: Break and Continue: While Loops Rustom Mody <rustompmody@gmail.com> - 2016-06-22 22:05 -0700
Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:09 -0700
Re: Break and Continue: While Loops DFS <nospam@dfs.com> - 2016-06-23 01:20 -0400
Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:09 -0700
Re: Break and Continue: While Loops Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-23 18:28 +1000
Re: Break and Continue: While Loops alister <alister.ware@ntlworld.com> - 2016-06-23 09:53 +0000
Re: Break and Continue: While Loops BartC <bc@freeuk.com> - 2016-06-23 11:15 +0100
Re: Break and Continue: While Loops Chris Angelico <rosuav@gmail.com> - 2016-06-23 21:39 +1000
Re: Break and Continue: While Loops Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-06-23 11:57 +0000
Re: Break and Continue: While Loops Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-26 00:32 -0700
Re: Break and Continue: While Loops Steven D'Aprano <steve@pearwood.info> - 2016-06-26 22:18 +1000
Re: Break and Continue: While Loops BartC <bc@freeuk.com> - 2016-06-23 16:52 +0100
Re: Break and Continue: While Loops Chris Angelico <rosuav@gmail.com> - 2016-06-24 02:23 +1000
Re: Break and Continue: While Loops pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-06-24 09:53 +0200
Re: Break and Continue: While Loops John Gordon <gordon@panix.com> - 2016-06-23 17:34 +0000
Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:42 -0700
Re: Break and Continue: While Loops Quivis <quivis@domain.invalid> - 2016-06-28 23:32 +0000
| From | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| Date | 2016-06-22 21:17 -0700 |
| Subject | Break and Continue: While Loops |
| Message-ID | <639b00e0-7b9d-4ed4-96ad-6afbcd536786@googlegroups.com> |
CODE #1:
i=0
while 1==1:
print(i)
i=i+1
if i>=5:
print("Breaking")
break
------
I understand that i=0 and i will only be printed if 1=1
The results of this is
0
1
2
3
4
Breaking
Why is Breaking going to be printed if i only goes up to 4? It does say if i>=5? Shouldn't this mean that the results should be:
0
1
2
3
4
5
CODE #2:
i=0
while True:
i=i+1
if i==2:
print("Skipping 2")
continue
if i==5:
print("Breaking")
break
print(i)
------
Questions:
1. what does the word True have to do with anything here?
2. i=i+1- I never understand this. Why isn't it i=i+2?
3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
4. How is i equal to 2 or 5 if i=0?
Thanks for all of your help!
[toc] | [next] | [standalone]
| From | Lawrence D’Oliveiro <lawrencedo99@gmail.com> |
|---|---|
| Date | 2016-06-22 21:49 -0700 |
| Message-ID | <6cec22f0-6e6d-4588-99e3-fd2abbc696fa@googlegroups.com> |
| In reply to | #110356 |
On Thursday, June 23, 2016 at 4:17:23 PM UTC+12, Elizabeth Weiss wrote:
>
> i=0
> while 1==1:
> print(i)
> i=i+1
> if i>=5:
> print("Breaking")
> break
>
> Why is Breaking going to be printed if i only goes up to 4? It does say if
> i>=5?
Because you incremented i after printing its value, and before checking it.
[toc] | [prev] | [next] | [standalone]
| From | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| Date | 2016-06-26 12:08 -0700 |
| Message-ID | <9cddb25e-e4d1-4243-8f70-c0e032e07feb@googlegroups.com> |
| In reply to | #110364 |
On Thursday, June 23, 2016 at 12:49:30 AM UTC-4, Lawrence D’Oliveiro wrote:
> On Thursday, June 23, 2016 at 4:17:23 PM UTC+12, Elizabeth Weiss wrote:
> >
> > i=0
> > while 1==1:
> > print(i)
> > i=i+1
> > if i>=5:
> > print("Breaking")
> > break
> >
> > Why is Breaking going to be printed if i only goes up to 4? It does say if
> > i>=5?
>
> Because you incremented i after printing its value, and before checking it.
Got it, Lawrence. Thanks so much for your help!!
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2016-06-22 22:05 -0700 |
| Message-ID | <edb8d6b2-57e1-46d9-84c0-6f2d5ab3c5f5@googlegroups.com> |
| In reply to | #110356 |
On Thursday, June 23, 2016 at 9:47:23 AM UTC+5:30, Elizabeth Weiss wrote:
> CODE #1:
>
> i=0
> while 1==1:
> print(i)
> i=i+1
> if i>=5:
> print("Breaking")
> break
>
> ------
> I understand that i=0 and i will only be printed if 1=1
> The results of this is
> 0
> 1
> 2
> 3
> 4
> Breaking
>
> Why is Breaking going to be printed if i only goes up to 4? It does say if i>=5? Shouldn't this mean that the results should be:
> 0
> 1
> 2
> 3
> 4
> 5
>
> CODE #2:
>
> i=0
> while True:
> i=i+1
> if i==2:
> print("Skipping 2")
> continue
> if i==5:
> print("Breaking")
> break
> print(i)
>
> ------
>
> Questions:
> 1. what does the word True have to do with anything here?
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> 4. How is i equal to 2 or 5 if i=0?
>
> Thanks for all of your help!
I suggested in your other post (Subject While Loops)
that the predecessor of python ABC's syntax for assignment would help unconfuse you
ie the ASSIGNMENT x=y we write as PUT y IN x
Using that rewrite your CODE 1 as
PUT 0 in i
while 1==1:
print(i)
PUT i+1 IN i
if i>=5:
print("Breaking")
break
Now try and rethink what that does
Then repeat for your other examples that confuse
[toc] | [prev] | [next] | [standalone]
| From | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| Date | 2016-06-26 12:09 -0700 |
| Message-ID | <bc5f3f24-4b40-4e81-bc10-34fb91ab50ad@googlegroups.com> |
| In reply to | #110367 |
On Thursday, June 23, 2016 at 1:06:09 AM UTC-4, Rustom Mody wrote:
> On Thursday, June 23, 2016 at 9:47:23 AM UTC+5:30, Elizabeth Weiss wrote:
> > CODE #1:
> >
> > i=0
> > while 1==1:
> > print(i)
> > i=i+1
> > if i>=5:
> > print("Breaking")
> > break
> >
> > ------
> > I understand that i=0 and i will only be printed if 1=1
> > The results of this is
> > 0
> > 1
> > 2
> > 3
> > 4
> > Breaking
> >
> > Why is Breaking going to be printed if i only goes up to 4? It does say if i>=5? Shouldn't this mean that the results should be:
> > 0
> > 1
> > 2
> > 3
> > 4
> > 5
> >
> > CODE #2:
> >
> > i=0
> > while True:
> > i=i+1
> > if i==2:
> > print("Skipping 2")
> > continue
> > if i==5:
> > print("Breaking")
> > break
> > print(i)
> >
> > ------
> >
> > Questions:
> > 1. what does the word True have to do with anything here?
> > 2. i=i+1- I never understand this. Why isn't it i=i+2?
> > 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> > 4. How is i equal to 2 or 5 if i=0?
> >
> > Thanks for all of your help!
>
> I suggested in your other post (Subject While Loops)
> that the predecessor of python ABC's syntax for assignment would help unconfuse you
>
> ie the ASSIGNMENT x=y we write as PUT y IN x
>
> Using that rewrite your CODE 1 as
>
> PUT 0 in i
> while 1==1:
> print(i)
> PUT i+1 IN i
> if i>=5:
> print("Breaking")
> break
>
> Now try and rethink what that does
> Then repeat for your other examples that confuse
Got it! Thank you!
[toc] | [prev] | [next] | [standalone]
| From | DFS <nospam@dfs.com> |
|---|---|
| Date | 2016-06-23 01:20 -0400 |
| Message-ID | <nkfriq$agr$2@dont-email.me> |
| In reply to | #110356 |
On 6/23/2016 12:17 AM, Elizabeth Weiss wrote:
> CODE #1:
>
> i=0
> while 1==1:
> print(i)
> i=i+1
> if i>=5:
> print("Breaking")
> break
>
> ------
> I understand that i=0 and i will only be printed if 1=1
> The results of this is
> 0
> 1
> 2
> 3
> 4
> Breaking
>
> Why is Breaking going to be printed if i only goes up to 4? It does say if i>=5? Shouldn't this mean that the results should be:
i is incremented up to 5, but you're printing i before incrementing and
evaluating it.
Move i=i+1 to the end and try it again.
> CODE #2:
>
> i=0
> while True:
> i=i+1
> if i==2:
> print("Skipping 2")
> continue
> if i==5:
> print("Breaking")
> break
> print(i)
>
> ------
>
> Questions:
> 1. what does the word True have to do with anything here?
'True' is the expression that's evaluated on each iteration of the While
loop.
while True:
print('True')
while 1:
print('True')
while 1==1:
print('True')
are all equivalent, and will run indefinitely, because the statements
after the while expression do nothing to stop the loop.
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
i=i+1 adds 1 to the current value of i, then assigns that value to i.
You can increment i by whatever you want:
i=i+1
i=i+2
i=i+35
etc.
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
The print results don't include 2 or 5 because you said 'continue' if 2
and 'break' if 5. So you skipped over 2, and exited at 5.
> 4. How is i equal to 2 or 5 if i=0?
i=0 before you entered the While loop. Then you increment it by one
(i=i+1) on each iteration of the loop.
> Thanks for all of your help!
Also, there's a million python resources on the web.
http://www.pythonschool.net/basics/while-loops/
[toc] | [prev] | [next] | [standalone]
| From | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| Date | 2016-06-26 12:09 -0700 |
| Message-ID | <e593bfae-766c-4094-acc9-97954e7c7da2@googlegroups.com> |
| In reply to | #110368 |
On Thursday, June 23, 2016 at 1:20:37 AM UTC-4, DFS wrote:
> On 6/23/2016 12:17 AM, Elizabeth Weiss wrote:
>
> > CODE #1:
> >
> > i=0
> > while 1==1:
> > print(i)
> > i=i+1
> > if i>=5:
> > print("Breaking")
> > break
> >
> > ------
> > I understand that i=0 and i will only be printed if 1=1
> > The results of this is
> > 0
> > 1
> > 2
> > 3
> > 4
> > Breaking
> >
> > Why is Breaking going to be printed if i only goes up to 4? It does say if i>=5? Shouldn't this mean that the results should be:
>
> i is incremented up to 5, but you're printing i before incrementing and
> evaluating it.
>
> Move i=i+1 to the end and try it again.
>
>
>
>
>
> > CODE #2:
> >
> > i=0
> > while True:
> > i=i+1
> > if i==2:
> > print("Skipping 2")
> > continue
> > if i==5:
> > print("Breaking")
> > break
> > print(i)
> >
> > ------
> >
> > Questions:
> > 1. what does the word True have to do with anything here?
>
> 'True' is the expression that's evaluated on each iteration of the While
> loop.
>
> while True:
> print('True')
>
> while 1:
> print('True')
>
> while 1==1:
> print('True')
>
> are all equivalent, and will run indefinitely, because the statements
> after the while expression do nothing to stop the loop.
>
>
>
>
> > 2. i=i+1- I never understand this. Why isn't it i=i+2?
>
> i=i+1 adds 1 to the current value of i, then assigns that value to i.
>
> You can increment i by whatever you want:
> i=i+1
> i=i+2
> i=i+35
> etc.
>
>
>
>
> > 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
>
> The print results don't include 2 or 5 because you said 'continue' if 2
> and 'break' if 5. So you skipped over 2, and exited at 5.
>
>
>
>
> > 4. How is i equal to 2 or 5 if i=0?
>
> i=0 before you entered the While loop. Then you increment it by one
> (i=i+1) on each iteration of the loop.
>
>
>
> > Thanks for all of your help!
>
>
> Also, there's a million python resources on the web.
>
> http://www.pythonschool.net/basics/while-loops/
Got it! Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-06-23 18:28 +1000 |
| Message-ID | <576b9d9e$0$2761$c3e8da3$76491128@news.astraweb.com> |
| In reply to | #110356 |
On Thursday 23 June 2016 14:17, Elizabeth Weiss wrote:
> CODE #2:
>
> i=0
> while True:
> i=i+1
> if i==2:
> print("Skipping 2")
> continue
> if i==5:
> print("Breaking")
> break
> print(i)
>
> ------
>
> Questions:
> 1. what does the word True have to do with anything here?
Consider:
"while the pasta is too hard to eat, keep boiling it"
"while you are feeling sick, stay in bed and drink plenty of fluids"
"While" repeatedly takes a condition, decides if it is true or false, and then
decides what to do. If you give it a condition True, that's *always* true, so
it repeats forever unless you use "break" to escape from the loop.
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
i = i + 1 increase i by one each time around the loop:
i = 0, then 1, then 2, then 3, then 4...
If you used i = i+2, it would increase by two each time around the loop:
i = 0, then 2, then 4, then 6, then ...
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> 4. How is i equal to 2 or 5 if i=0?
i starts off as 0. But then you increase it by 1 each time around the loop.
Can I ask, have you learned about for loops yet? There seems to be a fashion
among some teachers and educators to teach while loops before for loops. I
think that is a terrible idea, while loops are so much more complicated than
for loops.
Try this example instead:
for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
if i == 2:
print("skipping")
continue
if i == 5:
print("breaking")
break
print("i =", i)
The main thing you need to know to understand this is that the line "for i in
..." sets i=0, then runs the indented block under it, then sets i=1 and runs
the indented block, then sets i=2, and so forth. Run the code and see if it
makes sense to you. Feel free to ask as many questions as you like!
The line "for i in [0, 1, 2, ..." is a bit wordy and verbose. Can you imagine
if you wanted to loop 1000 times? Fortunately Python has an abbreviated
version:
for i in range(10):
if i == 2:
print("skipping")
continue
if i == 5:
print("breaking")
break
print("i =", i)
--
Steve
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.ware@ntlworld.com> |
|---|---|
| Date | 2016-06-23 09:53 +0000 |
| Message-ID | <0kOaz.154858$nm3.64299@fx38.am4> |
| In reply to | #110356 |
On Wed, 22 Jun 2016 21:17:03 -0700, Elizabeth Weiss wrote:
> CODE #1:
>
> i=0 while 1==1:
> print(i)
> i=i+1 if i>=5:
> print("Breaking") break
>
> ------
> I understand that i=0 and i will only be printed if 1=1 The results of
> this is 0
> 1
> 2
> 3
> 4
> Breaking
>
> Why is Breaking going to be printed if i only goes up to 4? It does say
> if i>=5? Shouldn't this mean that the results should be:
> 0
> 1
> 2
> 3
> 4
> 5
>
> CODE #2:
>
> i=0 while True:
> i=i+1
> if i==2:
> print("Skipping 2")
> continue
> if i==5:
> print("Breaking") break
> print(i)
>
> ------
>
> Questions:
> 1. what does the word True have to do with anything here?
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if
> i==5?
> 4. How is i equal to 2 or 5 if i=0?
>
> Thanks for all of your help!
unrelated to your question
while 1==1
Don't do this.
code 2 you correctly use while True
to see the problem with your code
try following it through line by line with a pen & paper
writing the value for I each time it is changed.
(this is known in the trade as a "Dry Run")
--
Fame is a vapor; popularity an accident; the only earthly certainty is
oblivion.
-- Mark Twain
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-06-23 11:15 +0100 |
| Message-ID | <nkgcrh$r90$1@dont-email.me> |
| In reply to | #110356 |
On 23/06/2016 05:17, Elizabeth Weiss wrote:
> CODE #1:
> i=0
> while True:
> i=i+1
> if i==2:
> print("Skipping 2")
> continue
> if i==5:
> print("Breaking")
> break
> print(i)
>
> ------
>
> Questions:
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> 4. How is i equal to 2 or 5 if i=0?
i=0 doesn't mean that 'i' is equal to 0.
In Python, "=" means 'assignment'. Something more like this:
SET i to 0
i=i+1 means:
SET i to i+1
'i' is a name for some value. The value of 'i' can change, usually
through assignments. If 'i' currently has the value 37, then:
SET i to i+1
will now store i+1 (ie. 38) into 'i'. Repeating that will set 'i' to 39
and so on.
To test if 'i' has a certain value, then '==' is used (to distinguish it
from '=' which means SET).
So i==37 will give True if i currently has a value of 37, or False
otherwise. This is used in your code:
if i==2:
print ("Skipping 2")
continue
The indented lines are executed when i has the value 2.
> 1. what does the word True have to do with anything here?
The 'while' statement (like others such as 'if') takes an expression
that should give True or False. If True, the loop is executed once more.
Actually pretty much any expression can be used, because Python can
interpret almost anything as either True or False. Don't ask for the
rules because they can be complicated, but for example, zero is False,
and any other number is True. I think.
Anyway, your code is doing an endless loop. Python doesn't have a
dedicated statement for an endless loop, so you have to use 'while True'
or 'while 1'.
(Well, the loop is only endless until you do an explicit exit out of it,
such as using 'break'.)
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-06-23 21:39 +1000 |
| Message-ID | <mailman.69.1466681968.11516.python-list@python.org> |
| In reply to | #110400 |
On Thu, Jun 23, 2016 at 8:15 PM, BartC <bc@freeuk.com> wrote: > Actually pretty much any expression can be used, because Python can > interpret almost anything as either True or False. Don't ask for the rules > because they can be complicated, but for example, zero is False, and any > other number is True. I think. The rules are very simple. Anything that represents "something" is true, and anything that represents "nothing" is false. An empty string, an empty list, an empty set, and the special "None" object (generally representing the absence of some other object) are all false. A string with something in it (eg "Hello"), a list with something in it (eg [1,2,4,8]), etc, etc, are all true. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Jon Ribbens <jon+usenet@unequivocal.co.uk> |
|---|---|
| Date | 2016-06-23 11:57 +0000 |
| Message-ID | <slrnnmnjm0.4us.jon+usenet@sable.unequivocal.co.uk> |
| In reply to | #110406 |
On 2016-06-23, Chris Angelico <rosuav@gmail.com> wrote: > On Thu, Jun 23, 2016 at 8:15 PM, BartC <bc@freeuk.com> wrote: >> Actually pretty much any expression can be used, because Python can >> interpret almost anything as either True or False. Don't ask for the rules >> because they can be complicated, but for example, zero is False, and any >> other number is True. I think. > > The rules are very simple. Anything that represents "something" is > true, and anything that represents "nothing" is false. An empty > string, an empty list, an empty set, and the special "None" object > (generally representing the absence of some other object) are all > false. A string with something in it (eg "Hello"), a list with > something in it (eg [1,2,4,8]), etc, etc, are all true. Exactly. This is a major everyday strength of Python in my view. I seem to recall that Java originally insisted that only booleans (excluding even Booleans, which are a different thing because of course they are) could be checked for truth and it was one of Java's significant warts.
[toc] | [prev] | [next] | [standalone]
| From | Lawrence D’Oliveiro <lawrencedo99@gmail.com> |
|---|---|
| Date | 2016-06-26 00:32 -0700 |
| Message-ID | <33a23d1b-320e-4ee7-987b-325b01843595@googlegroups.com> |
| In reply to | #110408 |
On Thursday, June 23, 2016 at 11:58:01 PM UTC+12, Jon Ribbens wrote: > I seem to recall that Java originally insisted that only booleans > (excluding even Booleans, which are a different thing because of > course they are) could be checked for truth and it was one of > Java's significant warts. Java’s wart was it didn’t implement Booleans the way they were done in Pascal. That wouldn’t have been a wart. Python’s Boolean handling is a wart in the other direction.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-06-26 22:18 +1000 |
| Message-ID | <576fc800$0$1600$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #110512 |
On Sun, 26 Jun 2016 05:32 pm, Lawrence D’Oliveiro wrote: > On Thursday, June 23, 2016 at 11:58:01 PM UTC+12, Jon Ribbens wrote: >> I seem to recall that Java originally insisted that only booleans >> (excluding even Booleans, which are a different thing because of >> course they are) could be checked for truth and it was one of >> Java's significant warts. > > Java’s wart was it didn’t implement Booleans the way they were done in > Pascal. That wouldn’t have been a wart. Oh? How are Java booleans different from Pascal booleans? Are you referring to "boxed" booleans, i.e. native bools in an object wrapper? -- Steven “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse.
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-06-23 16:52 +0100 |
| Message-ID | <nkh0jj$e4$1@dont-email.me> |
| In reply to | #110406 |
On 23/06/2016 12:39, Chris Angelico wrote: > On Thu, Jun 23, 2016 at 8:15 PM, BartC <bc@freeuk.com> wrote: >> Actually pretty much any expression can be used, because Python can >> interpret almost anything as either True or False. Don't ask for the rules >> because they can be complicated, but for example, zero is False, and any >> other number is True. I think. > > The rules are very simple. Anything that represents "something" is > true, and anything that represents "nothing" is false. An empty > string, an empty list, an empty set, and the special "None" object > (generally representing the absence of some other object) are all > false. A string with something in it (eg "Hello"), a list with > something in it (eg [1,2,4,8]), etc, etc, are all true. Maybe I was thinking of 'is' where sometimes the results are unintuitive. But even with ordinary conditionals, False is False, but [False] is True. And [] is False, while [[]] is True. A class instance is always True, even when empty. And then "False" is True as well! -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-06-24 02:23 +1000 |
| Message-ID | <mailman.80.1466698995.11516.python-list@python.org> |
| In reply to | #110424 |
On Fri, Jun 24, 2016 at 1:52 AM, BartC <bc@freeuk.com> wrote: > On 23/06/2016 12:39, Chris Angelico wrote: >> >> On Thu, Jun 23, 2016 at 8:15 PM, BartC <bc@freeuk.com> wrote: >>> >>> Actually pretty much any expression can be used, because Python can >>> interpret almost anything as either True or False. Don't ask for the >>> rules >>> because they can be complicated, but for example, zero is False, and any >>> other number is True. I think. >> >> >> The rules are very simple. Anything that represents "something" is >> true, and anything that represents "nothing" is false. An empty >> string, an empty list, an empty set, and the special "None" object >> (generally representing the absence of some other object) are all >> false. A string with something in it (eg "Hello"), a list with >> something in it (eg [1,2,4,8]), etc, etc, are all true. > > > But even with ordinary conditionals, False is False, but [False] is True. > And [] is False, while [[]] is True. A class instance is always True, even > when empty. And then "False" is True as well! [False] is a list with something in it. So is [[]]. "False" is a string with something in it, and honestly, if you think that ought to come out falsey, you're asking for magic. I don't know what you mean by a "class instance", or how it would be empty, but if you're creating a custom class and it might sometimes represent emptiness, you need to define either __len__ or __bool__ so the interpreter can understand what you mean by "empty". > Maybe I was thinking of 'is' where sometimes the results are unintuitive. > Not at all. The 'is' operator tells you whether the left and right operands are the same object. That's it. Maybe you have unintuitive situations where you don't realize you have the same object (or don't realize you have different objects), but the operator itself still has one very simple rule to follow. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) |
|---|---|
| Date | 2016-06-24 09:53 +0200 |
| Message-ID | <1mpce3m.abqjct1yseoyaN%pdorange@pas-de-pub-merci.mac.com> |
| In reply to | #110424 |
BartC <bc@freeuk.com> wrote: > But even with ordinary conditionals, False is False, but [False] is > True. And [] is False, while [[]] is True. A class instance is always > True, even when empty. And then "False" is True as well! "Empty" is not "Nothing". To be empty, something must exist first. -- Pierre-Alain Dorange Moof <http://clarus.chez-alice.fr/> Ce message est sous licence Creative Commons "by-nc-sa-2.0" <http://creativecommons.org/licenses/by-nc-sa/2.0/fr/>
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2016-06-23 17:34 +0000 |
| Message-ID | <nkh6j9$acb$1@reader1.panix.com> |
| In reply to | #110356 |
In <639b00e0-7b9d-4ed4-96ad-6afbcd536786@googlegroups.com> Elizabeth Weiss <cake240@gmail.com> writes:
> i=0
> while 1==1:
> print(i)
> i=i+1
> if i>=5:
> print("Breaking")
> break
> Why is Breaking going to be printed if i only goes up to 4?
Your code prints i and THEN adds one to it.
So i is 4, it gets printed, then 1 is added to it, so it becomes 5
and then the loop exits.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Elizabeth Weiss <cake240@gmail.com> |
|---|---|
| Date | 2016-06-26 12:42 -0700 |
| Message-ID | <b86a1950-6968-40df-886c-e7d812676b95@googlegroups.com> |
| In reply to | #110356 |
On Thursday, June 23, 2016 at 12:17:23 AM UTC-4, Elizabeth Weiss wrote:
> CODE #1:
>
> i=0
> while 1==1:
> print(i)
> i=i+1
> if i>=5:
> print("Breaking")
> break
>
> ------
> I understand that i=0 and i will only be printed if 1=1
> The results of this is
> 0
> 1
> 2
> 3
> 4
> Breaking
>
> Why is Breaking going to be printed if i only goes up to 4? It does say if i>=5? Shouldn't this mean that the results should be:
> 0
> 1
> 2
> 3
> 4
> 5
>
> CODE #2:
>
> i=0
> while True:
> i=i+1
> if i==2:
> print("Skipping 2")
> continue
> if i==5:
> print("Breaking")
> break
> print(i)
>
> ------
>
> Questions:
> 1. what does the word True have to do with anything here?
> 2. i=i+1- I never understand this. Why isn't it i=i+2?
> 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> 4. How is i equal to 2 or 5 if i=0?
>
> Thanks for all of your help!
Thank you, everyone!!! It all makes sense now!
[toc] | [prev] | [next] | [standalone]
| From | Quivis <quivis@domain.invalid> |
|---|---|
| Date | 2016-06-28 23:32 +0000 |
| Message-ID | <KODcz.857841$z63.608051@fx42.am4> |
| In reply to | #110537 |
On Sun, 26 Jun 2016 12:42:58 -0700, Elizabeth Weiss wrote: >> Thanks for all of your help! > > Thank you, everyone!!! It all makes sense now! Could you please snip out stuff you don't intend to respond to? Or even better, post one response message and add the thanks to the Subject, something like "Thanks all who helped - Re: Rest of subject". That way you don't have to open it and scroll forever and waste valuable time only to find a one-liner at the bottom. -- _____ __ __ __ __ __ __ __ (( )) || || || \\ // || (( \\_/X| \\_// || \V/ || \_)) Omnia paratus *~*~*~*~*~*~*
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web