Hầu hết các chương trình không hoạt động bằng cách thực hiện một tập hợp các câu lệnh tuần tự đơn giản. Mã này được xây dựng sao cho các quyết định và các đường dẫn khác nhau thông qua chương trình có thể được thực hiện dựa trên những thay đổi về giá trị biến
Để thực hiện điều này, tất cả các ngôn ngữ lập trình đều có một tập hợp các cấu trúc điều khiển cho phép điều này xảy ra
Trong tập này, chúng ta sẽ xem xét cách tạo vòng lặp và nhánh trong mã Python. Cụ thể, chúng ta sẽ xem xét ba cấu trúc điều khiển, cụ thể là
- nếu. khác
- trong khi…
- vì …
Câu lệnh print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 và các biến thể
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Câu lệnh
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 đơn giản cho phép chương trình phân nhánh dựa trên việc đánh giá một biểu thứcĐịnh dạng cơ bản của câu lệnh
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 làif expression :
statement 1
statement 2
...
statement n
statement always executed
Nếu biểu thức có giá trị là
Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
2 thì các câu lệnh từ 1 đến n sẽ được thực hiện theo sau là Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
3. Nếu biểu thức là Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
4, chỉ có Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
3 được thực thi. Python biết dòng mã nào có liên quan đến câu lệnh print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 bằng cách thụt đầu dòng, không cần thêm cú phápDưới đây là một số ví dụ
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Example 1
value is 5 threshold is 4
5 is bigger than 4
Example 2
value is 5 new threshold is 6
Example 3
value is 5 final threshold is 5
value, 5, and threshold, 5, are equal
Trong các ví dụ trên có ba điều cần lưu ý
- Dấu hai chấm
7 ở cuối dòngExample 1 value is 5 threshold is 4 5 is bigger than 4 Example 2 value is 5 new threshold is 6 Example 3 value is 5 final threshold is 5 value, 5, and threshold, 5, are equal
9. Để lại điều này là một lỗi phổ biếnprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- Sự thụt lề của câu lệnh in. Nếu bạn nhớ
7 trên dòng trước đó, Jupyter [hoặc bất kỳ IDE Python nào khác] sẽ tự động thụt đầu dòng cho bạn. Tất cả các câu được thụt vào ở cấp độ này được coi là một phần của câu lệnhExample 1 value is 5 threshold is 4 5 is bigger than 4 Example 2 value is 5 new threshold is 6 Example 3 value is 5 final threshold is 5 value, 5, and threshold, 5, are equal
9. Đây là một tính năng khá độc đáo của Python, nó quan tâm đến việc thụt đầu dòng. Nếu có quá nhiều hoặc quá ít thụt đầu dòng, bạn sẽ gặp lỗiprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- Câu lệnh
9 được kết thúc bằng cách xóa thụt lề. Không có kết thúc rõ ràng cho câu lệnhprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 như trong nhiều ngôn ngữ lập trình khácprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Trong ví dụ trước, lưu ý rằng trong Python, toán tử được sử dụng để kiểm tra đẳng thức là
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
73Tập thể dục
Thêm một câu lệnh if khác vào ví dụ 2 để kiểm tra xem b có lớn hơn hoặc bằng a không
Dung dịch
7print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Thay vì sử dụng hai câu lệnh
print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
9 riêng biệt để quyết định câu lệnh nào lớn hơn, chúng ta có thể sử dụng cấu trúc print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
75print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
1print["\nExample 1\n"]
value = 5
threshold= 4
print["value is", value, "threshold is ",threshold]
if value > threshold :
print[value, "is bigger than ", threshold]
print["\nExample 2\n"]
high_threshold = 6
print["value is", value, "new threshold is ",high_threshold]
if value > high_threshold :
print[value , "is above ", high_threshold, "threshold"]
print["\nExample 3\n"]
mid_threshold = 5
print["value is", value, "final threshold is ",mid_threshold]
if value == mid_threshold :
print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
2Tập thể dục
Lặp lại ở trên với các toán tử khác nhau ‘ threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"] 9 là phiên bản
77print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Ví dụ dưới đây cho phép bạn so sánh cụ thể hơn giữa a và b
5print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
6print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Cấu trúc tổng thể tương tự như câu lệnh
78. Có ba điều bổ sung cần chú ýprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- Mỗi mệnh đề
79 có biểu thức kiểm tra riêngprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- Bạn có thể có bao nhiêu mệnh đề
79 tùy thíchprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- Việc thực thi toàn bộ câu lệnh dừng lại sau khi một biểu thức
79 được tìm thấy là Đúng. Do đó, thứ tự của mệnh đềprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
79 có thể có ý nghĩaprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Vòng lặp
13print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Vòng lặp while được sử dụng để thực thi liên tục các dòng mã cho đến khi một số điều kiện trở thành Sai
Để vòng lặp kết thúc, phải có thứ gì đó trong mã có khả năng thay đổi điều kiện
3if expression : statement 1 statement 2 ... statement n statement always executed
4if expression : statement 1 statement 2 ... statement n statement always executed
Điểm cần lưu ý
- Mệnh đề điều kiện [i threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"] 15 và
16 trong số các toán tử khácprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- Các câu lệnh sau mệnh đề while chỉ được thực hiện nếu điều kiện đánh giá là True
- Trong các câu lệnh sau mệnh đề while nên có điều gì đó có khả năng khiến điều kiện được đánh giá là
4 vào khoảng thời gian tới. Nếu không vòng lặp sẽ không bao giờ kết thúcExample 1 value is 5 threshold is 4 5 is bigger than 4 Example 2 value is 5 new threshold is 6 Example 3 value is 5 final threshold is 5 value, 5, and threshold, 5, are equal
- Trong trường hợp này, câu lệnh cuối cùng trong vòng lặp sẽ thay đổi giá trị của i, một phần của mệnh đề điều kiện, vì vậy hy vọng vòng lặp sẽ kết thúc
- Chúng tôi gọi biến của chúng tôi là
18 chứ không phảiprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
19 vìprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
19 là một hàm dựng sẵn [thử nhập nó vào, chú ý trình soạn thảo chuyển nó thành màu xanh lá cây]. Nếu chúng tôi xác địnhprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
21 bây giờ, chúng tôi không thể sử dụng chức năngprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
19 trong phiên Python nàyprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Bài tập - Những điều có thể sai với vòng lặp while
Trong các ví dụ bên dưới, không cần chạy chúng, hãy cố gắng quyết định tại sao chúng tôi sẽ không nhận được câu trả lời cần thiết. Chạy từng cái một, rồi sửa chúng. Hãy nhớ rằng khi đầu vào bên cạnh ô sổ ghi chép là [*] thì trình thông dịch Python của bạn vẫn hoạt động
0print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
1print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
2print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
3print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Dung dịch
- Bởi vì tôi tăng trước tổng, nên bạn tính tổng từ 1 đến 11
- Giá trị Boolean được đặt thành Sai, vòng lặp sẽ không bao giờ được thực hiện
- Khi i bằng 10 thì biểu thức là Sai và vòng lặp không thực hiện nên ta chỉ tính tổng từ 1 đến 9
- Bởi vì bạn không thể đảm bảo đại diện bên trong của Float, bạn không bao giờ nên cố gắng so sánh chúng cho bình đẳng. Trong trường hợp cụ thể này, tôi không bao giờ 'bằng' n và do đó vòng lặp không bao giờ kết thúc. - Nếu bạn đã thử chạy chương trình này, bạn có thể dừng nó bằng cách sử dụng Ctrl+c trong thiết bị đầu cuối hoặc vào menu nhân của sổ ghi chép và chọn ngắt
Vòng lặp
23print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Vòng lặp for, giống như vòng lặp while, lặp đi lặp lại việc thực thi một tập hợp các câu lệnh. Sự khác biệt là trong vòng lặp for chúng ta biết ngay từ đầu tần suất các câu lệnh trong vòng lặp sẽ được thực thi. Chúng ta không cần phải dựa vào một biến được thay đổi trong các câu lệnh lặp
Định dạng cơ bản của câu lệnh
23 làprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
4print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Phần quan trọng của điều này là
25. Cụm từ được sử dụng trong tài liệu là nó phải là 'có thể lặp lại'. Điều đó có nghĩa là, bạn có thể đếm theo trình tự, bắt đầu từ đầu và dừng lại ở cuốiprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Có rất nhiều ví dụ về những thứ có thể lặp lại, một số trong số đó chúng ta đã bắt gặp
- Danh sách có thể lặp lại - chúng không nhất thiết phải chứa số, bạn lặp lại các phần tử trong danh sách
- Hàm
26print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
- Các ký tự trong một chuỗi
5print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
6print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Tập thể dục
Giả sử rằng chúng ta có một chuỗi chứa bộ 4 loại giá trị khác nhau được phân tách bằng
27 như thế nàyprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
7print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Nghiên cứu phương pháp của
28 rồi viết lại ví dụ 8 từ phần vòng lặpprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
23 ở trên để nó in ra 4 thành phần củaprint["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
50print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Dung dịch
8print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
Định dạng của
50 rất giống định dạng của bản ghi trong tệp csv. Trong các phần sau, chúng ta sẽ thấy cách chúng ta có thể trích xuất các giá trị này và gán chúng cho các biến để xử lý tiếp thay vì in chúng ra.print["\nExample 1\n"] value = 5 threshold= 4 print["value is", value, "threshold is ",threshold] if value > threshold : print[value, "is bigger than ", threshold] print["\nExample 2\n"] high_threshold = 6 print["value is", value, "new threshold is ",high_threshold] if value > high_threshold : print[value , "is above ", high_threshold, "threshold"] print["\nExample 3\n"] mid_threshold = 5 print["value is", value, "final threshold is ",mid_threshold] if value == mid_threshold : print["value, ", value, " and threshold,", mid_threshold, ", are equal"]
3 loại cấu trúc điều khiển trong Python là gì?
Python có ba loại cấu trúc điều khiển. .Tuần tự - chế độ mặc địnhLựa chọn - được sử dụng cho các quyết định và phân nhánhSự lặp lại - được sử dụng để lặp lại, i. e. , lặp lại một đoạn mã nhiều lầnBa loại câu lệnh kiểm soát là gì?
Có ba loại câu lệnh điều khiển. .Câu lệnh điều kiện/lựa chọnCâu lệnh lặp/vòng lặpcâu lệnh nhảyCâu lệnh điều khiển trong Python là gì?
Các vòng lặp được sử dụng trong Python để lặp lại liên tục một đoạn mã. Các câu lệnh điều khiển được thiết kế để phục vụ mục đích sửa đổi việc thực thi vòng lặp từ hành vi mặc định của nó. Dựa trên một điều kiện, các câu lệnh điều khiển được áp dụng để thay đổi cách vòng lặp thực thi3 cấu trúc điều khiển chính là gì?
Luồng điều khiển thông qua bất kỳ chức năng nhất định nào được triển khai với ba loại cấu trúc điều khiển cơ bản. .tuần tự. chế độ mặc định. .Lựa chọn. được sử dụng cho các quyết định, phân nhánh -- lựa chọn giữa 2 hoặc nhiều đường dẫn thay thế. .sự lặp lại. được sử dụng để lặp, tôi. e. lặp lại một đoạn mã nhiều lần liên tiếp