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.
Ví 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____35
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
Hello Geek Hello Geek Hello Geek41
Hello Geek Hello Geek Hello Geek42
_______144____145____146
Hello Geek Hello Geek Hello Geek47
đầ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ự
Hello Geek Hello Geek Hello Geek4
ví dụ
con trăn
Hello Geek Hello Geek Hello Geek8
Hello Geek Hello Geek Hello Geek49
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____35
if condition: # execute these statements else: # execute these statements26
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
Hello Geek Hello Geek Hello Geek41
Hello Geek Hello Geek Hello Geek42
_______144____145____146
Hello Geek Hello Geek Hello Geek47
if condition: # execute these statements else: # execute these statements48____449
_______37____144____145____493
Hello Geek Hello Geek Hello Geek47
đầu ra.
if condition: # execute these statements else: # execute these statements2
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
if condition: # execute these statements else: # execute these statements96
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 statements81
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 statements26
Hello Geek Hello Geek Hello Geek44
Hello Geek Hello Geek Hello Geek45
Hello Geek Hello Geek Hello Geek46
Hello Geek Hello Geek Hello Geek47
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