OneCompiler

Loops

Loops are used to perform iterations on a block of code based on a criteria.

Python provides for and while loops to loop over iterable-objects

For

For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.

Syntax

for variable in arrays :
    #code

example

mylist=("Iphone","Pixel","Samsung")
for i in mylist:
    print(i)

While

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of ierations is not known in advance.

Syntax

while condition :  
    #code 

Example

i = 1
while(i<=10) :
  print (i)
  i += 1

check Result here

Note

Indentation is very important in Python, make sure the indentation is followed correctly