For Loops


When we wish to repeat code a certain number of times, we use a For Loop. For Loops are useful for sums, creating tables or seeing how a function changes through different values. A For Loop is created with the syntax For i in range (a, b): where i is any variable name (although i is a standard name) and a and b are integers where a < b. Note that the loop starts at a and ends at the value 1 less than b. We will use a For Loop here to print the first 4 derivatives of the function

\begin{equation} f(x) = x^6 + 3x^5 - 41x^4 - 87x^3 + 400x^2 + 444x - 720 \end{equation}

Sage Cell

Note that any code used in the loop is indented to signal it is part of the loop to Sage.


f(x) = x^6 + 3*x^5 - 41*x^4 - 87*x^3 + 400*x^2 + 444*x - 720
for i in range(1, 5):
    print(diff(f(x), x, i))




Primary Tags—Programming: Python

Secondary Tags—Python: Control flow statements

Related Cells




Date: 14 Mar 2019 20:21

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License