Re: Trying to understand nested loops
On 08/08/2022 12:59, Dan Purgert wrote: dn wrote: On 06/08/2022 11.41, avi.e.gr...@gmail.com wrote: I wonder if someone is pulling our leg as they are sending from an invalid email address of "GB " which is a bit sick. There are a number of folk who use evidently false email addresses - the OP's had me amused. Such 'hiding' is a matter for the List-Admins (thanks for all the work exerted on our behalf!) and how it fits with the Code-of-Conduct. Invalid sending addresses (email@somewhere.invalid) are a standard practice in Usenet, to combat bots scraping posts for email addresses (remember, Usenet predates basically all spam prevention tech). As there is a gateway between the mailing lists and Usenet, I *imagine* that the use of said invalid addresses are within the rules -- I mean, if they weren't, the maintainers wouldn't keep the two lists connected. > The history in my case is as follows: Years ago, I used Outlook Express, and, for setting up NG access, they suggested an example email address: some...@microsoft.com. I did not want to give my true email address, lest it be 'scraped', so I used: notsome...@microsoft.com. I added the NOT, to avoid misleading people. Later, I was prevailed upon to change com to invalid. List admins? This particular NG appears to be blessedly free of spam, but I hadn't realised it is moderated? -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
-BEGIN PGP SIGNED MESSAGE- Hash: SHA512 dn wrote: > On 06/08/2022 11.41, avi.e.gr...@gmail.com wrote: >> I wonder if someone is pulling our leg as they are sending from an >> invalid email address of "GB " which is >> a bit sick. > > There are a number of folk who use evidently false email addresses - the > OP's had me amused. > > Such 'hiding' is a matter for the List-Admins (thanks for all the work > exerted on our behalf!) and how it fits with the Code-of-Conduct. Invalid sending addresses (email@somewhere.invalid) are a standard practice in Usenet, to combat bots scraping posts for email addresses (remember, Usenet predates basically all spam prevention tech). As there is a gateway between the mailing lists and Usenet, I *imagine* that the use of said invalid addresses are within the rules -- I mean, if they weren't, the maintainers wouldn't keep the two lists connected. -BEGIN PGP SIGNATURE- iQIzBAEBCgAdFiEE3asj+xn6fYUcweBnbWVw5UznKGAFAmLw+rkACgkQbWVw5Uzn KGBT4A/8DR8GWlEgZhcoaLYTnQCfBv8YmykUxQ69/Xwf7o34S/ObtN0WTapgEBkB xamJHzFKENVeOoFwNgWkAaLSfOm2704DqO4vEJBZmCGrPtLTjHmpQuIcg2c+cjwz EEaYCuWmQZrviFS9ZeWX4FIMX9IlZ3Sgy7e/qHvdI5jeclw4oltYdRxVO3h9WylM BXn3Lvqntw+/5xV3vI/B2SODBTkB8a4D0vC64a3NwGqA3eUkyZrJYlVX8igubBwJ /sDLTy9EOK7wmokoXIHjj7KN+s5bC8idUN/V6DJiE6Z3vU0AOujKsw8jyQbndP9d 7da3gDLECMTHTFuKkj8kWC005aaWdM+GYzm1nofQC0jpINCP88IVce9curm11IQa AVWmxNBuGtLh8E3iE5WaydkgwZJXAll2cewOtzyJ0H8qeMwcqtL7hcKwdVPSzSYG JDqcbthDAzJg/PXcLjBHidXNZQ1BhyLw9ySzf+/7buiSsMdHuYZEohHXH4ybRMtL hlg6r09b5nDbKD7C9hVfL21+mogcX9t+LehsqiPLXssyMHRpTmGRzQ+CBySg5QaV gXshFJxRqvp9UtL+xw/wNL3wODsaVTU430qSKUI1ov7ClqcUXvEP5cbmKa9ufJ/J 8iDSbAP2B54pGyF9hfF0f8h267bZy13gWWqBqiFoCBZcVkaC/Jo= =6yDi -END PGP SIGNATURE- -- |_|O|_| |_|_|O| Github: https://github.com/dpurgert |O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860 -- https://mail.python.org/mailman/listinfo/python-list
RE: Trying to understand nested loops
This reminds me a bit of how routine spelling checkers and often especially grammar checkers, generate so many suggestions about mistakes as to become ignored. If you are writing using lots of names and jargon that keep getting flagged as errors and they are spelled or used exactly as you want, then it gets to be too annoying and you turn off the feature. Yes, you can sometimes silence some things in a spell checker that allows you to add words to a custom dictionary but for now, that dictionary is not known to every application that checks your spelling. That can be the nature of the beast. If your compiler decides to warn you every time you do a division that you might be dividing by zero because you do not perform the test right before that, then you end up with code that tests it AGAIN after having arranged for the value to never be zero in the first place, then test it again when you divide something else by it, ... I am sure some programming language has some kind of pragma that lets you whisper to the compiler that this variable at this time is guaranteed not to be zero, or that the region is in a try/catch zone which will intercept and fix the results of a divide by zero error. Just an example. And as you say, new or improved compilers can be made to be ever more picky about trying to find possible anomalies. Things are useful when they are somewhat manageable. If a compiler things it detects 372 places in your code where the same thing needs to be mentioned, such as reserving space for an object without simultaneously filling it with some contents, then it would be nice if instead of printing ALL of them, it consolidated it into one warning and say you can click on something to see the 372 instances. -Original Message- From: Python-list On Behalf Of Chris Angelico Sent: Saturday, August 6, 2022 8:12 AM To: python-list@python.org Subject: Re: Trying to understand nested loops On Sat, 6 Aug 2022 at 22:08, Richard Damon wrote: > > On 8/6/22 12:01 AM, Chris Angelico wrote: > > On Sat, 6 Aug 2022 at 13:54, Dan Stromberg wrote: > >> On Fri, Aug 5, 2022 at 12:54 PM Grant Edwards > >> > >> wrote: > >> > >>> In C, this doesn't do what it looks like it's supposed to do. > >>> > >>> if (foo) > >>> do_this(); > >>> and_this(); > >>> then_do_this(); > >>> > >> It's been quite a while since I used C, but with the right compiler > >> flag(s), I think this may be a thing of the past when compiling with gcc: > >> https://developers.redhat.com/blog/2016/02/26/gcc-6-wmisleading-ind > >> entation-vs-goto-fail > > Ah yes, because compiler warnings are always viewed and acted upon. > > > > Have you ever watched the compilation of a large open-source > > project, done using the project's own build system and therefore the > > team's preferred warning settings? It's normal to have such a spew > > of warnings that you can't find anything interesting, or to have new > > warnings in new versions of GCC be utterly useless for the same > > reason. > > > > ChrisA > > You make it so you HAVE to fix the warning by adding the option to > make warnings into errors. > > This does mean that you need to fix all the warnings that don't > actually mean anything, > > Good code shouldn't generate many warnings, either you have warnings > enabled that you don't care about, or your code is doing things you > have told the complier you shouldn't do. > I say again: have you ever watched the compilation of a large open-source project? You cannot turn warnings into errors, because there are ALWAYS warnings. Maybe, once upon a time, the policy was to ensure that there were no warnings on any major compiler; but times change, compilers add new warnings, new compilers join the club, and it becomes practically impossible to prevent warnings. Which, in turn, makes all warnings basically meaningless. Hmm. I don't think I've ever compiled gcc from source. Maybe I should do that, just to see whether gcc itself compiles with no warnings under gcc. ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On Sat, 6 Aug 2022 at 22:39, Richard Damon wrote: > > On 8/6/22 8:12 AM, Chris Angelico wrote: > > On Sat, 6 Aug 2022 at 22:08, Richard Damon wrote: > >> On 8/6/22 12:01 AM, Chris Angelico wrote: > >>> On Sat, 6 Aug 2022 at 13:54, Dan Stromberg wrote: > On Fri, Aug 5, 2022 at 12:54 PM Grant Edwards > wrote: > > > In C, this doesn't do what it looks like it's supposed to do. > > > > if (foo) > >do_this(); > >and_this(); > > then_do_this(); > > > It's been quite a while since I used C, but with the right compiler > flag(s), I think this may be a thing of the past when compiling with gcc: > https://developers.redhat.com/blog/2016/02/26/gcc-6-wmisleading-indentation-vs-goto-fail > >>> Ah yes, because compiler warnings are always viewed and acted upon. > >>> > >>> Have you ever watched the compilation of a large open-source project, > >>> done using the project's own build system and therefore the team's > >>> preferred warning settings? It's normal to have such a spew of > >>> warnings that you can't find anything interesting, or to have new > >>> warnings in new versions of GCC be utterly useless for the same > >>> reason. > >>> > >>> ChrisA > >> You make it so you HAVE to fix the warning by adding the option to make > >> warnings into errors. > >> > >> This does mean that you need to fix all the warnings that don't actually > >> mean anything, > >> > >> Good code shouldn't generate many warnings, either you have warnings > >> enabled that you don't care about, or your code is doing things you have > >> told the complier you shouldn't do. > >> > > I say again: have you ever watched the compilation of a large > > open-source project? You cannot turn warnings into errors, because > > there are ALWAYS warnings. Maybe, once upon a time, the policy was to > > ensure that there were no warnings on any major compiler; but times > > change, compilers add new warnings, new compilers join the club, and > > it becomes practically impossible to prevent warnings. Which, in turn, > > makes all warnings basically meaningless. > > > > Hmm. I don't think I've ever compiled gcc from source. Maybe I should > > do that, just to see whether gcc itself compiles with no warnings > > under gcc. > > > > ChrisA > > And for any project, that is a choice THEY made. Indeed. So you can't really say "good code shouldn't generate many warnings" unless (a) you're saying that lots of projects are made up of bad code, or (b) your statement that this is "a thing of the past" is flat-out false, because it can only be valid if you assume that everyone has that warning enabled, and preferably set to be an error. So, for the vast majority of projects out there, indentation errors are going to continue to go uncaught by C compilers. It's not "a thing of the past" until most projects use the flag, and preferably, the flag becomes active by default. And for the record, I have seen spurious warnings from *that exact flag* in a large project (an image parsing library). Spurious in that the code was actually correct, despite the compiler warning about it. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 8/6/22 8:12 AM, Chris Angelico wrote: On Sat, 6 Aug 2022 at 22:08, Richard Damon wrote: On 8/6/22 12:01 AM, Chris Angelico wrote: On Sat, 6 Aug 2022 at 13:54, Dan Stromberg wrote: On Fri, Aug 5, 2022 at 12:54 PM Grant Edwards wrote: In C, this doesn't do what it looks like it's supposed to do. if (foo) do_this(); and_this(); then_do_this(); It's been quite a while since I used C, but with the right compiler flag(s), I think this may be a thing of the past when compiling with gcc: https://developers.redhat.com/blog/2016/02/26/gcc-6-wmisleading-indentation-vs-goto-fail Ah yes, because compiler warnings are always viewed and acted upon. Have you ever watched the compilation of a large open-source project, done using the project's own build system and therefore the team's preferred warning settings? It's normal to have such a spew of warnings that you can't find anything interesting, or to have new warnings in new versions of GCC be utterly useless for the same reason. ChrisA You make it so you HAVE to fix the warning by adding the option to make warnings into errors. This does mean that you need to fix all the warnings that don't actually mean anything, Good code shouldn't generate many warnings, either you have warnings enabled that you don't care about, or your code is doing things you have told the complier you shouldn't do. I say again: have you ever watched the compilation of a large open-source project? You cannot turn warnings into errors, because there are ALWAYS warnings. Maybe, once upon a time, the policy was to ensure that there were no warnings on any major compiler; but times change, compilers add new warnings, new compilers join the club, and it becomes practically impossible to prevent warnings. Which, in turn, makes all warnings basically meaningless. Hmm. I don't think I've ever compiled gcc from source. Maybe I should do that, just to see whether gcc itself compiles with no warnings under gcc. ChrisA And for any project, that is a choice THEY made. For projects where code quality is actually a defined metric, there is normally a specified warning level (for a specified set of compilers and versions) that the code needs to compile at least nearly clean at. Yes, you can get that ton of warnings when at a higher warning level, but that is why you specify the warning level to use, and put the specific mitigations/suppressions for the few cases where the code is correct, but generates that warning. Yes, you can get a lot of warnings with another compiler, but that is because you aren't running at the correct warning level for that compiler, which is why the set of compilers that you are "warning free" on is specified. When you add a new compiler, it may first not be warning free until you make the effort (if you ever do) to make it warning free for that. Major open source projects will have a "toll gate" on the official repository that checks that additions keep the code to the standard it has established, -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On Sat, 6 Aug 2022 at 22:08, Richard Damon wrote: > > On 8/6/22 12:01 AM, Chris Angelico wrote: > > On Sat, 6 Aug 2022 at 13:54, Dan Stromberg wrote: > >> On Fri, Aug 5, 2022 at 12:54 PM Grant Edwards > >> wrote: > >> > >>> In C, this doesn't do what it looks like it's supposed to do. > >>> > >>> if (foo) > >>> do_this(); > >>> and_this(); > >>> then_do_this(); > >>> > >> It's been quite a while since I used C, but with the right compiler > >> flag(s), I think this may be a thing of the past when compiling with gcc: > >> https://developers.redhat.com/blog/2016/02/26/gcc-6-wmisleading-indentation-vs-goto-fail > > Ah yes, because compiler warnings are always viewed and acted upon. > > > > Have you ever watched the compilation of a large open-source project, > > done using the project's own build system and therefore the team's > > preferred warning settings? It's normal to have such a spew of > > warnings that you can't find anything interesting, or to have new > > warnings in new versions of GCC be utterly useless for the same > > reason. > > > > ChrisA > > You make it so you HAVE to fix the warning by adding the option to make > warnings into errors. > > This does mean that you need to fix all the warnings that don't actually > mean anything, > > Good code shouldn't generate many warnings, either you have warnings > enabled that you don't care about, or your code is doing things you have > told the complier you shouldn't do. > I say again: have you ever watched the compilation of a large open-source project? You cannot turn warnings into errors, because there are ALWAYS warnings. Maybe, once upon a time, the policy was to ensure that there were no warnings on any major compiler; but times change, compilers add new warnings, new compilers join the club, and it becomes practically impossible to prevent warnings. Which, in turn, makes all warnings basically meaningless. Hmm. I don't think I've ever compiled gcc from source. Maybe I should do that, just to see whether gcc itself compiles with no warnings under gcc. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 8/6/22 12:01 AM, Chris Angelico wrote: On Sat, 6 Aug 2022 at 13:54, Dan Stromberg wrote: On Fri, Aug 5, 2022 at 12:54 PM Grant Edwards wrote: In C, this doesn't do what it looks like it's supposed to do. if (foo) do_this(); and_this(); then_do_this(); It's been quite a while since I used C, but with the right compiler flag(s), I think this may be a thing of the past when compiling with gcc: https://developers.redhat.com/blog/2016/02/26/gcc-6-wmisleading-indentation-vs-goto-fail Ah yes, because compiler warnings are always viewed and acted upon. Have you ever watched the compilation of a large open-source project, done using the project's own build system and therefore the team's preferred warning settings? It's normal to have such a spew of warnings that you can't find anything interesting, or to have new warnings in new versions of GCC be utterly useless for the same reason. ChrisA You make it so you HAVE to fix the warning by adding the option to make warnings into errors. This does mean that you need to fix all the warnings that don't actually mean anything, Good code shouldn't generate many warnings, either you have warnings enabled that you don't care about, or your code is doing things you have told the complier you shouldn't do. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On Sat, 6 Aug 2022 at 13:54, Dan Stromberg wrote: > > On Fri, Aug 5, 2022 at 12:54 PM Grant Edwards > wrote: > > > In C, this doesn't do what it looks like it's supposed to do. > > > >if (foo) > > do_this(); > > and_this(); > >then_do_this(); > > > It's been quite a while since I used C, but with the right compiler > flag(s), I think this may be a thing of the past when compiling with gcc: > https://developers.redhat.com/blog/2016/02/26/gcc-6-wmisleading-indentation-vs-goto-fail Ah yes, because compiler warnings are always viewed and acted upon. Have you ever watched the compilation of a large open-source project, done using the project's own build system and therefore the team's preferred warning settings? It's normal to have such a spew of warnings that you can't find anything interesting, or to have new warnings in new versions of GCC be utterly useless for the same reason. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On Fri, Aug 5, 2022 at 12:54 PM Grant Edwards wrote: > In C, this doesn't do what it looks like it's supposed to do. > >if (foo) > do_this(); > and_this(); >then_do_this(); > It's been quite a while since I used C, but with the right compiler flag(s), I think this may be a thing of the past when compiling with gcc: https://developers.redhat.com/blog/2016/02/26/gcc-6-wmisleading-indentation-vs-goto-fail -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On Fri, 5 Aug 2022 08:34:45 +0100, ojomooluwatolami675 wrote: > Hello, I’m new to learning python and I stumbled upon a question nested > loops. This is the question below. Can you please how they arrived at 9 > as the answer. Thanks > > var = 0 for i in range(3): > for j in range(-2,-7,-2): > var += 1 > print(var) > > Sent from my iPhone That's simple. Number 9 itself is equal to 3*3. Inner loop executes 3 times for each of the 3 executions of the outer loop. PS: The reply is intentionally written in such a way that it cannot be used as an answer to a homework question. You'll have to work out the details yourself. -- Mladen Gogala Database Consultant https://dbwhisperer.wordpress.com -- https://mail.python.org/mailman/listinfo/python-list
RE: Trying to understand nested loops
I had considered that, Dave. Albeit others did at least put in some three-dot markers to show there was other code between the three lines shown. But the same silly argument they used applies elsewhere. Consider nested calls like: Delta(Gamma(Beta(Alpha))) Now say one of those functions takes an argument like 666. The following lines all mean different things, especially if all the above functions can take an optional argument of 666. Delta(Gamma(Beta(Alpha,666))) Delta(Gamma(Beta(Alpha),666)) Delta(Gamma(Beta(Alpha)),666) Delta(666,Gamma(Beta(Alpha))) Delta(Gamma(666,Beta(Alpha))) Delta(Gamma(Beta(666,Alpha))) And of course any such function calls may be in contexts such as this: Result, const = Delta(Gamma(Beta(Alpha))),666 My point is that whether using indentation or parentheses or braces or other grouping techniques, exact placement according to the rules must apply. I often write code where I use indentation to remind ME which argument goes with which, such as this (more often not in python where indentation has no real meaning and things can span multiple lines. This: Delta(Gamma(666,Beta(Alpha))) May make more sense to write like this: Delta(Gamma(666, Beta(Alpha))) Or in a more general case where each of the functions may take multiple arguments before and/or after nested, calls, I might have a long convoluted code where all arguments to a particular function are vertically aligned and it is easier to spot if you left one out or put it in the wrong place. It becomes a somewhat childish argument when writing CODE in any language with rules, to suggest that it should ignore your mistakes and assume you meant to have a comma here or parentheses there and similarly, that the indentation level should govern which block your print statement is part of. Hence my suggestion that perhaps someone is in a sense punking us Of course it is perfectly possible the software this person is using makes that deliberately unworkable email address as I have seen this elsewhere. It just raises my suspicion level when I have seen other posts on various mailing lists ranging from someone with pretty much no knowledge about a topic but wanting someone to do their homework, to someone who throws in something (perhaps incendiary) to watch others waste their time trying to deal with their best guesses of what was wanted, to one guy who seems to write articles or books and wants to see what people think but then does not participate or tell us that is what they wanted. My point was not to tell anyone else here what to do, simply that I will be cautious with such posters as I have way better things to do! Nested loops are indeed a hard topic for many. But when explained it no longer seems reasonable to ask why print statements at different levels of nesting differ. Not to me, at least. - Avi (for those like someone on another language/group who did not know how to address me in overall too-polite format and referred to me as "Dear " followed by more lines. -Original Message- From: Python-list On Behalf Of dn Sent: Friday, August 5, 2022 7:58 PM To: python-list@python.org Subject: Re: Trying to understand nested loops On 06/08/2022 11.41, avi.e.gr...@gmail.com wrote: > I wonder if someone is pulling our leg as they are sending from an > invalid email address of "GB " which is a bit sick. There are a number of folk who use evidently false email addresses - the OP's had me amused. Such 'hiding' is a matter for the List-Admins (thanks for all the work exerted on our behalf!) and how it fits with the Code-of-Conduct. > I have trouble imagining ANYONE learning a language like python > without rapidly being told that python uses indentation instead of > various ways to detect when a body of text is considered a single composite item. > > And code like their example is also nonsense: > > print(var) > print(var) > print(var) Another way to look at that post, and what the author may have meant; is that the final print(), incorrectly indented in the OP, could have been written with three different indentations, and thus have three very different effects (cf that they are all to be used, as-is). -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 06/08/2022 11.41, avi.e.gr...@gmail.com wrote: > I wonder if someone is pulling our leg as they are sending from an invalid > email address of "GB " which is a bit sick. There are a number of folk who use evidently false email addresses - the OP's had me amused. Such 'hiding' is a matter for the List-Admins (thanks for all the work exerted on our behalf!) and how it fits with the Code-of-Conduct. > I have trouble imagining ANYONE learning a language like python without > rapidly being told that python uses indentation instead of various ways to > detect when a body of text is considered a single composite item. > > And code like their example is also nonsense: > > print(var) > print(var) > print(var) Another way to look at that post, and what the author may have meant; is that the final print(), incorrectly indented in the OP, could have been written with three different indentations, and thus have three very different effects (cf that they are all to be used, as-is). -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On Fri, Aug 5, 2022 at 12:30 PM GB wrote: > On 05/08/2022 08:56, Frank Millman wrote: > > > BTW, there is an indentation error in your original post - line 5 should > > line up with line 4. > > As a Python beginner, I find that Python is annoyingly picky about > indents. And, the significance of indents is a bit of a minefield for > beginners. > No, you should indent properly anyway. Python just reduces the number of things to worry about. Please see: https://stromberg.dnsalias.org/~strombrg/significant-whitespace.html ...for the usual. -- https://mail.python.org/mailman/listinfo/python-list
RE: Trying to understand nested loops
I wonder if someone is pulling our leg as they are sending from an invalid email address of "GB " which is a bit sick. I have trouble imagining ANYONE learning a language like python without rapidly being told that python uses indentation instead of various ways to detect when a body of text is considered a single composite item. And code like their example is also nonsense: print(var) print(var) print(var) On my version of python, and likely most or all, it generates an error message like: SyntaxError: multiple statements found while compiling a single statement The way I see it, it is beyond irrelevant when learning a language what YOU (the learner) think. The rules are the rules. If you do not like them and do not have to use that language, go find another you might like. If you want to stay with the language, you not only adjust but welcome new ways of doing things. I have seen people stuck on code like this: a, b, c = 5,4,3 others get nauseous at something like: a, b, _ = some_func(args) But the whole point is learning to appreciate what someone decided might make the language more useful, easier to program in, avoid errors, be more flexible, or maybe more efficient, or whatever design criteria apply. Only once you understand more things like that, can you be in a position to critique it as horrible. So, speaking for myself, the poster is assumed, for now, to not be worth responding to. I doubt they are a true beginner or at least that they have spent any serious time learning. -Original Message- From: Python-list On Behalf Of GB Sent: Friday, August 5, 2022 5:57 AM To: python-list@python.org Subject: Re: Trying to understand nested loops On 05/08/2022 08:56, Frank Millman wrote: > BTW, there is an indentation error in your original post - line 5 > should line up with line 4. As a Python beginner, I find that Python is annoyingly picky about indents. And, the significance of indents is a bit of a minefield for beginners. For example, in the above code, the indent of the final line very significantly affects the results: print(var) print(var) print(var) These are all different. -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 06/08/2022 10.50, Dan Stromberg wrote: > On Fri, Aug 5, 2022 at 12:35 AM wrote: ... > Of if you don't have (or want) a debugger, you could change it to: > > var = 0 > for i in range(3): > print('i is', i) > for j in range(-2,-7,-2): > print('j is', j) > var += 1 > print(var) > > And note that 3 times 3 is 9. Combining the above advice and illustration, with several more recent comments about indentation revealing structure, should the above be amended to: indent = "" # your choice of how many spaces/tabs ... print( "i is", i ) ... print( indent + "j is", j ) ... print( indent + indent, var ) # add sep="" if pernickety ... Thus, the output matches the code and each complements the other's view of structure! -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On Fri, Aug 5, 2022 at 12:35 AM wrote: > Hello, I’m new to learning python and I stumbled upon a question nested > loops. This is the question below. Can you please how they arrived at 9 as > the answer. Thanks > > var = 0 > for i in range(3): > for j in range(-2,-7,-2): > var += 1 > print(var) > A debugger is useful for more than debugging; it can also help give an intuition for control flow. If you single step through this snippet with a debugger, you'll probably see what's happening. Of if you don't have (or want) a debugger, you could change it to: var = 0 for i in range(3): print('i is', i) for j in range(-2,-7,-2): print('j is', j) var += 1 print(var) And note that 3 times 3 is 9. -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 8/5/22 03:56, GB wrote: > On 05/08/2022 08:56, Frank Millman wrote: > >> BTW, there is an indentation error in your original post - line 5 >> should line up with line 4. > > As a Python beginner, I find that Python is annoyingly picky about > indents. And, the significance of indents is a bit of a minefield for > beginners. > > For example, in the above code, the indent of the final line very > significantly affects the results: > > print(var) > print(var) > print(var) > > These are all different. Yes, very picky. Python has chosen to use a (consistent) indent to indicate a code block, as opposed to using extra syntactical characters (curly braces, etc.) to delimit blocks. It's just a choice that was made about how to instruct the interpreter what you mean, and there's some argument that it improves readability later, when you go look at your code months later, or someone else's code: the requirement to have consistent indents means your brain can trust that the way the code is indented is meaningful, rather than arbitrary. Also note that most (all?) code formatters for other languages will enforce consistent indenting too, In Python it just happens to be part of the language rather than optional. So: you need to be clear about what you mean by indenting correctly. All good code editors that understand Python understand about this and will help you as much as they can. -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 2022-08-05, GB wrote: >> BTW, there is an indentation error in your original post - line 5 >> should line up with line 4. > > As a Python beginner, I find that Python is annoyingly picky about > indents. And, the significance of indents is a bit of a minefield for > beginners. As a C beginner, you'll find that C is annoyingly picky about curly braces and semcolons. All programming languages are picky about syntax. > For example, in the above code, the indent of the final line very > significantly affects the results: > > print(var) > print(var) > print(var) > > These are all different. Indeed. It looks different, and it _is_ different. Seems like a good thing to me. I like programs to do what it looks like they are going to do. In C, this doesn't do what it looks like it's supposed to do. if (foo) do_this(); and_this(); then_do_this(); -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 05/08/2022 08:56, Frank Millman wrote: BTW, there is an indentation error in your original post - line 5 should line up with line 4. As a Python beginner, I find that Python is annoyingly picky about indents. And, the significance of indents is a bit of a minefield for beginners. For example, in the above code, the indent of the final line very significantly affects the results: print(var) print(var) print(var) These are all different. -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
ojomooluwatolami...@gmail.com wrote at 2022-8-5 08:34 +0100: >Hello, I’m new to learning python and I stumbled upon a question nested loops. For future, more complex, questions of this kind, you might have a look at the module `pdb` in Python's runtime library. It implements a debugger which allows you (among other features) to interactively run a program line by line and explore the state of all involved variables. There are also IDEs (= Integrated Development Environments) which support this. Whenever a program does things you do not understand, debugging usually helps to bring light into the scene. -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
It’s also a poor code example. Doing a pointless double loop is not good instructional practice, especially when simpler alternatives exist. e.g. for i in range(3): for j in range(-2.-7,-2): print(i +j ) — Gerard Weatherby | Application Architect NMRbox | NAN | Department of Molecular Biology and Biophysics UConn Health 263 Farmington Avenue, Farmington, CT 06030-6406 uchc.edu On Aug 5, 2022, 4:38 AM -0400, ojomooluwatolami...@gmail.com , wrote: var = 0 for i in range(3): for j in range(-2,-7,-2): var += 1 print(var) -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 05Aug2022 09:47, Lars Liedtke wrote: >this looks to me like it might be a piece of homework, as it would be >given by teachers or professors. > >This list has got the rule, that members do not solve other's >homework. Because very often homework is meant to sit down and think >about it. Very true. However, we're happy to help people understand things. In this case it look like Frank has provided apt advice, showing the OP how to see how many times the inner loop runs, which affects how many times `var+=1` occurs. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
On 2022-08-05 9:34 AM, ojomooluwatolami...@gmail.com wrote: Hello, I’m new to learning python and I stumbled upon a question nested loops. This is the question below. Can you please how they arrived at 9 as the answer. Thanks var = 0 for i in range(3): for j in range(-2,-7,-2): var += 1 print(var) Welcome to Python. I am sure you are going to enjoy it. To learn Python, you must learn to use the Python interactive prompt (also known as the REPL). Type 'python' at your console, and it should bring up something like this - C:\Users\E7280>python Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> The confusing part of your example above is 'for j in range(-2,-7,-2)'. To find out what it does, enter it at the '>>>' prompt - >>> for j in range(-2, -7, -2): ... print(j) ... -2 -4 -6 >>> For the purposes of your exercise, all you need to know at this stage is that it loops three times. Does that help answer your question? If not, feel free to come back with more questions. BTW, there is an indentation error in your original post - line 5 should line up with line 4. It is preferable to copy/paste your code into any messages posted here rather than type it in, as that avoids the possibility of any typos. Frank Millman -- https://mail.python.org/mailman/listinfo/python-list
Re: Trying to understand nested loops
Hello, this looks to me like it might be a piece of homework, as it would be given by teachers or professors. This list has got the rule, that members do not solve other's homework. Because very often homework is meant to sit down and think about it. But maybe I interpreted that wrongly, so if you could try to formulate in words what this loop does, I or other people on this list, will be pleased to show you where you might have got things wrong and how it really works. Cheers Lars -- Lars Liedtke Software Entwickler Phone: Fax:+49 721 98993- E-mail: l...@solute.de solute GmbH Zeppelinstraße 15 76185 Karlsruhe Germany Marken der solute GmbH | brands of solute GmbH billiger.de | Shopping.de Geschäftsführer | Managing Director: Dr. Thilo Gans, Bernd Vermaaten Webseite | www.solute.de Sitz | Registered Office: Karlsruhe Registergericht | Register Court: Amtsgericht Mannheim Registernummer | Register No.: HRB 110579 USt-ID | VAT ID: DE234663798 Informationen zum Datenschutz | Information about privacy policy http://solute.de/ger/datenschutz/grundsaetze-der-datenverarbeitung.php Am 05.08.22 um 09:34 schrieb ojomooluwatolami...@gmail.com: Hello, I’m new to learning python and I stumbled upon a question nested loops. This is the question below. Can you please how they arrived at 9 as the answer. Thanks var = 0 for i in range(3): for j in range(-2,-7,-2): var += 1 print(var) Sent from my iPhone -- https://mail.python.org/mailman/listinfo/python-list
Trying to understand nested loops
Hello, I’m new to learning python and I stumbled upon a question nested loops. This is the question below. Can you please how they arrived at 9 as the answer. Thanks var = 0 for i in range(3): for j in range(-2,-7,-2): var += 1 print(var) Sent from my iPhone -- https://mail.python.org/mailman/listinfo/python-list