Trying to understand nested loops

2022-08-05 Thread ojomooluwatolami675
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


Re: Trying to understand nested loops

2022-08-05 Thread Lars Liedtke

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


Re: Trying to understand nested loops

2022-08-05 Thread Frank Millman

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

2022-08-05 Thread Cameron Simpson
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

2022-08-05 Thread Weatherby,Gerard
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

2022-08-05 Thread Dieter Maurer
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: Exclude 'None' from list comprehension of dicts

2022-08-05 Thread Loris Bennett
Antoon Pardon  writes:

> Op 4/08/2022 om 13:51 schreef Loris Bennett:
>> Hi,
>>
>> I am constructing a list of dictionaries via the following list
>> comprehension:
>>
>>data = [get_job_efficiency_dict(job_id) for job_id in job_ids]
>>
>> However,
>>
>>get_job_efficiency_dict(job_id)
>>
>> uses 'subprocess.Popen' to run an external program and this can fail.
>> In this case, the dict should just be omitted from 'data'.
>>
>> I can have 'get_job_efficiency_dict' return 'None' and then run
>>
>>filtered_data = list(filter(None, data))
>>
>> but is there a more elegant way?
>
> Just wondering, why don't you return an empty dictionary in case of a failure?
> In that case your list will be all dictionaries and empty ones will be 
> processed
> fast enough.

When the list of dictionaries is processed, I would have to check each
element to see if it is empty.  That strikes me as being less efficient
than filtering out the empty dictionaries in one go, although obviously
one would need to benchmark that.

Cheers,

Loris


-- 
This signature is currently under construction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trying to understand nested loops

2022-08-05 Thread GB

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

2022-08-05 Thread Grant Edwards
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: Exclude 'None' from list comprehension of dicts

2022-08-05 Thread avi.e.gross
Benchmarking aside, Lori, there are some ideas about such things.

You are describing a case, in abstract terms, where an algorithm grinds away
and produces results that may include an occasional or a common unwanted
result. The question is when to eliminate the unwanted. Do you eliminate
them immediately at the expense of some extra code at that point, or do you
want till much later or even at the end?

The answer is it DEPENDS and let me point out that many problems can start
multi-dimensional (as in processing a 5-D matrix) and produce a linear
output (as in a 1-D list) or it can be the other way around. Sometimes what
you want eliminated is something like duplicates. Is it easier to remove
duplicates as they happen, or later when you have some huge data structure
containing oodles of copies of each duplicate?

You can imagine many scenarios and sometimes you need to also look at costs.
What does it cost to check if a token is valid, as in can the word be found
in a dictionary? Is it cheaper to wait till you have lots of words including
duplicates and do one lookup to find a bad word then mark it so future
occurrences are removed without that kind of lookup? Or is it better to read
I the dictionary once and hash it so later access is easy?

In your case, you have a single simple criterion for recognizing an item to
leave out. So the above may not apply. But I note we often use pre-created
software that simply returns a result and then the only reasonable way to
remove things  is after calling it. Empty or unwanted items may take up some
room, though, so a long-running process may be better off pruning as it
goes.

-Original Message-
From: Python-list  On
Behalf Of Loris Bennett
Sent: Friday, August 5, 2022 1:50 AM
To: python-list@python.org
Subject: Re: Exclude 'None' from list comprehension of dicts

Antoon Pardon  writes:

> Op 4/08/2022 om 13:51 schreef Loris Bennett:
>> Hi,
>>
>> I am constructing a list of dictionaries via the following list
>> comprehension:
>>
>>data = [get_job_efficiency_dict(job_id) for job_id in job_ids]
>>
>> However,
>>
>>get_job_efficiency_dict(job_id)
>>
>> uses 'subprocess.Popen' to run an external program and this can fail.
>> In this case, the dict should just be omitted from 'data'.
>>
>> I can have 'get_job_efficiency_dict' return 'None' and then run
>>
>>filtered_data = list(filter(None, data))
>>
>> but is there a more elegant way?
>
> Just wondering, why don't you return an empty dictionary in case of a
failure?
> In that case your list will be all dictionaries and empty ones will be 
> processed fast enough.

When the list of dictionaries is processed, I would have to check each
element to see if it is empty.  That strikes me as being less efficient than
filtering out the empty dictionaries in one go, although obviously one would
need to benchmark that.

Cheers,

Loris


--
This signature is currently under construction.
--
https://mail.python.org/mailman/listinfo/python-list

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trying to understand nested loops

2022-08-05 Thread Mats Wichmann
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

2022-08-05 Thread Dan Stromberg
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

2022-08-05 Thread dn
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

2022-08-05 Thread avi.e.gross
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

2022-08-05 Thread Dan Stromberg
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

2022-08-05 Thread dn
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

2022-08-05 Thread avi.e.gross
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

2022-08-05 Thread Mladen Gogala via Python-list
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

2022-08-05 Thread Dan Stromberg
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

2022-08-05 Thread Chris Angelico
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