user7796883
user7796883

Reputation:

How to create an array using for loop while by keeping range fixed in python

newarray = [x + 0.5 for x in range(1, 10)]

this code will give me following result:

newarray
[1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]

Instead of adding 0.5 with x I want to increase my x by 0.5 for each 1 increment of x. The output suppose to be

newarray=[0.5,1,1.5,2,2.5......5.5].

Keep in mind that my range must be fix in 1 to 10. What can be better approach to make that?

Upvotes: 0

Views: 47

Answers (1)

Andrew Che
Andrew Che

Reputation: 968

[0.5 * x for x in range(1, 12)]

Will do the thing, I'm afraid generating that array with range(1, 10) is impossible

Upvotes: 1

Related Questions