Cú pháp chính xác cho câu lệnh if trong Python là gì?

Không giống như nhiều ngôn ngữ máy tính khác, như C++ và java, Python sử dụng thụt đầu dòng để xác định các khối mã. Mã giả dưới đây sẽ giúp chứng minh

This is some code.
This is some more code.
 This code is part of a code block.
 And so is this!
 And this!!!
This code isn't part of the code block. =[
 But this code is part of a new code block!
 And this code is, as well!

Như chúng ta có thể thấy, có thể có nhiều khối mã. Bạn cũng có thể chứa các khối mã bên trong một khối mã khác, miễn là nó thụt vào. Không có giới hạn cho thụt lề, nhưng nó phải được giữ cố định nếu không bạn sẽ gặp lỗi

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!

Mặc dù bạn được phép sử dụng khoảng trắng và tab, nhưng bạn nên sử dụng khoảng trắng. Ngoài ra, bạn không thể trộn cả khoảng trắng và tab với nhau nếu không bạn sẽ gặp lỗi

Bạn nên biết rằng vì chúng ta sẽ sử dụng Trình thông dịch Python để tính toán mã ví dụ, nên

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
7 hoàn toàn giống với
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
8, ngoại trừ điều đó có nghĩa là bạn đang ở trong một khối mã. Đoạn mã dưới đây sẽ chứng minh

>>> if True:
..     print["Hello, world!"]
...
Hello, world!


Bây giờ chúng ta đã giải quyết được vấn đề này, chúng ta có thể tiếp tục với phần chính của bài học

Trong hầu hết, nếu không muốn nói là tất cả các ngôn ngữ lập trình, đều có những cách đặc biệt để kiểm soát dòng mã. Điều khiển như vậy cho phép một số phần mã thực thi nếu các điều kiện phù hợp. Kiểm soát luồng này được thực hiện bằng cách sử dụng các câu lệnh và khối mã. Câu lệnh phổ biến và được sử dụng rộng rãi nhất là câu lệnh

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9. Nó sẽ tính toán nếu một tuyên bố là đúng hay sai. Nếu đúng, nó sẽ thực thi mã. Nếu nó sai, nó sẽ không thực thi mã

Hãy nhớ rằng

>>> if True:
..     print["Hello, world!"]
...
Hello, world!
0 và
>>> if True:
..     print["Hello, world!"]
...
Hello, world!
1 là Booleans trong Python. Điều này có nghĩa là
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 và các câu lệnh có điều kiện khác sẽ sử dụng toán Boolean để tính toán trạng thái Boolean của chúng. Bạn cũng nên lưu ý sự cần thiết của dấu hai chấm [
>>> if True:
..     print["Hello, world!"]
...
Hello, world!
3] ở cuối câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9. Điều này là cần thiết ở phần cuối của câu lệnh luồng điều khiển

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
1


Bạn cũng có thể kết hợp nhiều điều kiện với nhau bằng cách sử dụng các toán tử Boolean đã dạy trước đó;

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
5


Như đã nói ở phần đầu của bài học, bạn có thể lồng các câu lệnh trong một khối mã để bắt đầu một khối mã mới, miễn là chúng tuân theo các vết lõm tương ứng của chúng

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
6


Mặc dù bạn có thể lồng các câu lệnh vào nhau, nhưng nó có thể trở nên khó quản lý. Sẽ là Pythonic hơn nếu giữ cho các câu lệnh phẳng. Điều này có nghĩa là thay vì lồng bốn câu lệnh

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 với nhau để làm một việc gì đó, sẽ tốt hơn nếu sử dụng một câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 với một số toán tử
>>> if True:
..     print["Hello, world!"]
...
Hello, world!
6

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
0

Dấu chấm phẩy, dấu gạch chéo ngược và dấu ngoặc đơn[sửa | sửa mã nguồn]. sửa nguồn]

Có thể bạn sẽ muốn giữ nhiều dòng mã trên một dòng. Điều này có thể được thực hiện bằng cách sử dụng dấu chấm phẩy [

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
11]. Dấu chấm phẩy hoạt động như một dòng mới, mặc dù lập trình viên sẽ không thấy nó như vậy. Lấy đoạn mã sau làm ví dụ

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
2


Mã tương tự này có thể được cô đọng thành một dòng, như ví dụ bên dưới


Điều tuyệt vời khi sử dụng dấu chấm phẩy là không cần thụt lề. Mặc dù công dụng của chúng có vẻ tuyệt vời, nhưng nên sử dụng ít dấu chấm phẩy. Do đó, bạn không nên sử dụng chúng với các khối mã nhiều dòng lớn hoặc dài. Điều này có thể gây khó khăn và rắc rối trong việc quản lý. Hãy nhìn vào hai ví dụ xấu

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
3


Bây giờ hãy nghĩ về một vấn đề nan giải khác. Hãy tưởng tượng rằng bạn cần so sánh sáu số trong một câu lệnh

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9. Một câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 như vậy có thể trở nên dài và bạn không muốn làm nó tệ hơn bằng cách lồng các phần riêng biệt vào sáu câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 riêng biệt. Dấu gạch chéo ngược [
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
15] có thể giúp giải quyết vấn đề này. Dấu gạch chéo ngược cho phép bạn chia một đoạn mã dài thành nhiều phần

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
8


Giống như dấu chấm phẩy, bạn không cần lo lắng về việc thụt lề sau dấu chấm phẩy

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
16. Điều này cho phép bạn giữ mức mã với phần đầu tiên của câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 của bạn. Tuy nhiên, hãy coi chừng, vì bất kỳ ký tự nào sau dấu gạch chéo ngược sẽ gây ra lỗi. Điều này thậm chí có nghĩa là khoảng trắng thừa sẽ gây ra lỗi. Bạn sẽ cần hết sức cẩn thận khi làm việc với dấu gạch chéo ngược

Một giải pháp khả thi hơn là sử dụng dấu ngoặc đơn [

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
18] để đính kèm tất cả mã. Việc sử dụng dấu ngoặc đơn hoạt động giống như dấu gạch chéo ngược, nhưng nó cho phép thêm các ký tự ở cuối dòng logic không hoàn chỉnh, bao gồm cả khoảng trắng

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
0

Sẽ có nhiều lúc bạn muốn thực thi mã trong trường hợp câu lệnh

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 không đúng. Điều này sẽ yêu cầu sử dụng câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
50. Câu lệnh này sẽ thực thi khi câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 là sai. Nó cần phải ở cùng mức thụt đầu dòng như câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
1


Mặc dù

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
50 và
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 cần được căn chỉnh với nhau, nhưng phần thụt vào khối mã của chúng có thể khác nhau như ví dụ dưới đây minh họa

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
2


Mặc dù không có yêu cầu đối với nó, nhưng bạn nên thụt lề sau mỗi bốn khoảng trắng


Câu lệnh

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
50 có thể được sử dụng để làm cho mã dễ đọc hơn. Nếu bạn muốn mã hoạt động khi điều kiện không đúng, bạn có thể phủ nhận điều kiện

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
3

hoặc bạn có thể để nguyên điều kiện và hành động theo câu lệnh

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
50

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
4

Giống như một vấn đề tương tự trước đây, bạn có thể cần phải làm điều gì đó có vẻ tẻ nhạt. Giả sử bạn muốn có bốn câu lệnh

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 cùng nhau, nhưng bạn chỉ muốn một câu lệnh thực thi. Bạn có thể thử viết những câu lệnh đó, nhưng sẽ dễ dàng hơn khi viết một câu lệnh lớn bằng cách sử dụng câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
58.
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
58 là viết tắt của "else if", hoạt động như một
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
50 và
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9. Điều này có nghĩa là nếu
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
9 đầu tiên không đúng, thì nó sẽ chuyển sang
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
58 và xem nó có đúng không. Điều này có thể có ích sau này

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
5

Câu lệnh

This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
58 là một trong những cách mà Python triển khai câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
65 hoặc
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
66. Ví dụ đơn giản sau minh họa tương đương với câu lệnh
This is some code.
           This is a block of code indented very far away.
           This is completely legal in Python, although not encouraged.
           The preferred indentation is in multiples of four.
    This isn't a block of code; it follows no indentation style.
   This would cause an error!
65

Chủ Đề