Các cách khác nhau để viết vòng lặp for trong Python là gì?

Vòng lặp for là một công cụ luồng điều khiển rất cơ bản của hầu hết các ngôn ngữ lập trình. Ví dụ, một vòng lặp for đơn giản trong C có dạng như sau

int i;
for (i=0;i{
//do something
}

Không có cách nào khác để viết một vòng lặp for một cách tao nhã hơn trong C. Đối với một kịch bản phức tạp, chúng ta thường cần phải viết xấu…

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ã duy nhất. 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_______4_______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_______4_______5
if condition:
    # execute these statements
else:
    # execute these statements
6

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

if condition:
    # execute these statements
else:
    # execute these statements
7_______15_______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_______4_______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_______4_______5
if condition:
    # execute these statements
else:
    # execute these statements
6

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

if condition:
    # execute these statements
else:
    # execute these statements
7_______15_______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
for iterator_var in sequence:
    statements(s)
9

if condition:
    # execute these statements
else:
    # execute these statements
7_______15_______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_______4_______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_______15_______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____4_______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_______15_______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_______4_______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_______15_______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
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
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_______15_______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_______15_______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_______15_______4
while condition:
     # execute these statements
else:
     # execute these statements
5_______2_______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_______2_______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_______15_______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_______2_______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_______2_______10

geeks
for
geeks
6
if condition:
    # execute these statements
else:
    # execute these statements
22
geeks
for
geeks
8
geeks
for
geeks
9_______15_______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_______15_______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 lệnh other sẽ được thực thi ngay sau khi khối lệnh for kết thúc quá trình 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_______2_______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_______2_______10

geeks
for
geeks
6
if condition:
    # execute these statements
else:
    # execute these statements
22
geeks
for
geeks
8
geeks
for
geeks
9_______15_______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_______15_______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
for iterator_var in sequence:
    statements(s)
9

if condition:
    # execute these statements
else:
    # execute these statements
7_______15_______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_______4_______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_______71_______6
if condition:
    # execute these statements
else:
    # execute these statements
84_______71_______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_______15_______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_______15_______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_______15_______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_______15_______28

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

if condition:
    # execute these statements
else:
    # execute these statements
7_______15_______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_______15_______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
if condition:
    # execute these statements
else:
    # execute these statements
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_______15_______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ó bao nhiêu cách chúng ta có thể viết vòng lặp for trong Python?

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.

Cách chính xác để viết vòng lặp for trong Python là gì?

Để lặp qua một bộ mã với số lần được chỉ định, chúng ta có thể sử dụng hàm phạm vi (), Hàm phạm vi () trả về một chuỗi số, bắt đầu từ 0 theo mặc định và tăng thêm 1 (theo mặc định) và kết thúc

3 phần của vòng lặp for trong Python là gì?

Tương tự như vòng lặp While, vòng lặp For bao gồm ba phần. từ khóa For bắt đầu vòng lặp, điều kiện đang được kiểm tra và từ khóa EndFor kết thúc vòng lặp .

3 điều trong vòng lặp for là gì?

Câu lệnh for bao gồm ba phần cần thiết cho các vòng lặp. khởi tạo, kiểm tra và cập nhật . bắt đầu vòng lặp. Cả ba câu lệnh vòng lặp (while, do và for) đều tương đương về mặt chức năng.