Operators + = and / = with while in Python

1

I am starting to study Python through an online course. Although I have worked in R for a couple of years, and in general, I think the programming has many similar bases, there are notable differences. One of the most obvious ones I have noticed is the use, in Python , of operators such as: + =, * = and / =. The following code, which I took from an exercise of the course that I comment has a couple of those operators:

n=100
number_of_times = 0
while n >= 1:
    n /= 2
    number_of_times += 1
print(number_of_times)

When one runs the code, the result in the console is 7. The point is that the exercise only asked to run the code and write down the result in the answer sheet, which does not have much complication.

However, I would like to know how to "reason" the program so that it gives 7, that is, how the code could be dissected step by step to arrive at that answer considering that a "while" is used.

I would like someone to explain to me or give me some reference about it.

    
asked by Alejandro Carrera 23.08.2017 в 20:49
source

3 answers

3

If you notice, you are telling him to make a while loop while n >= 1 , that is, when he goes below 1 he will end and print number_of_times .

For each loop loop, you make n /= 2 , which is the same as n = n / 2 , that is, you divide n by 2 and you reassign the new value, therefore, the first round n will be 50, the second 25, etc, etc. It turns out that on lap 7 it drops below 1, so the loop ends.

If you do the following:

n=100
number_of_times = 0
while n >= 1:
    n /= 2
    print("n = " + str(n))
    number_of_times += 1
print("number_of_times = " + str(number_of_times))

You'll see that it prints:

n = 50
n = 25
n = 12
n = 6
n = 3
n = 1
n = 0
number_of_times = 7

I hope I have clarified;)

    
answered by 23.08.2017 / 20:58
source
1

If we could translate the question into a pseudocode, it would look something like this:

n empieza en 100
veces empieza en 0
mientras n sea mayor o igual que 1:
    n ahora vale la mitad de n
    sumar 1 al valor de veces
imprimir el valor de veces

In each repetition of the cycle n is changing, in fact, always reducing by half. The number of times you can do this is 7, going through these numbers 50,25,12,6,3,1,0.

The only case that I think you could cause confusion is, why the zero? Well, it is because of the specific condition that the process must be repeated until n no longer matches the condition that is equal to or greater than 1. In this case, when n = 1 , it still meets the condition of greater or equal to 1 and repeat the cycle for the last time, increasing for the last time the variable number_of_times .

    
answered by 23.08.2017 в 21:06
1

for starting the code is not indented:

n=100
number_of_times = 0
while n >= 1:
    n /= 2
    number_of_times += 1
print(number_of_times)

the while begins with n = 100

n /= 2          # n queda a 50
number_of_times += 1 # number_of_times en 1

with n = 50

n /= 2          # n queda a 25
number_of_times += 1 # number_of_times en 2

with n = 25

n /= 2          # n queda a 12
number_of_times += 1 # number_of_times en 3

with n = 12

n /= 2          # n queda a 6
number_of_times += 1 # number_of_times en 4

with n = 6

n /= 2          # n queda a 3
number_of_times += 1 # number_of_times en 5

with n = 3

n /= 2          # n queda a 1
number_of_times += 1 # number_of_times en 6

with n = 1

n /= 2          # n queda a 0
number_of_times += 1 # number_of_times en 7
    
answered by 23.08.2017 в 21:08