I'm trying to add value z
to starting value x
each time the following for loop runs. The output I'm expecting is 1000,1021,1042,1063...
or x, x z, x z z, x z z z...
When I run the following, I only get 1000,1021
as the output.
Why am I only getting a list of two values when the range is 0-1000? I'm obviously very new to python, so I'm sure the answer is something simple that I haven't come across yet.
Thanks for your time!
`
x = [1000]
y = []
z = 21
for i in range(0,1000):
y = np.append(x,sum(x,z))
print(y)
`
CodePudding user response:
The code in your query is independent of the loop variable i
, the loop is running for 1000 times but it does not do anything.
Before the loop exits, it adds two elements to y
i.e. x
and sum(x,z)
, which are 1000
and 1021
, hence you get the output [1000 1021]
You can try the below code to increment every time by z using range() function
for i in range(1000, 1100, z):
y.append(i)
print(y)
#Output: 1000, 1021, 1042, 1063, 1084