Python For Loop

 

for

statement iterates over each item of a sequence.

Examples of sequences:

  • Sequence of Characters (string)
  • Sequence of numbers, etc.

For Syntax

Code

word = "Python"
for each_char in word:
print(each_char)
PYTHON

Output

P
y
t
h
o
n

Range

Generates a sequence of integers starting from 0. Syntax:

range(n)
Stops before n (n is not included).

Code

for number in range(3):
print(number)
PYTHON

Output

0
1
2

Range with Start and End

Generates a sequence of numbers starting from

start

Syntax:
range(start, end)
Stops before
end
(end is not included).

Code

for number in range(5, 8):
print(number)
PYTHON

Output

5
6
7

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form