-1

I am new to Python and found a function that checks to see if an array of arguments can be made equal by only multiplying the number by 2. However, there is some notation that I do not understand.

Function:

def isEqual(a,n): # a is an arrary, n is the length of array
for i in range(0,n):
    while a[i]%2==0: 
        a[i]//=2 # this is the part I do not understand
    print(a[i])
    if a[i] != a[0]:  
        return print("False")

# Otherwise, all elements equal, return true    
return print("True")

When I step through the function I see that it replaces the a[i] number by a[i]//2, but I do not understand why you would write // equals to number

I understand the // is "floor" division, but not why someone would write a[i]//=2. I would have thought to write it as a[i]=a[i]//2. I can only assume these are the same things, I just never saw it written this way.

Test code:

a = [50, 4, 2]
n = len(a)  

isEqual(a, n)
Jack Armstrong
  • 1,182
  • 4
  • 26
  • 59

1 Answers1

0

You might have came across operations that also assign value. Think

a += 1    # same as: a = a + 1

This is exactly the same. It integer divides and assigns the value. Probably better understood with proper spacing:

a //= 2    # same as: a = a // 2
tituszban
  • 4,797
  • 2
  • 19
  • 30