Việc sử dụng vòng lặp for trong python là gì?

Ngôn ngữ lập trình Python cung cấp các loại vòng lặp sau để xử lý các yêu cầu về vòng lặp. Python cung cấp ba cách để thực hiện các vòng lặp. Mặc dù tất cả các cách đều cung cấp chức năng cơ bản giống nhau, nhưng chúng khác nhau về cú pháp và thời gian kiểm tra điều kiện

Vòng lặp While trong Python

Trong python, vòng lặp while được sử dụng để thực thi lặp đi lặp lại một khối câu lệnh cho đến khi một điều kiện nhất định được thỏa mãn. Và khi điều kiện trở thành sai thì dòng ngay sau vòng lặp trong chương trình được thực hiện

cú pháp

while expression:
    statement(s)

Tất cả các câu lệnh được thụt vào bởi cùng một số khoảng cách ký tự sau cấu trúc lập trình được coi là một phần của một khối mã. Python sử dụng thụt đầu dòng làm phương pháp nhóm các câu lệnh.  
Thí dụ.  

con trăn




Hello Geek
Hello Geek
Hello Geek
8

Hello Geek
Hello Geek
Hello Geek
9

if condition:
    # execute these statements
else:
    # execute these statements
0
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
2

if condition:
    # execute these statements
else:
    # execute these statements
3
if condition:
    # execute these statements
else:
    # execute these statements
4_______3_______5
if condition:
    # execute these statements
else:
    # execute these statements
6

if condition:
    # execute these statements
else:
    # execute these statements
7
if condition:
    # execute these statements
else:
    # execute these statements
0
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
0_______14_______1
while condition:
     # execute these statements
else:
     # execute these statements
2

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
while condition:
     # execute these statements
else:
     # execute these statements
6
while condition:
     # execute these statements
else:
     # execute these statements
7

đầu ra.  

Hello Geek
Hello Geek
Hello Geek

Sử dụng câu lệnh else với vòng lặp while

Như đã thảo luận ở trên, vòng lặp while thực thi khối cho đến khi một điều kiện được thỏa mãn. Khi điều kiện trở thành sai, câu lệnh ngay sau vòng lặp được thực hiện.  

Mệnh đề khác chỉ được thực hiện khi điều kiện trong khi của bạn trở thành sai. Nếu bạn thoát ra khỏi vòng lặp hoặc nếu một ngoại lệ được đưa ra, nó sẽ không được thực thi.  

Nếu khác như thế này

if condition:
    # execute these statements
else:
    # execute these statements

và vòng lặp while như thế này cũng tương tự

while condition:
     # execute these statements
else:
     # execute these statements

ví dụ

con trăn




Hello Geek
Hello Geek
Hello Geek
8

while condition:
     # execute these statements
else:
     # execute these statements
9

if condition:
    # execute these statements
else:
    # execute these statements
0
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
2

if condition:
    # execute these statements
else:
    # execute these statements
3
if condition:
    # execute these statements
else:
    # execute these statements
4_______3_______5
if condition:
    # execute these statements
else:
    # execute these statements
6

if condition:
    # execute these statements
else:
    # execute these statements
7
if condition:
    # execute these statements
else:
    # execute these statements
0
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
0_______14_______1
while condition:
     # execute these statements
else:
     # execute these statements
2

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
while condition:
     # execute these statements
else:
     # execute these statements
6
while condition:
     # execute these statements
else:
     # execute these statements
7

for iterator_var in sequence:
    statements(s)
8_______44_______9

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
0
1
2
3
3
while condition:
     # execute these statements
else:
     # execute these statements
7

đầu ra.  

Hello Geek
Hello Geek
Hello Geek
In Else Block

Câu lệnh đơn trong khi chặn

Cũng giống như khối if, nếu khối while chỉ gồm một câu lệnh thì chúng ta có thể khai báo toàn bộ vòng lặp trong một dòng như hình bên dưới

con trăn




Hello Geek
Hello Geek
Hello Geek
8

0
1
2
3
6

if condition:
    # execute these statements
else:
    # execute these statements
0
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
2

if condition:
    # execute these statements
else:
    # execute these statements
3
List Iteration
geeks
for
geeks

Tuple Iteration
geeks
for
geeks

String Iteration
G
e
e
k
s

Dictionary Iteration
xyz  123
abc  345
1
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
2
if condition:
    # execute these statements
else:
    # execute these statements
6

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
while condition:
     # execute these statements
else:
     # execute these statements
6
while condition:
     # execute these statements
else:
     # execute these statements
7

Ghi chú. Không nên sử dụng loại vòng lặp này vì đây là vòng lặp vô hạn không bao giờ kết thúc trong đó điều kiện luôn đúng và bạn phải kết thúc trình biên dịch một cách mạnh mẽ

Vòng lặp trong Python

Đối với các vòng lặp được sử dụng để truyền tải tuần tự. Ví dụ. duyệt qua một danh sách hoặc chuỗi hoặc mảng, v.v. Trong Python, không có kiểu C cho vòng lặp, tôi. e. , cho (i=0; i

cú pháp

for iterator_var in sequence:
    statements(s)

Nó có thể được sử dụng để lặp qua một phạm vi và các trình lặp

Python3




Hello Geek
Hello Geek
Hello Geek
8

geeks
for
geeks
2

 

geeks
for
geeks
3_______3_______1
geeks
for
geeks
5

geeks
for
geeks
6
geeks
for
geeks
7
geeks
for
geeks
8
geeks
for
geeks
9
while condition:
     # execute these statements
else:
     # execute these statements
5
if condition:
    # execute these statements
else:
    # execute these statements
2
geeks
for
geeks
Inside Else Block
2

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
geeks
for
geeks
Inside Else Block
5

đầu ra

0
1
2
3

Ví dụ với phép lặp Danh sách, Tuple, chuỗi và từ điển bằng Vòng lặp For

con trăn




Hello Geek
Hello Geek
Hello Geek
8

geeks
for
geeks
Inside Else Block
7

while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
Hello Geek
Hello Geek
Hello Geek
00
while condition:
     # execute these statements
else:
     # execute these statements
7

Hello Geek
Hello Geek
Hello Geek
02_______3_______1
Hello Geek
Hello Geek
Hello Geek
04
Hello Geek
Hello Geek
Hello Geek
05
Hello Geek
Hello Geek
Hello Geek
06
Hello Geek
Hello Geek
Hello Geek
07
Hello Geek
Hello Geek
Hello Geek
06
Hello Geek
Hello Geek
Hello Geek
05
Hello Geek
Hello Geek
Hello Geek
10

geeks
for
geeks
6
geeks
for
geeks
7
geeks
for
geeks
8
Hello Geek
Hello Geek
Hello Geek
14

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
geeks
for
geeks
Inside Else Block
5

 

Hello Geek
Hello Geek
Hello Geek
18

while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
Hello Geek
Hello Geek
Hello Geek
21
while condition:
     # execute these statements
else:
     # execute these statements
7

Hello Geek
Hello Geek
Hello Geek
23
if condition:
    # execute these statements
else:
    # execute these statements
1
while condition:
     # execute these statements
else:
     # execute these statements
5
Hello Geek
Hello Geek
Hello Geek
05_______1_______06
Hello Geek
Hello Geek
Hello Geek
07
Hello Geek
Hello Geek
Hello Geek
06
Hello Geek
Hello Geek
Hello Geek
05
while condition:
     # execute these statements
else:
     # execute these statements
7

geeks
for
geeks
6
geeks
for
geeks
7
geeks
for
geeks
8
Hello Geek
Hello Geek
Hello Geek
35

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
geeks
for
geeks
Inside Else Block
5

 

Hello Geek
Hello Geek
Hello Geek
39

while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
Hello Geek
Hello Geek
Hello Geek
42
while condition:
     # execute these statements
else:
     # execute these statements
7

Hello Geek
Hello Geek
Hello Geek
44
if condition:
    # execute these statements
else:
    # execute these statements
1
Hello Geek
Hello Geek
Hello Geek
46

geeks
for
geeks
6
geeks
for
geeks
7
geeks
for
geeks
8
Hello Geek
Hello Geek
Hello Geek
50

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
geeks
for
geeks
Inside Else Block
5

 

Hello Geek
Hello Geek
Hello Geek
54

while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
Hello Geek
Hello Geek
Hello Geek
57
while condition:
     # execute these statements
else:
     # execute these statements
7

Hello Geek
Hello Geek
Hello Geek
59
if condition:
    # execute these statements
else:
    # execute these statements
1
Hello Geek
Hello Geek
Hello Geek
61
Hello Geek
Hello Geek
Hello Geek
62

Hello Geek
Hello Geek
Hello Geek
63
Hello Geek
Hello Geek
Hello Geek
64
Hello Geek
Hello Geek
Hello Geek
10
if condition:
    # execute these statements
else:
    # execute these statements
1
Hello Geek
Hello Geek
Hello Geek
67

Hello Geek
Hello Geek
Hello Geek
63
Hello Geek
Hello Geek
Hello Geek
69
Hello Geek
Hello Geek
Hello Geek
10
if condition:
    # execute these statements
else:
    # execute these statements
1
Hello Geek
Hello Geek
Hello Geek
72

geeks
for
geeks
6
geeks
for
geeks
7
geeks
for
geeks
8
Hello Geek
Hello Geek
Hello Geek
76

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
Hello Geek
Hello Geek
Hello Geek
80
Hello Geek
Hello Geek
Hello Geek
81
Hello Geek
Hello Geek
Hello Geek
82

 

Hello Geek
Hello Geek
Hello Geek
83

while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
Hello Geek
Hello Geek
Hello Geek
86
while condition:
     # execute these statements
else:
     # execute these statements
7

Hello Geek
Hello Geek
Hello Geek
88
if condition:
    # execute these statements
else:
    # execute these statements
1
Hello Geek
Hello Geek
Hello Geek
90
while condition:
     # execute these statements
else:
     # execute these statements
2
Hello Geek
Hello Geek
Hello Geek
06
Hello Geek
Hello Geek
Hello Geek
93
Hello Geek
Hello Geek
Hello Geek
06
if condition:
    # execute these statements
else:
    # execute these statements
5
Hello Geek
Hello Geek
Hello Geek
06
geeks
for
geeks
5
Hello Geek
Hello Geek
Hello Geek
06
Hello Geek
Hello Geek
Hello Geek
99
Hello Geek
Hello Geek
Hello Geek
06
if condition:
    # execute these statements
else:
    # execute these statements
01
if condition:
    # execute these statements
else:
    # execute these statements
02

geeks
for
geeks
6
geeks
for
geeks
7
geeks
for
geeks
8
if condition:
    # execute these statements
else:
    # execute these statements
06

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
if condition:
    # execute these statements
else:
    # execute these statements
09

đầu ra.  

List Iteration
geeks
for
geeks

Tuple Iteration
geeks
for
geeks

String Iteration
G
e
e
k
s

Dictionary Iteration
xyz  123
abc  345

Lặp lại theo chỉ mục của trình tự.  

Chúng ta cũng có thể sử dụng chỉ mục của các phần tử trong chuỗi để lặp lại. Ý tưởng chính là trước tiên tính toán độ dài của danh sách và lặp lại chuỗi trong phạm vi độ dài này.  
Xem ví dụ bên dưới.  

con trăn




Hello Geek
Hello Geek
Hello Geek
8

if condition:
    # execute these statements
else:
    # execute these statements
11

 

if condition:
    # execute these statements
else:
    # execute these statements
12
if condition:
    # execute these statements
else:
    # execute these statements
1
Hello Geek
Hello Geek
Hello Geek
04
Hello Geek
Hello Geek
Hello Geek
05_______1_______06
Hello Geek
Hello Geek
Hello Geek
07
Hello Geek
Hello Geek
Hello Geek
06
Hello Geek
Hello Geek
Hello Geek
05
Hello Geek
Hello Geek
Hello Geek
10

geeks
for
geeks
6
if condition:
    # execute these statements
else:
    # execute these statements
22
geeks
for
geeks
8
geeks
for
geeks
9
while condition:
     # execute these statements
else:
     # execute these statements
5
if condition:
    # execute these statements
else:
    # execute these statements
26
while condition:
     # execute these statements
else:
     # execute these statements
5
if condition:
    # execute these statements
else:
    # execute these statements
12
if condition:
    # execute these statements
else:
    # execute these statements
29

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
if condition:
    # execute these statements
else:
    # execute these statements
12
if condition:
    # execute these statements
else:
    # execute these statements
34

đầu ra.  

geeks
for
geeks

Sử dụng câu lệnh other với vòng lặp for

Chúng ta cũng có thể kết hợp câu lệnh other với vòng lặp for như trong vòng lặp while. Nhưng vì không có điều kiện nào trong vòng lặp for mà dựa vào đó quá trình thực thi sẽ kết thúc nên khối other sẽ được thực thi ngay sau khi khối for kết thúc thực thi.  
Ví dụ dưới đây giải thích cách thực hiện việc này.  

con trăn




Hello Geek
Hello Geek
Hello Geek
8

if condition:
    # execute these statements
else:
    # execute these statements
36

 

if condition:
    # execute these statements
else:
    # execute these statements
12
if condition:
    # execute these statements
else:
    # execute these statements
1
Hello Geek
Hello Geek
Hello Geek
04
Hello Geek
Hello Geek
Hello Geek
05_______1_______06
Hello Geek
Hello Geek
Hello Geek
07
Hello Geek
Hello Geek
Hello Geek
06
Hello Geek
Hello Geek
Hello Geek
05
Hello Geek
Hello Geek
Hello Geek
10

geeks
for
geeks
6
if condition:
    # execute these statements
else:
    # execute these statements
22
geeks
for
geeks
8
geeks
for
geeks
9
while condition:
     # execute these statements
else:
     # execute these statements
5
if condition:
    # execute these statements
else:
    # execute these statements
26
while condition:
     # execute these statements
else:
     # execute these statements
5
if condition:
    # execute these statements
else:
    # execute these statements
12
if condition:
    # execute these statements
else:
    # execute these statements
29

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
if condition:
    # execute these statements
else:
    # execute these statements
12
if condition:
    # execute these statements
else:
    # execute these statements
34

for iterator_var in sequence:
    statements(s)
8_______44_______9

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
if condition:
    # execute these statements
else:
    # execute these statements
65
while condition:
     # execute these statements
else:
     # execute these statements
7

đầu ra.  

geeks
for
geeks
Inside Else Block

Vòng lồng nhau

Ngôn ngữ lập trình Python cho phép sử dụng một vòng lặp bên trong một vòng lặp khác. Phần sau đây cho thấy một vài ví dụ để minh họa khái niệm.  

Hello Geek
Hello Geek
Hello Geek
0

Cú pháp của câu lệnh vòng lặp while lồng nhau trong ngôn ngữ lập trình Python như sau.  

Hello Geek
Hello Geek
Hello Geek
1

Lưu ý cuối cùng về cách lồng vòng lặp là chúng ta có thể đặt bất kỳ loại vòng lặp nào bên trong bất kỳ loại vòng lặp nào khác. Ví dụ: vòng lặp for có thể nằm trong vòng lặp while hoặc ngược lại

con trăn




Hello Geek
Hello Geek
Hello Geek
8

if condition:
    # execute these statements
else:
    # execute these statements
68

if condition:
    # execute these statements
else:
    # execute these statements
69
if condition:
    # execute these statements
else:
    # execute these statements
70
if condition:
    # execute these statements
else:
    # execute these statements
71
if condition:
    # execute these statements
else:
    # execute these statements
72

geeks
for
geeks
6
geeks
for
geeks
7
geeks
for
geeks
8
geeks
for
geeks
9
while condition:
     # execute these statements
else:
     # execute these statements
5
while condition:
     # execute these statements
else:
     # execute these statements
2
Hello Geek
Hello Geek
Hello Geek
06
Hello Geek
Hello Geek
Hello Geek
99
if condition:
    # execute these statements
else:
    # execute these statements
6

if condition:
    # execute these statements
else:
    # execute these statements
7
geeks
for
geeks
6
if condition:
    # execute these statements
else:
    # execute these statements
84_______70_______8
geeks
for
geeks
9
if condition:
    # execute these statements
else:
    # execute these statements
87

if condition:
    # execute these statements
else:
    # execute these statements
88
while condition:
     # execute these statements
else:
     # execute these statements
4
if condition:
    # execute these statements
else:
    # execute these statements
90
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
92
while condition:
     # execute these statements
else:
     # execute these statements
7

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
Hello Geek
Hello Geek
Hello Geek
62

đầu ra.  

Hello Geek
Hello Geek
Hello Geek
2

Câu lệnh điều khiển vòng lặp

Các câu lệnh điều khiển vòng lặp thay đổi quá trình thực thi so với trình tự bình thường của chúng. Khi thực thi rời khỏi một phạm vi, tất cả các đối tượng tự động được tạo trong phạm vi đó sẽ bị hủy. Python hỗ trợ các câu lệnh điều khiển sau.  

Tiếp tục tuyên bố

Nó trả điều khiển về đầu vòng lặp

con trăn




if condition:
    # execute these statements
else:
    # execute these statements
97

geeks
for
geeks
6
if condition:
    # execute these statements
else:
    # execute these statements
99
geeks
for
geeks
8
while condition:
     # execute these statements
else:
     # execute these statements
01
for iterator_var in sequence:
    statements(s)
9

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
04
if condition:
    # execute these statements
else:
    # execute these statements
99
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
1
while condition:
     # execute these statements
else:
     # execute these statements
08
while condition:
     # execute these statements
else:
     # execute these statements
09
if condition:
    # execute these statements
else:
    # execute these statements
99
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
1
while condition:
     # execute these statements
else:
     # execute these statements
13
for iterator_var in sequence:
    statements(s)
9

if condition:
    # execute these statements
else:
    # execute these statements
88
while condition:
     # execute these statements
else:
     # execute these statements
16

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
while condition:
     # execute these statements
else:
     # execute these statements
20
while condition:
     # execute these statements
else:
     # execute these statements
21

đầu ra.  

Hello Geek
Hello Geek
Hello Geek
3

Tuyên bố phá vỡ.  

Nó mang lại sự kiểm soát ra khỏi vòng lặp

con trăn




geeks
for
geeks
6
if condition:
    # execute these statements
else:
    # execute these statements
99
geeks
for
geeks
8
while condition:
     # execute these statements
else:
     # execute these statements
01
for iterator_var in sequence:
    statements(s)
9

 

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
28

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
30

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
04
if condition:
    # execute these statements
else:
    # execute these statements
99
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
1
while condition:
     # execute these statements
else:
     # execute these statements
08
while condition:
     # execute these statements
else:
     # execute these statements
09
if condition:
    # execute these statements
else:
    # execute these statements
99
if condition:
    # execute these statements
else:
    # execute these statements
1
if condition:
    # execute these statements
else:
    # execute these statements
1
while condition:
     # execute these statements
else:
     # execute these statements
13
for iterator_var in sequence:
    statements(s)
9

if condition:
    # execute these statements
else:
    # execute these statements
88
while condition:
     # execute these statements
else:
     # execute these statements
44

 

while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
while condition:
     # execute these statements
else:
     # execute these statements
20
while condition:
     # execute these statements
else:
     # execute these statements
21

đầu ra.  

Hello Geek
Hello Geek
Hello Geek
4

Tuyên bố vượt qua.  

Chúng tôi sử dụng câu lệnh vượt qua để viết các vòng lặp trống. Pass cũng được sử dụng cho các câu lệnh, hàm và lớp điều khiển trống

con trăn




while condition:
     # execute these statements
else:
     # execute these statements
49

geeks
for
geeks
6
if condition:
    # execute these statements
else:
    # execute these statements
99
geeks
for
geeks
8
while condition:
     # execute these statements
else:
     # execute these statements
01
for iterator_var in sequence:
    statements(s)
9

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
56

while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
5
while condition:
     # execute these statements
else:
     # execute these statements
59
while condition:
     # execute these statements
else:
     # execute these statements
21

đầu ra.  

Hello Geek
Hello Geek
Hello Geek
5

Làm thế nào để vòng lặp trong Python hoạt động nội bộ?

Trước khi tiếp tục phần này, bạn nên có hiểu biết trước về Python Iterators

Đầu tiên, hãy xem vòng lặp for đơn giản trông như thế nào

Python3




while condition:
     # execute these statements
else:
     # execute these statements
61

 

while condition:
     # execute these statements
else:
     # execute these statements
62_______3_______1
Hello Geek
Hello Geek
Hello Geek
04
while condition:
     # execute these statements
else:
     # execute these statements
65
Hello Geek
Hello Geek
Hello Geek
06
while condition:
     # execute these statements
else:
     # execute these statements
67
Hello Geek
Hello Geek
Hello Geek
06
while condition:
     # execute these statements
else:
     # execute these statements
69
Hello Geek
Hello Geek
Hello Geek
10

 

geeks
for
geeks
6
while condition:
     # execute these statements
else:
     # execute these statements
72
geeks
for
geeks
8
while condition:
     # execute these statements
else:
     # execute these statements
74

 

if condition:
    # execute these statements
else:
    # execute these statements
7
while condition:
     # execute these statements
else:
     # execute these statements
4
while condition:
     # execute these statements
else:
     # execute these statements
77

Đầu ra

Hello Geek
Hello Geek
Hello Geek
6

Ở đây chúng ta có thể thấy các vòng for lặp qua đối tượng iterable fruit là một danh sách. Danh sách, bộ, từ điển là một số đối tượng có thể lặp lại trong khi đối tượng số nguyên không phải là đối tượng có thể lặp lại

Công dụng của vòng lặp for là gì?

A cho. of loop hoạt động trên các giá trị được lấy từ một lần lặp từng cái một theo thứ tự tuần tự . Mỗi hoạt động của vòng lặp trên một giá trị được gọi là một lần lặp và vòng lặp được gọi là lặp qua lần lặp. Mỗi lần lặp thực hiện các câu lệnh có thể tham chiếu đến giá trị chuỗi hiện tại.

Tại sao chúng ta sử dụng các câu lệnh lặp trong Python?

Các vòng lặp rất quan trọng trong Python hoặc trong bất kỳ ngôn ngữ lập trình nào khác vì chúng giúp bạn thực thi lặp đi lặp lại một khối mã . Bạn sẽ thường xuyên phải đối mặt với những tình huống mà bạn cần phải sử dụng đi sử dụng lại một đoạn mã nhưng bạn không muốn viết cùng một dòng mã nhiều lần.

Tại sao chúng ta sử dụng vòng lặp for?

Vòng lặp for thường được sử dụng khi biết trước số lần lặp trước khi vào vòng lặp. Các vòng lặp for có thể được coi là cách viết tắt của các vòng lặp while tăng dần và kiểm tra một biến vòng lặp

Vòng lặp Python for hoạt động như thế nào?

Một vòng lặp Python for lặp qua một đối tượng cho đến khi đối tượng đó hoàn thành . Chẳng hạn, bạn có thể lặp lại nội dung của một danh sách hoặc một chuỗi. Vòng lặp for sử dụng cú pháp. đối với mục trong đối tượng, trong đó “đối tượng” là mục có thể lặp lại mà bạn muốn lặp lại. Vòng lặp cho phép bạn lặp lại các thao tác tương tự trong mã của mình.