I'm trying so simulate coin tosses and profits and plot the graph in matplotlib:
from random import choice
import matplotlib.pyplot as plt
import time
start_time = time.time()
num_of_graphs = 2000
tries = 2000
coins = [150, -100]
last_loss = 0
for a in range(num_of_graphs):
    profit = 0
    line = []
    for i in range(tries):
        profit = profit + choice(coins)
        if (profit < 0 and last_loss < i):
            last_loss = i
        line.append(profit)
    plt.plot(line)
plt.show()
print("--- %s seconds ---" % (time.time() - start_time))
print("No losses after " + str(last_loss) + " iterations")
The end result is
--- 9.30498194695 seconds ---
No losses after 310 iterations
Why is it taking so long to run this script? If I change num_of_graphs to 10000, the scripts never finishes.
How would you optimize this?

lineis going to be would be to use numpy and pre-allocate your array.line = np.zeros((2000,))outside of either loop, followed byline[i] = profitinside the second loop. Allocate once and then keep rewriting.