Câu lệnh lặp trong ví dụ 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

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 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____35
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
Hello Geek
Hello Geek
Hello Geek
41
Hello Geek
Hello Geek
Hello Geek
42

_______144____145____146

Hello Geek
Hello Geek
Hello Geek
47

đầ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 Geek
4

ví dụ

con trăn




Hello Geek
Hello Geek
Hello Geek
8

Hello Geek
Hello Geek
Hello Geek
49

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____35
if condition:
    # execute these statements
else:
    # execute these statements
26

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
Hello Geek
Hello Geek
Hello Geek
41
Hello Geek
Hello Geek
Hello Geek
42

_______144____145____146

Hello Geek
Hello Geek
Hello Geek
47

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

_______37____144____145____493

Hello Geek
Hello Geek
Hello Geek
47

đầu ra.  

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

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

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

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
81
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
26
Hello Geek
Hello Geek
Hello Geek
44
Hello Geek
Hello Geek
Hello Geek
45
Hello Geek
Hello Geek
Hello Geek
46
Hello Geek
Hello Geek
Hello Geek
47

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

Chủ Đề