-3

Given a list x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10] use a for loop to create a new list, y, that contains the value aSin(10a) for each value, a, in list x. Plot the results using plot(x,y).

I have...

x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]

for a in x:

print sin(a)*(10*a)

The code returns the correct sin values but I'm not sure how to get the values into a new list y..

Any help would be greatly appreciated.

Dan
  • 3
  • 1
  • 3
    you mentioned in the text that you want a*sin(10a), but you have different thing in the code – rawwar May 18 '18 at 14:22

5 Answers5

5

Use list comprehensions here

>>> y = [sin(10*i)*(i) for i in x]
rawwar
  • 4,834
  • 9
  • 32
  • 57
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
2

try this

 x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
 y=list()
 for a in x:
     y.append(sin(a)*(10*a))
ashish pal
  • 467
  • 4
  • 10
1

the following code works

x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y = [i*sin(10*i) for i in x]
rawwar
  • 4,834
  • 9
  • 32
  • 57
1
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y=[]
for a in x:
    y.append(sin(a)*(10*a))

Should work but do try to learn python!!

Paula Thomas
  • 1,152
  • 1
  • 8
  • 13
0

Try list comprehension:

y = [a * sin(10*a) for a in x]

Hope that helps.

ohduran
  • 787
  • 9
  • 15