CoCalc Public FilesDiscussion Due Feb 24.ipynbOpen with one click!
Author: Wadud Khan
Views : 132
Description: Discussion Due Feb 24
Compute Environment: Ubuntu 20.04 (Default)

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:

In [1]:
x = 0 sum_total = 0 while x <=5: sum_total = sum_total + x x = x+1

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

In [2]:
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

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