Python Language / Python loops

It is a sequence of instructions that is repeated until a certain condition is reached to complete a task.

while loops With the while loop we can execute a set of statements as long as a condition is true.
for loops for loop is used for iterating over a sequence (I.E a list, a tuple, a dictionary, a set, or a string).


Sample Programs
Print sum of 1 to 5 using while Loop Output

sum = 0
i = 1
while i <= 5:
sum = sum + i
i = i+1
print("The sum is", sum)

The sum is 15


For Loop in python
It iterate over a given sequence. (i.e is either a list / tuple / dictionary / set / string).

Example Output
primenos = [2, 3, 5, 7]
for primenos in primenos:
print(primenos)
2
3
5
7

numbers = ["even", "odd", "prime"]
for nos in numbers:
print(nos)

even
odd
prime


Home     Back