Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download

Discussion Due Feb 24

Views: 232
Image: ubuntu2004
Kernel: Python 3 (system-wide)

There are two types of common loops in Python

"While Loops" and "For Loops"

If you wish to repeat a process until a certain requirement is met then it is best to use a "While Loop"

Consider we wish to sum up the values 1-5:

x = 0 sum_total = 0 while x <=5: sum_total = sum_total + x x = x+1

This Code will repeat WHILE x<=5x<=5

print (sum_total)
15

15 is correctly produced by this code

What about using a FOR loop?

A "For Loop" is best when you know the number of iterations you want your code to run

sum_tot=0 for n in range (1,6): sum_tot= sum_tot +n print (sum_tot)
15