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 Show
Vòng lặp While trong PythonTrong 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. con trănHello 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 whileNhư đã 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ănHello 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ặnCũ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ănHello 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 cú pháp Nó có thể được sử dụng để lặp qua một phạm vi và các trình lặp Python3Hello Geek Hello Geek Hello Geek8 geeks for geeks2
geeks for geeks3_______3_______1 geeks for geeks5 geeks for geeks6 geeks for geeks7 geeks for geeks8 geeks for geeks9 while condition: # execute these statements else: # execute these statements5 if condition: # execute these statements else: # execute these statements2 geeks for geeks Inside Else Block2 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements4 geeks for geeks Inside Else Block5 đầ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 Forcon trănHello Geek Hello Geek Hello Geek8 geeks for geeks Inside Else Block7 while condition: # execute these statements else: # execute these statements4 while condition: # execute these statements else: # execute these statements5 Hello Geek Hello Geek Hello Geek00 while condition: # execute these statements else: # execute these statements7 Hello Geek Hello Geek Hello Geek02_______3_______1 Hello Geek Hello Geek Hello Geek04 Hello Geek Hello Geek Hello Geek05 Hello Geek Hello Geek Hello Geek06 Hello Geek Hello Geek Hello Geek07 Hello Geek Hello Geek Hello Geek06 Hello Geek Hello Geek Hello Geek05 Hello Geek Hello Geek Hello Geek10 geeks for geeks6 geeks for geeks7 geeks for geeks8 Hello Geek Hello Geek Hello Geek14 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements4 geeks for geeks Inside Else Block5
Hello Geek Hello Geek Hello Geek18 while condition: # execute these statements else: # execute these statements4 while condition: # execute these statements else: # execute these statements5 Hello Geek Hello Geek Hello Geek21 while condition: # execute these statements else: # execute these statements7 Hello Geek Hello Geek Hello Geek23 if condition: # execute these statements else: # execute these statements1 while condition: # execute these statements else: # execute these statements5 Hello Geek Hello Geek Hello Geek05_______1_______06 Hello Geek Hello Geek Hello Geek07 Hello Geek Hello Geek Hello Geek06 Hello Geek Hello Geek Hello Geek05 while condition: # execute these statements else: # execute these statements7 geeks for geeks6 geeks for geeks7 geeks for geeks8 Hello Geek Hello Geek Hello Geek35 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements4 geeks for geeks Inside Else Block5
Hello Geek Hello Geek Hello Geek39 while condition: # execute these statements else: # execute these statements4 while condition: # execute these statements else: # execute these statements5 Hello Geek Hello Geek Hello Geek42 while condition: # execute these statements else: # execute these statements7 Hello Geek Hello Geek Hello Geek44 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek46 geeks for geeks6 geeks for geeks7 geeks for geeks8 Hello Geek Hello Geek Hello Geek50 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements4 geeks for geeks Inside Else Block5
Hello Geek Hello Geek Hello Geek54 while condition: # execute these statements else: # execute these statements4 while condition: # execute these statements else: # execute these statements5 Hello Geek Hello Geek Hello Geek57 while condition: # execute these statements else: # execute these statements7 Hello Geek Hello Geek Hello Geek59 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek61 Hello Geek Hello Geek Hello Geek62 Hello Geek Hello Geek Hello Geek63 Hello Geek Hello Geek Hello Geek64 Hello Geek Hello Geek Hello Geek10 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek67 Hello Geek Hello Geek Hello Geek63 Hello Geek Hello Geek Hello Geek69 Hello Geek Hello Geek Hello Geek10 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek72 geeks for geeks6 geeks for geeks7 geeks for geeks8 Hello Geek Hello Geek Hello Geek76 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 Hello Geek Hello Geek Hello Geek80 Hello Geek Hello Geek Hello Geek81 Hello Geek Hello Geek Hello Geek82
Hello Geek Hello Geek Hello Geek83 while condition: # execute these statements else: # execute these statements4 while condition: # execute these statements else: # execute these statements5 Hello Geek Hello Geek Hello Geek86 while condition: # execute these statements else: # execute these statements7 Hello Geek Hello Geek Hello Geek88 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek90 while condition: # execute these statements else: # execute these statements2 Hello Geek Hello Geek Hello Geek06 Hello Geek Hello Geek Hello Geek93 Hello Geek Hello Geek Hello Geek06 if condition: # execute these statements else: # execute these statements5 Hello Geek Hello Geek Hello Geek06 geeks for geeks5 Hello Geek Hello Geek Hello Geek06 Hello Geek Hello Geek Hello Geek99 Hello Geek Hello Geek Hello Geek06 if condition: # execute these statements else: # execute these statements01 if condition: # execute these statements else: # execute these statements02 geeks for geeks6 geeks for geeks7 geeks for geeks8 if condition: # execute these statements else: # execute these statements06 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements4 if condition: # execute these statements else: # execute these statements09 đầ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. con trănHello Geek Hello Geek Hello Geek8 if condition: # execute these statements else: # execute these statements11
if condition: # execute these statements else: # execute these statements12 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek04 Hello Geek Hello Geek Hello Geek05_______1_______06 Hello Geek Hello Geek Hello Geek07 Hello Geek Hello Geek Hello Geek06 Hello Geek Hello Geek Hello Geek05 Hello Geek Hello Geek Hello Geek10 geeks for geeks6 if condition: # execute these statements else: # execute these statements22 geeks for geeks8 geeks for geeks9 while condition: # execute these statements else: # execute these statements5 if condition: # execute these statements else: # execute these statements26 while condition: # execute these statements else: # execute these statements5 if condition: # execute these statements else: # execute these statements12 if condition: # execute these statements else: # execute these statements29 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 if condition: # execute these statements else: # execute these statements12 if condition: # execute these statements else: # execute these statements34 đầu ra. geeks for geeks Sử dụng câu lệnh other với vòng lặp forChú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. con trănHello Geek Hello Geek Hello Geek8 if condition: # execute these statements else: # execute these statements36
if condition: # execute these statements else: # execute these statements12 if condition: # execute these statements else: # execute these statements1 Hello Geek Hello Geek Hello Geek04 Hello Geek Hello Geek Hello Geek05_______1_______06 Hello Geek Hello Geek Hello Geek07 Hello Geek Hello Geek Hello Geek06 Hello Geek Hello Geek Hello Geek05 Hello Geek Hello Geek Hello Geek10 geeks for geeks6 if condition: # execute these statements else: # execute these statements22 geeks for geeks8 geeks for geeks9 while condition: # execute these statements else: # execute these statements5 if condition: # execute these statements else: # execute these statements26 while condition: # execute these statements else: # execute these statements5 if condition: # execute these statements else: # execute these statements12 if condition: # execute these statements else: # execute these statements29 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 if condition: # execute these statements else: # execute these statements12 if condition: # execute these statements else: # execute these statements34 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 if condition: # execute these statements else: # execute these statements65 while condition: # execute these statements else: # execute these statements7 đầu ra. geeks for geeks Inside Else Block Vòng lồng nhauNgô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 Geek0 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 Geek1 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ănHello Geek Hello Geek Hello Geek8 if condition: # execute these statements else: # execute these statements68 if condition: # execute these statements else: # execute these statements69 if condition: # execute these statements else: # execute these statements70 if condition: # execute these statements else: # execute these statements71 if condition: # execute these statements else: # execute these statements72 geeks for geeks6 geeks for geeks7 geeks for geeks8 geeks for geeks9 while condition: # execute these statements else: # execute these statements5 while condition: # execute these statements else: # execute these statements2 Hello Geek Hello Geek Hello Geek06 Hello Geek Hello Geek Hello Geek99 if condition: # execute these statements else: # execute these statements6 if condition: # execute these statements else: # execute these statements7 geeks for geeks6 if condition: # execute these statements else: # execute these statements84_______70_______8 geeks for geeks9 if condition: # execute these statements else: # execute these statements87 if condition: # execute these statements else: # execute these statements88 while condition: # execute these statements else: # execute these statements4 if condition: # execute these statements else: # execute these statements90 if condition: # execute these statements else: # execute these statements1 if condition: # execute these statements else: # execute these statements92 while condition: # execute these statements else: # execute these statements7 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements4 Hello Geek Hello Geek Hello Geek62 đầu ra. Hello Geek Hello Geek Hello Geek2 Câu lệnh điều khiển vòng lặpCá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ănif condition: # execute these statements else: # execute these statements97 geeks for geeks6 if condition: # execute these statements else: # execute these statements99 geeks for geeks8 while condition: # execute these statements else: # execute these statements01 for iterator_var in sequence: statements(s)9 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements04 if condition: # execute these statements else: # execute these statements99 if condition: # execute these statements else: # execute these statements1 if condition: # execute these statements else: # execute these statements1 while condition: # execute these statements else: # execute these statements08 while condition: # execute these statements else: # execute these statements09 if condition: # execute these statements else: # execute these statements99 if condition: # execute these statements else: # execute these statements1 if condition: # execute these statements else: # execute these statements1 while condition: # execute these statements else: # execute these statements13 for iterator_var in sequence: statements(s)9 if condition: # execute these statements else: # execute these statements88 while condition: # execute these statements else: # execute these statements16 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 statements20 while condition: # execute these statements else: # execute these statements21 đầu ra. Hello Geek Hello Geek Hello Geek3 Tuyên bố phá vỡ.Nó mang lại sự kiểm soát ra khỏi vòng lặp con trăngeeks for geeks6 if condition: # execute these statements else: # execute these statements99 geeks for geeks8 while condition: # execute these statements else: # execute these statements01 for iterator_var in sequence: statements(s)9
if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements28 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements30 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements04 if condition: # execute these statements else: # execute these statements99 if condition: # execute these statements else: # execute these statements1 if condition: # execute these statements else: # execute these statements1 while condition: # execute these statements else: # execute these statements08 while condition: # execute these statements else: # execute these statements09 if condition: # execute these statements else: # execute these statements99 if condition: # execute these statements else: # execute these statements1 if condition: # execute these statements else: # execute these statements1 while condition: # execute these statements else: # execute these statements13 for iterator_var in sequence: statements(s)9 if condition: # execute these statements else: # execute these statements88 while condition: # execute these statements else: # execute these statements44
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 statements20 while condition: # execute these statements else: # execute these statements21 đầu ra. Hello Geek Hello Geek Hello Geek4 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ănwhile condition: # execute these statements else: # execute these statements49 geeks for geeks6 if condition: # execute these statements else: # execute these statements99 geeks for geeks8 while condition: # execute these statements else: # execute these statements01 for iterator_var in sequence: statements(s)9 if condition: # execute these statements else: # execute these statements7 while condition: # execute these statements else: # execute these statements56 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 statements59 while condition: # execute these statements else: # execute these statements21 đầu ra. Hello Geek Hello Geek Hello Geek5 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 Python3while condition: # execute these statements else: # execute these statements61
while condition: # execute these statements else: # execute these statements62_______3_______1 Hello Geek Hello Geek Hello Geek04 while condition: # execute these statements else: # execute these statements65 Hello Geek Hello Geek Hello Geek06 while condition: # execute these statements else: # execute these statements67 Hello Geek Hello Geek Hello Geek06 while condition: # execute these statements else: # execute these statements69 Hello Geek Hello Geek Hello Geek10
geeks for geeks6 while condition: # execute these statements else: # execute these statements72 geeks for geeks8 while condition: # execute these statements else: # execute these statements74
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 statements77 Đầu ra Hello Geek Hello Geek Hello Geek6 Ở đâ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. |