FOR LOOP IN PYTHON

GOCOURSE


For Loop

When a for loop can type the number of times it wants to loop through a block of code, instead of a while loop, this type of loop is used.

Syntax:

for x in condition:
    print(x)

Example 1:

1.Print each Mobile in a Mobile list

Program:


   mobile=["vivo","oppo","nokia","poco"]
   for x in mobile:
     print(x)



Output:


   vivo
   oppo
   nokia
   poco


Example 2:

2. For loop through a sting 

Program:


   for x in "Gocourse":
     print(x)


Output:


   G
   o
   c
   o
   u
   r
   s
   e


Example 3:

A number sequence is returned by the range() and range() functions. The value at the end is enclosed in range brackets and starts at 0 and is increased by 

Program:


   for x in range(10):
     print(x)


Output:


   0
   1
   2
   3
   4
   5
   6
   7
   8
   9





More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send