for i in range(2, 5):
for j in range(4, 9):
print(i, j)
Trace the code above in the Python Visualizer. How many times is p r i n t ( i , j ) executed?
1)
2
4
2)
3
3)
1
5
4)
5
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
I don't know python but I know java ...
Tell me why is it not 24 ... I mean i takes values from 2 to 5 and j from 4 to 9
we actually get 4*6
In Python the
range
function works as follows.
range (x, y)
, for integral x, y and
y
≥
x
considers all the integral values from
x
to
y
−
1
, inclusive. So:
range (2, 5)
considers
2
,
3
,
4
and
range (4, 9)
considers
4
,
5
,
6
,
7
,
8
. Thus, it considers
y
−
x
values, not
y
−
x
+
1
values.
"for i in range(2, 5)" means i ∈ [ 2 , 4 ] and it is an integer. Therefore, there are 4 − 2 + 1 = 3 possibilities for i . "for j in range(4, 9)" means j ∈ [ 4 , 8 ] and it is an integer. Thus, there are 8 − 4 + 1 = 5 possibilities for j . Therefore, p r i n t ( 1 , j ) is executed 3 ⋅ 5 = 3 ) 1 5
This is an example of nested loops where i is counter of outer loop and j is counter of inner loop. The outer loop runs 3 times with i=2,3,4 and corresponding to each execution of outer loop, the inner loop gets executed 5 times with j=4,5,6,7,8. print(i,j) is within the inner loop. So, total no. of times print(i,j) gets executed = ( 3 × 5 ) = 1 5
Answer: 3) 15.
For 'i' in range (2,5) means that the loop will run if 'i' is 2, 3, or 4. Hence, 3 times. Now, the second loop will run 5 times as the value of 'j' should be in the range of (4,9) that is, 4, 5, 6, 7 or 8.
Now the nested loop [for j in range(4,9)] will run 5 times in each of the three instances of the first loop [for i in range(2,5)]. Ergo, resulting in total 5*3 = 15 executions of print(i,j)
r a n g e ( 2 , 5 ) iterates 3 values: 2, 3, and 4
r a n g e ( 4 , 9 ) iterates 5 values: 4, 5, 6, 7, and 8
Therefore there are 3 × 5 = 1 5 combinations of i and j
for i it takes 3 num and for j it takes 5 num so 5*3=15
I have no idea about python language . But when i tried to compile the code given on the problem statement , following warning showed up : 'IndentationError: expected an indented block (<string>, line 2) ' . I didn't hear anything like 'IndentationError:' in c or cpp language .
c=0 for i in range(2, 5): for j in range(4, 9): c=c+1 print c
in the above python code the value of c gives the answer..
Its So Simple Actually It should be told that the python includes the range values or excludes it, here situaltion is bit differnet a range in python works like this as (4,9) means 4 is inclusive while 9 exclusive :) so it will iterate 4,5,6,7,8
in range(a,b) :iteration takes place from a(included) to b(not included)..
Python will read set formed by ( 2 , 3 , 4 ) × ( 4 , 5 , 6 , 7 , 8 )
The answer 3 × 5 = 1 5
Write the script for a python compiler and declare a variable 'counter' at first. write 'counter = counter + 1' below 'print (i, j)' and write 'print counter' in the script's end.
This is actually more of a counting problem. Once inside the first for- loop, we see that the program will iterate over 3 objects (due to syntax: 2 , 3 , 4 ). For each of the objects we go on to the next step and see that the for- loop at this level is iterated over 5 times ( 4 , 5 , 6 , 7 , 8 ) executing the print statement for each object. Thus the print statement will be executed 3 ∗ 5 = 1 5 times.
Problem Loading...
Note Loading...
Set Loading...
The answer is 0 because the indentation is faulty. But assuming the correct indentation, we note that we encounter 3 values of i and 5 values of j , so the answer is 3 ⋅ 5 = 1 5 .