I am new to python & understanding the coding. I tried two sets of code, but for the below second line, I couldn't execute. can you plz explain why?
distance = np.array([0.723,1.0,1.524,5.203])
distance = [0.723,1.0,1.524,5.203]
flux = 1370 / (distance**2)
When I execute the second part (Line2) instead of first-line I get an error, Plz help me to understand:
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
CodePudding user response:
If you work in form of list
you have to iterate via for
or while
loop
One way is:-
distance = [0.723,1.0,1.524,5.203]
for i in distance:
flux = 1370 / (i**2)
print(flux)
Output:-
2620.8609049813576
1370.0
589.8622908356928
50.607270624669916
CodePudding user response:
You have iterate through distance
distance = [0.723,1.0,1.524,5.203]
new_distance = []
for d in distance:
new_distance = 1370 / (d**2)
print(new_distance)
CodePudding user response:
You're getting the error because the second instantiated variable distance
distance = [0.723,1.0,1.524,5.203]
is a python list which doesn't support the power operator **
.
So, you should just stick to numpy array.
distance = np.array([0.723,1.0,1.524,5.203])
flux = 1370 / (distance**2)
Which gives result array([2620.86090498, 1370., 589.86229084, 50.60727062])
You can get python list result if you want with:
>>> flux.tolist()
[2620.8609049813576, 1370.0, 589.8622908356928, 50.607270624669916]