Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python for Loop

Python for Loop

Python3 Loop Statements Python3 loop statements


for is the most commonly used loop statement in Python, used to traverse iterables (sequences, dictionaries, sets, etc.).

Python's for The loop is more concise and powerful than in languages such as C/C++; there is no need to manage a loop variable manually, as elements are traversed directly.

Word meaning: for means "for", used for iterative traversal.


Basic syntax and parameters

Syntax format


for variable in iterable:
    代码块

Syntax Description

else clause


Example

Example 1: traversing a list

Example

Traverse the list

fruits

=

[

"Apple"

,

"Banana"

,

"Orange"

]

for

fruit

in

fruits:

   

print

(

fruit

)

Traversal with indexes

for

i

,

fruit

in

enumerate

(

fruits

)

:

   

print

(

f

"{i}: {fruit}"

)

Expected output:

苹果
香蕉
橙子
0: 苹果
1: 香蕉
2: 橙子

Code analysis:

  1. The for loop traverses each element in the list directly.
  2. enumerate() provides both the index and the value.

Example 2: traversing strings and tuples

Example

Traverse a string

for

char

in

"Python"

:

   

print

(

char

,

end

=

" "

)

print

(

)

 

Newline

Traverse a tuple

point

=

(

10

,

20

)

for

value

in

point:

   

print

(

value

,

end

=

" "

)

print

(

)

Expected output:

P y t h o n
10 20

Strings and tuples are also iterable.

Example 3: traversing a dictionary

Example

Traverse a dictionary (keys are traversed by default)

person

=

{

"name"

:

"Tom"

,

"age"

:

20

,

"city"

:

"Beijing"

}

for

key

in

person:

   

print

(

key

)

print

(

"---"

)

Traverse key/value pairs

for

key

,

value

in

person.

items

(

)

:

   

print

(

f

"{key}: {value}"

)

print

(

"---"

)

Traverse values only

for

value

in

person.

values

(

)

:

   

print

(

value

)

Expected output:

name
age
city
---
name: Tom
age: 20
city: Beijing
---
Tom
20
Beijing

The items(), keys(), and values() methods of a dictionary can traverse different parts.

Example 4: for loop with else

Example

for-else structure

for

i

in

range

(

3

)

:

   

print

(

i

)

else

:

   

print

(

"The loop ended normally"

)

print

(

"---"

)

else is not executed when break is used

for

i

in

range

(

5

)

:

   

if

i

==

3

:

       

break

   

print

(

i

)

else

:

   

print

(

"This line will not be printed"

)

Expected output:

0
1
2
循环正常结束
---
0
1
2

In a for-else structure, else is not executed when the loop is terminated by break.

Example 5: nested loops

Example

Nested loops - printing the multiplication table

for

i

in

range

(

1

,

10

)

:

   

for

j

in

range

(

1

,

i +

1

)

:

       

print

(

f

"{j}{i}={ij}"

,

end

=

"\t"

)

   

print

(

)

Expected output:

1*1=1
1*2=2    2*2=4
1*3=3    2*3=6    3*3=9
...

Nested loops can handle multi-dimensional data structures or generate tables.


Python3 Loop Statements Python3 loop statements