Python Program we get 100 values of a sine function in one line of code


Example

									
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
# calculate 100 values for x and y without a for loop
x = np.linspace(0, 2* np.pi, 100)
y = np.sin(x)
print(x)
print(y)


Output

[ 0.          0.06346652  0.12693304 ...,  6.15625227  6.21971879
  6.28318531]
[  0.00000000e+00   6.34239197e-02   1.26592454e-01 ...,  -1.26592454e-01
  -6.34239197e-02  -2.44929360e-16]


Explanation

For loops tend to get slow if there are many iterations to do. They are not necessary for calculations on numbers, if the Numpy module is used.