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 Geek8
Hello Geek Hello Geek Hello Geek9
if condition: # execute these statements else: # execute these statements0
if condition: # execute these statements else: # execute these statements1
if condition: # execute these statements else: # execute these statements2
if condition: # execute these statements else: # execute these statements3
if condition: # execute these statements else: # execute these statements4_______3_______5
if condition: # execute these statements else: # execute these statements6
if condition: # execute these statements else: # execute these statements7
if condition: # execute these statements else: # execute these statements0
if condition: # execute these statements else: # execute these statements1
if condition: # execute these statements else: # execute these statements0_______14_______1
while condition: # execute these statements else: # execute these statements2
if condition: # execute these statements else: # execute these statements7
while condition: # execute these statements else: # execute these statements4
while condition: # execute these statements else: # execute these statements5
while condition: # execute these statements else: # execute these statements6
while condition: # execute these statements else: # execute these statements7
đầ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 Geek8
while condition: # execute these statements else: # execute these statements9
if condition: # execute these statements else: # execute these statements0
if condition: # execute these statements else: # execute these statements1
if condition: # execute these statements else: # execute these statements2
if condition: # execute these statements else: # execute these statements3
if condition: # execute these statements else: # execute these statements4_______3_______5
if condition: # execute these statements else: # execute these statements6
if condition: # execute these statements else: # execute these statements7
if condition: # execute these statements else: # execute these statements0
if condition: # execute these statements else: # execute these statements1
if condition: # execute these statements else: # execute these statements0_______14_______1
while condition: # execute these statements else: # execute these statements2
if condition: # execute these statements else: # execute these statements7
while condition: # execute these statements else: # execute these statements4
while condition: # execute these statements else: # execute these statements5
while condition: # execute these statements else: # execute these statements6
while condition: # execute these statements else: # execute these statements7
for iterator_var in sequence: statements[s]8_______44_______9
if condition: # execute these statements else: # execute these statements7
while condition: # execute these statements else: # execute these statements4
while condition: # execute these statements else: # execute these statements5
0 1 2 33
while condition: # execute these statements else: # execute these statements7
đầ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 Geek8
0 1 2 36
if condition: # execute these statements else: # execute these statements0
if condition: # execute these statements else: # execute these statements1
if condition: # execute these statements else: # execute these statements2
if condition: # execute these statements else: # execute these statements3
List Iteration geeks for geeks Tuple Iteration geeks for geeks String Iteration G e e k s Dictionary Iteration xyz 123 abc 3451
if condition: # execute these statements else: # execute these statements1
if condition: # execute these statements else: # execute these statements1
if condition: # execute these statements else: # execute these statements2
if condition: # execute these statements else: # execute these statements6
if condition: # execute these statements else: # execute these statements7
while condition: # execute these statements else: # execute these statements4
while condition: # execute these statements else: # execute these statements5
while condition: # execute these statements else: # execute these statements6
while condition: # execute these statements else: # execute these statements7
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