Hướng dẫn can you say if true in python? - bạn có thể nói nếu đúng trong python?

Bài học [Chỉnh sửa | Chỉnh sửa nguồn][edit | edit source]

Vết lõm [chỉnh sửa | Chỉnh sửa nguồn][edit | edit source]

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 lề để xác định các khối mã. Mã giả sau đâ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ã trong một khối mã khác, miễn là nó được đặt ra. Không có giới hạn ở thụt lề, nhưng nó phải được giữ không đổi hoặc 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 không gian trắng và các tab, nhưng bạn sẽ sử dụng không gian trắng. Ngoài ra, bạn không thể trộn cả hai không gian trắng và các tab với nhau hoặc bạn sẽ gặp lỗi.

Bạn nên biết rằng vì chúng tôi sẽ sử dụng trình thông dịch Python để tính toán 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!
8 giống hệt 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, ngoại trừ điều đó có nghĩa là bạn đang ở trong một khối mã. Mã dưới đây sẽ chứng minh.

>>> if True:
...     print("Hello, world!")
...
Hello, world!


Bây giờ chúng tôi đã hiểu điều này, chúng tôi có thể tiếp tục với phần chính của bài học.

Câu lệnh IF [Chỉnh sửa | Chỉnh sửa nguồn][edit | edit source]

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

>>> if True:
...     print("Hello, world!")
...
Hello, world!
0. Nó sẽ tính toán nếu một câu lệnh là đúng hoặc sai. Nếu đó là sự thật, 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!
1 và
>>> if True:
...     print("Hello, world!")
...
Hello, world!
2 là Booleans trong Python. Điều này có nghĩa là
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 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 họ. Bạn cũng nên lưu ý sự cần thiết của đại tràng (
>>> if True:
...     print("Hello, world!")
...
Hello, world!
4) vào cuối tuyên bố
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0. Điều này là cần thiết ở cuối một tuyên bố dòng điều khiển.

>>> if 1 == 1:
...     print("This will print!")
...
This will print!
>>> if 1 == 2:
...     print("This will never print. =/")
...
>>> if True:
...     print("True!")
...
True!
>>> if False:
...     print("False!")
...


Bạn cũng có thể trộn một số điều kiện với nhau bằng cách sử dụng các nhà khai thác Boolean được dạy trước đó;

>>> if True:
...     print("Hello, world!")
...
Hello, world!
6,
>>> if True:
...     print("Hello, world!")
...
Hello, world!
7,
>>> if True:
...     print("Hello, world!")
...
Hello, world!
8.

>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!


Như đã nêu ở đầu bài học, bạn có thể làm tổ 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.

>>> if 30 == 30:
...     print("30 equals 30!")
...     if True:
...         print("True is True!")
...
30 equals 30!
True is True!


Mặc dù bạn có thể làm tổ trong các câu lệnh trong nhau, nhưng nó có thể trở nên khó quản lý. Nó sẽ là pythonic hơn để giữ tuyên bố phẳng. Điều này có nghĩa là thay vì làm tổ bốn câu lệnh

>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 cùng nhau để làm một cái gì đó, tốt hơn là sử dụng một câu lệnh
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 với một số toán tử
>>> if True:
...     print("Hello, world!")
...
Hello, world!
7.

>>> if 1 == 1:
...     if 2 == 2:
...         if 3 == 3:
...             print("1, 2, and 3 are equal to their selves!")
...
1, 2, and 3 are equal to their selves!
>>>
>>> if 1 == 1 and 2 == 2 and 3 == 3:
...     print("1, 2, and 3 are equal to their selves!")
...
1, 2, and 3 are equal to their selves!

Bán Đại tá, Backslash và dấu ngoặc đơn [Chỉnh sửa | Chỉnh sửa nguồn][edit | edit source]

Có lẽ bạn sẽ muốn giữ một số dòng mã chỉ 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 (

>>> if 1 == 1:
...     print("This will print!")
...
This will print!
>>> if 1 == 2:
...     print("This will never print. =/")
...
>>> if True:
...     print("True!")
...
True!
>>> if False:
...     print("False!")
...
2). Bán kết hoạt động giố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 mã sau làm ví dụ.

>>> if 1 != 2:
...  a = 1
...  b = 2
...  c = 3
...


Mã tương tự này có thể được ngưng tụ thành một dòng, giống như ví dụ dưới đây.

>>> if 1 != 2: a = 1; b = 2; c = 3
...


Điều tuyệt vời về việc sử dụng dấu chấm phẩy là không cần phải thụt vào. Mặc dù việc sử dụng của chúng có vẻ tuyệt vời, nên sử dụng 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ã đa dòng lớn hoặc dài. Điều này có thể làm cho nó khó khăn và khó hiểu để quản lý. Hãy xem hai ví dụ xấu.

>>> if 1 != 2: a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; g = 7; h = 8; i = 9;
...
>>> if 1 == 1: spam = 2 * 2; eggs = spam / 2; bacon = (spam + eggs) * 4;
...


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 tuyên bố

>>> if True:
...     print("Hello, world!")
...
Hello, world!
0. Một tuyên bố
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 như vậy có thể trở nên dài và bạn không muốn làm cho nó tồi tệ hơn bằng cách làm tổ các phần riêng biệt trong sáu câu lệnh riêng biệt. Backslash (
>>> if 1 == 1:
...     print("This will print!")
...
This will print!
>>> if 1 == 2:
...     print("This will never print. =/")
...
>>> if True:
...     print("True!")
...
True!
>>> if False:
...     print("False!")
...
6) có thể giúp giải quyết vấn đề này. Backslash 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!
0


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

>>> if 1 == 1:
...     print("This will print!")
...
This will print!
>>> if 1 == 2:
...     print("This will never print. =/")
...
>>> if True:
...     print("True!")
...
True!
>>> if False:
...     print("False!")
...
7. Đ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
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 của bạn. Xem ra mặc dù, vì bất kỳ nhân vậ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 dư thừa sẽ gây ra lỗi. Bạn sẽ cần phải cẩn thận hơn khi làm việc với các dấu gạch chéo ngược.any character after the backslash will cause an error. This even means excess whitespaces will cause an error. You'll need to be extra careful when working with backslashes.

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

>>> if 1 == 1:
...     print("This will print!")
...
This will print!
>>> if 1 == 2:
...     print("This will never print. =/")
...
>>> if True:
...     print("True!")
...
True!
>>> if False:
...     print("False!")
...
9) để gửi kèm tất cả các mã. Việc sử dụng dấu ngoặc đơn hoạt động như dấu gạch chéo ngược, nhưng nó cho phép các ký tự phụ ở 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!
1

Tuyên bố khác [Chỉnh sửa | Chỉnh sửa nguồn][edit | edit source]

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

>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 không đúng. Điều này sẽ yêu cầu sử dụng tuyên bố
>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
1. Tuyên bố này sẽ thực thi khi câu lệnh
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 là sai. Nó cần phải ở cùng cấp độ thụt với câu lệnh
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0.

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ù

>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
1 và
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 cần được liên kết với nhau, việc thụt các khối mã của chúng có thể khác nhau như ví dụ dưới đây thể hiệ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


Mặc dù không có yêu cầu cho nó, nhưng bạn khuyên bạn nên thụt lên mỗi bốn không gian.


Câu lệnh

>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
1 có thể được sử dụng để làm cho mã dễ đọc hơn. Nếu bạn muốn mã hành động khi một đ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!
4

Hoặc bạn có thể rời khỏi điều kiện như hiện tại và hành động trên tuyên bố

>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
1:

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

Tuyên bố Elif [Chỉnh sửa | Chỉnh sửa nguồn][edit | edit source]

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 bốn câu

>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 cùng nhau, nhưng bạn chỉ muốn một câu thực thi. Bạn có thể cố gắng viết những điều đó cho các câu lệnh, nhưng sẽ dễ dàng hơn khi viết một bản lớn bằng cách sử dụng câu lệnh
>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
9.
>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
9 là viết tắt của "khác nếu", hoạt động giống như
>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
1 và
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0. Điều này có nghĩa là nếu
>>> if True:
...     print("Hello, world!")
...
Hello, world!
0 ban đầu không đúng, nó sẽ đi đến
>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
9 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!
6

Tuyên bố

>>> if 1 == 1 and 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 and 2 == 2:
...     print("True")
...
>>> if 1 == 2 or 2 == 2:
...     print("True!")
...
True!
>>> if 1 == 2 or 2 == 3:
...     print("True!")
...
>>> if not False:
...     print("True!")
...
True!
>>> if not 1 == 2:
...     print("True!")
...
True!
9 là một trong những cách mà Python thực hiện một tuyên bố
>>> if 30 == 30:
...     print("30 equals 30!")
...     if True:
...         print("True is True!")
...
30 equals 30!
True is True!
6 hoặc
>>> if 30 == 30:
...     print("30 equals 30!")
...     if True:
...         print("True is True!")
...
30 equals 30!
True is True!
7. Ví dụ đơn giản sau đây minh họa tương đương của câu lệnh
>>> if 30 == 30:
...     print("30 equals 30!")
...     if True:
...         print("True is True!")
...
30 equals 30!
True is True!
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!
7

>>> if 30 == 30:
...     print("30 equals 30!")
...     if True:
...         print("True is True!")
...
30 equals 30!
True is True!
9

1. Tài liệu của Python:


2. Phương pháp của Python:


3. Các chức năng tích hợp của Python:

Nếu nếu công việc thực sự trong Python?

Nó sẽ tính toán nếu một câu lệnh là đúng hoặc sai.Nếu đó là sự thật, nó sẽ thực thi mã.Nếu nó sai, nó sẽ không thực thi mã.If it's true, it will execute the code. If it's false, it won't execute the code.

Có đúng và sai trong Python không?

Trong lập trình, bạn thường cần biết một biểu thức là đúng hay sai.Bạn có thể đánh giá bất kỳ biểu thức nào trong Python và nhận được một trong hai câu trả lời, đúng hoặc sai.You can evaluate any expression in Python, and get one of two answers, True or False .

Đúng là một biến trong Python?

Một biến Bool Python có hai giá trị có thể, đúng và sai.Trong Python 3, các giá trị này thực sự là từ khóa Python và được viết hoa.Do đó, chúng không thể được sử dụng làm biến và không thể được gán các giá trị khác nhau.Đúng và sai có thể được gán cho bất kỳ biến nào, sau đó trở thành bool.True and False can be assigned to any variable, which then becomes a bool .