81.5k views
1 vote
What does this code do?

for n in range(10, 0, -1):
print(str( n ) + " Mississippi")


What does this code do?

x = 0
while(x < 10):
print(x)
x = x + 1

1 Answer

3 votes

Answer:

The `for` part prints:

"10 Mississippi"

"9 Mississippi"

All the way down to

"0 Mississippi".

The `while` part prints a number a line starting from 0 all the way up to 9.

Step-by-step explanation:

`for n in range(10, 0, -1) means iterate over all the elements starting at 10, all the way down to 0 by steps of -1. Then using that `n` value, convert it to a string value, because by default it is an integer, and then use it to fill in the string "n Mississippi".

A `while` loop will continue iterating while the parenthesis-enclosed condition is true. That means, while x is smaller than 10 the code inside the while loop will execute. In this case, the variable x is declared and initialised at a value of 0. 0 is smaller than 10 so the variable is printed and then, x is given a new value which is the current value of x plus 1. Then, the while loop is executed again and again, until x equals 9. When that happens and 1 is added to its value, the condition is no longer true, because 10 is not smaller than 10, so the loop won't execute any further.

User Leri Gogsadze
by
7.8k points

No related questions found