Trying to understand nested loops

Mladen Gogala mgogala at yahoo.com
Fri Aug 5 20:29:19 EDT 2022


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


More information about the Python-list mailing list