Hari Sam
Hari Sam

Reputation: 11

assign operator(=) in python

I am trying to assign a assign values to the something-array using the assign operator(=) in for loop.

Below is the one with something-array initially having numbers going from 0 to 15 in its elements: [something array with its elements as numbers going from 0 to 15] (https://i.sstatic.net/a9cOg.jpg)

Below is the one with something-array initially having all its elements as zero: something array with all its elements as zeros

Why do we have different results?

I see that having different values previously as the elements of the "something-array" gives us different result from the ones that started from having zeros as its all elements previously.

I wanna know what is happening here and especially in the first case I wanna know how the calculations are done?

Upvotes: 1

Views: 34

Answers (1)

Prachi Patel
Prachi Patel

Reputation: 350

In numpy, zeros() method initialize the array with 0 in float whereas when you use arange() it puts the value in int datatype.

something = np.zeros(2)
for i in something:
    print(type(i))

print()

something = np.arange(2)
for i in something:
    print(type(i))

Output:

output

As you see I have printed the types of elements in the lists in both the cases, so when any other operation is performed on them it is performed according to the datatypes so the result can differ in both the cases.

I hope this explains the confusion.

Upvotes: 1

Related Questions