Ba loại câu lệnh điều khiển được sử dụng trong python là gì?

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ể

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áp

Dướ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 ý

  1. Dấu hai chấm
    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
    
    7 ở cuối dòng
    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ại điều này là một lỗi phổ biến
  2. Sự thụt lề của câu lệnh in. Nếu bạn nhớ
    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
    
    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ệ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. Đâ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ỗi
  3. 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 đượ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ệ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 như trong nhiều ngôn ngữ lập trình khác

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"]
73

Tậ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

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"]
7

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"]
75

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"]
1

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"]
2

Tậ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

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"]
77

Ví dụ dưới đây cho phép bạn so sánh cụ thể hơn giữa a và b

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"]
5

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"]
6

Cấu trúc tổng thể tương tự như 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"]
78. Có ba điều bổ sung cần chú ý

  1. Mỗi 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ó biểu thức kiểm tra riêng
  2. Bạn có thể có bao nhiêu 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 tùy thích
  3. Việc thực thi toàn bộ câu lệnh dừng lại sau khi một biểu thứ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"]
    
    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ĩa

Vòng lặp
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"]
13

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

if expression :
    statement 1
    statement 2
    ...
    statement n

statement always executed
3

if expression :
    statement 1
    statement 2
    ...
    statement n

statement always executed
4

Điểm cần lưu ý

  1. 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à
    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"]
    
    16 trong số các toán tử khác
  2. 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
  3. 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à
    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 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úc
  4. 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
  5. Chúng tôi gọi biến của chúng tôi 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"]
    
    18 chứ không phải
    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 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 đị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"]
    
    21 bây giờ, chúng tôi không thể sử dụng chức năng
    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 trong phiên Python này

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

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"]
0

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"]
1

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"]
2

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

Dung dịch

  1. Bởi vì tôi tăng trước tổng, nên bạn tính tổng từ 1 đến 11
  2. Giá trị Boolean được đặt thành Sai, vòng lặp sẽ không bao giờ được thực hiện
  3. 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
  4. 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
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"]
23

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

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"]
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"]
4

Phần quan trọng của điều này 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"]
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ối

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
    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"]
    
    26
  • Các ký tự trong một chuỗi

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"]
5

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"]
6

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

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"]
27 như thế này

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"]
7

Nghiên cứu phương pháp của

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"]
28 rồi viết lại ví dụ 8 từ phần vòng lặp
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"]
23 ở trên để nó in ra 4 thành phần của
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"]
50

Dung dị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"]
8

Định dạng của

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"]
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.

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 định
Lựa chọn - được sử dụng cho các quyết định và phân nhánh
Sự 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ần

Ba 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ọn
Câu lệnh lặp/vòng lặp
câu lệnh nhảy

Câ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 thi

3 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

Chủ Đề