Hướng dẫn what is an else suite in python? - một bộ khác trong python là gì?

Hãy cùng tìm hiểu về if-else, for-else, while, và cố gắng sử dụng các điều khoản trong Python.

Ảnh của bạn X mạo hiểm trên unplash

Những người khác trong Python

Trong Python, chúng ta có thể tìm thấy điều khoản khác trong ____ 10, ____ 11,

if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
2 vòng lặp và
if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
3Block cũng vậy. mệnh đề khác là tùy chọn trong tất cả các tuyên bố này. Hãy để xem cách mà khối khác được thực thi trong tất cả các câu lệnh và vòng lặp này.else clause in
if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
0statement,
if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
1,
if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
2 loops, and
if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
3block also. else clause is optional in all these statements. Let’s see how else block is executed in all these statements and loops.

4 điều khác trong Python:

  1. if-else
  2. for-else
  3. while-else
  4. try-else

Báo cáo ghép

Báo cáo ghép chứa (các nhóm của) các tuyên bố khác; Chúng ảnh hưởng hoặc kiểm soát việc thực hiện các tuyên bố khác theo một cách nào đó. - Tài liệu Python

________ 14, ________ 12, ________ 16,

if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
7 là các tuyên bố ghép.

Các câu lệnh

if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
4,
if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
2 và
if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
6 thực hiện các cấu trúc luồng điều khiển truyền thống.

if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
3 Chỉ định các trình xử lý ngoại lệ và/hoặc mã dọn dẹp cho một nhóm các câu lệnh.

SYNTAX FOR-ALSE M điều khoản:

for item in iterable:    --->header
suite
else:
suite

Điều khoản IF-Else:

Tuyên bố

if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
4 được sử dụng để thực hiện có điều kiện.

SYNTAX IF-ALSE MULAUS:

if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite

Nếu câu lệnh sẽ chọn chính xác một trong các bộ bằng cách đánh giá từng biểu thức cho đến khi một cho đến khi được tìm thấy là đúng, thì bộ đó được thực thi (và không có phần nào khác của câu lệnh IF được thực thi hoặc đánh giá). if statement is executed or evaluated).

Nếu tất cả các biểu thức là sai, bộ của mệnh đề khác, nếu có, được thực thi.else clause, if present, is executed.

Chỉ có một bộ sẽ được thực thi trong câu lệnh IF. [Hoặc If-suite hoặc elif-suite hoặc duite khác).if statement.[either if-suite or elif-suite or else-suite).

Có thể không có mệnh đề Elif hoặc nhiều hơn, và mệnh đề khác là tùy chọn.elif clause, and the else clause is optional.

Ảnh của tác giả

Ví dụ 1: mệnh đề IF-Else

Thực hiện mã dưới đây ba lần.

  1. Đầu vào lần đầu tiên được đưa ra là
    x = int(input("Please enter an integer: "))
    if x>5:
    print ("Greater than 5")
    elif x<5:
    print ("Less than 5")
    else:
    print ("The number is 5")

    print ("Outside if-else block")
    '''
    Output 1 :
    Please enter an integer: 7
    Greater than 5
    Outside if-else block

    Output 2:
    Please enter an integer: 5
    The number is 5
    Outside if-else block

    Output 3:
    Please enter an integer: 3
    Less than 5
    Outside if-else block
    '''

    3. Nếu mệnh đề
    x = int(input("Please enter an integer: "))
    if x>5:
    print ("Greater than 5")
    elif x<5:
    print ("Less than 5")
    else:
    print ("The number is 5")

    print ("Outside if-else block")
    '''
    Output 1 :
    Please enter an integer: 7
    Greater than 5
    Outside if-else block

    Output 2:
    Please enter an integer: 5
    The number is 5
    Outside if-else block

    Output 3:
    Please enter an integer: 3
    Less than 5
    Outside if-else block
    '''

    4Evalted thành true và chỉ khi bộ được thực thi. Không có phần nào khác của câu lệnh IF được đánh giá hoặc thực thi.True and only if suite is executed. No other part of the if statement is evaluated or executed.
  2. Đầu vào lần thứ hai được đưa ra dưới dạng mệnh đề
    x = int(input("Please enter an integer: "))
    if x>5:
    print ("Greater than 5")
    elif x<5:
    print ("Less than 5")
    else:
    print ("The number is 5")

    print ("Outside if-else block")
    '''
    Output 1 :
    Please enter an integer: 7
    Greater than 5
    Outside if-else block

    Output 2:
    Please enter an integer: 5
    The number is 5
    Outside if-else block

    Output 3:
    Please enter an integer: 3
    Less than 5
    Outside if-else block
    '''

    5if ____26 và mệnh đề ELIF
    x = int(input("Please enter an integer: "))
    if x>5:
    print ("Greater than 5")
    elif x<5:
    print ("Less than 5")
    else:
    print ("The number is 5")

    print ("Outside if-else block")
    '''
    Output 1 :
    Please enter an integer: 7
    Greater than 5
    Outside if-else block

    Output 2:
    Please enter an integer: 5
    The number is 5
    Outside if-else block

    Output 3:
    Please enter an integer: 3
    Less than 5
    Outside if-else block
    '''

    7 được đánh giá là sai, do đó, theo mặc định, bộ khác được thực thi.else suite is executed.
  3. Đầu vào lần thứ ba được đưa ra dưới dạng ________ 28.if Điều khoản ____26 được đánh giá là sai. Sau đó, mệnh đề Elif được đánh giá trả về đúng, vì vậy bộ Elif được thực thi. Mệnh đề khác không được thực thi. elif suite is executed. Else clause is not executed.
x = int(input("Please enter an integer: "))
if x>5:
print ("Greater than 5")
elif x<5:
print ("Less than 5")
else:
print ("The number is 5")

print ("Outside if-else block")
'''
Output 1 :
Please enter an integer: 7
Greater than 5
Outside if-else block

Output 2:
Please enter an integer: 5
The number is 5
Outside if-else block

Output 3:
Please enter an integer: 3
Less than 5
Outside if-else block
'''

mệnh đề for-else

for item in iterable:   
suite
else:
suite
0Statement được sử dụng để lặp lại các phần tử của một chuỗi (như chuỗi, tuple hoặc danh sách) hoặc các đối tượng có thể đi được khác.

Đối với vòng lặp có thể có mệnh đề

for item in iterable:   
suite
else:
suite
1; Nó được thực hiện khi vòng lặp chấm dứt thông qua sự cạn kiệt của điều đó. Nhưng không phải khi vòng lặp bị chấm dứt bởi một tuyên bố phá vỡ.
But not when the loop is terminated by a break statement.

Syntax:

for item in iterable:   
suite
else:
suite
  • Có thể đánh giá chỉ một lần. Một iterator được tạo ra cho kết quả của điều đó.
  • Bộ bộ sau đó được thực hiện một lần cho mỗi mục được cung cấp bởi trình lặp, theo thứ tự được trả về bởi trình lặp.suite is then executed once for each item provided by the iterator, in the order returned by the iterator.
  • Khi các mục bị cạn kiệt, bộ trong mệnh đề
    for item in iterable:   
    suite
    else:
    suite
    1, nếu có, được thực thi và vòng lặp chấm dứt.loop terminates.
Hình ảnh của tác giả

Ví dụ: mệnh đề For-Else

  • Mệnh đề khác được thực hiện khi vòng lặp chấm dứt sau khi kiệt sức của điều đó. is executed when the for loop terminates after the exhaustion of the iterable.
for i in [1,2,3,4,5]:
print (i)
else:
print ("for loop is done")

print ("Outside the for loop")
'''
1
2
3
4
5
for loop is done
Outside the for loop
'''

Điều khoản trong khi sử dụng

Câu lệnh trong khi được sử dụng để thực hiện lặp đi lặp lại miễn là biểu thức là đúng.while statement is used for repeated execution as long as an expression is true.

while expression:
suite
else:
suite
Photo của tác giả

Điều này liên tục kiểm tra biểu thức và, nếu đó là sự thật, thực hiện bộ đầu tiên; Nếu biểu thức là sai (có thể là lần đầu tiên nó được kiểm tra), bộ của mệnh đề

for item in iterable:   
suite
else:
suite
1, nếu có, sẽ được thực thi và vòng lặp chấm dứt.

Ví dụ 9: Sử dụng mệnh đề khác trong vòng lặp.

  • trong khi vòng lặp được thực thi cho đến khi điều kiện
    for item in iterable:   
    suite
    else:
    suite
    4 là sai.
    is executed until the condition
    for item in iterable:   
    suite
    else:
    suite
    4 is False.
  • mệnh đề khác được thực thi sau khi điều kiện là sai. is executed after the condition is False.
i=0
while i<5:
print (i)
i+=1
else:
print ("Element is greater than 5")
'''
Output:
0
1
2
3
4
Element is greater than 5
'''

Điều khoản cố gắng

Câu lệnh

if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
7 chỉ định trình xử lý ngoại lệ và/hoặc mã dọn dẹp cho một nhóm các câu lệnh:

Syntax:

try:
suite
except Exception:
suite
else:
suite
finally:
suite

Hãy thử: Trong khối thử, mã được thực thi và nếu một ngoại lệ được nâng lên, điều khiển sẽ đi đến mệnh đề ngoại trừ. Nếu không có ngoại lệ nào được nâng lên, không có người xử lý ngoại lệ nào được thực thi.
In the try block, code is executed and if an exception is raised, control goes to the except clause. If no exception is raised, no exception handler is executed.

ngoại trừ: mệnh đề ngoại trừ chỉ định một hoặc nhiều trình xử lý ngoại lệ. Khối này chỉ được thực thi nếu một ngoại lệ được nêu trong khối thử.
The except clause(s) specify one or more exception handlers.
This block is executed only if an exception is raised in the try block.

Khác: Điều khoản ____ 31 được thực thi nếu luồng điều khiển rời khỏi bộ thử, không có ngoại lệ nào được nêu ra và không có tuyên bố quay lại, tiếp tục hoặc phá vỡ được thực thi.

for item in iterable:   
suite
else:
suite
1 clause is executed if the control flow leaves the try suite, no exception was raised, and no return, continue, or break statement was executed.

Cuối cùng: ________ 37 Chỉ định một trình xử lý ‘Dọn dẹp. Khối này luôn được thực thi ngay cả khi có một ngoại lệ được nêu trong khối thử hay không.

for item in iterable:   
suite
else:
suite
7 specifies a ‘cleanup’ handler.
This block is always executed even if there is an exception raised in the try block or not.

Ngay cả khi một câu lệnh trả lại, phá vỡ hoặc tiếp tục được thực thi trong bộ thử của câu lệnh ____ ____ 17 ____ ____ 37, điều khoản cuối cùng cũng được thực hiện trên đường ra.return, break or continue statement is executed in the try suite of a

if assignment_expression:
suite
elif assignment_expression:
suite
else:
suite
7…
for item in iterable:   
suite
else:
suite
7 statement, the finally clause is also executed ‘on the way out.’

Hình ảnh của tác giả

Ví dụ: mệnh đề For-Else

Mệnh đề khác được thực hiện khi vòng lặp chấm dứt sau khi kiệt sức của điều đó.

def add(a,b):
try:
result=a+b
except Exception as e:
print (e)
else:
print (result)
finally:
print ("Executed finally")

add(4,5)
'''
Output:
9
Executed finally
'''

Điều khoản trong khi sử dụng

Câu lệnh trong khi được sử dụng để thực hiện lặp đi lặp lại miễn là biểu thức là đúng.

def add(a,b):
try:
result=a+b
except Exception as e:
print (e)
else:
print (result)
finally:
print ("Executed finally")

add({'a':1},{'b':2})
'''
Output:
unsupported operand type(s) for +: 'dict' and 'dict'
Executed finally
'''

Conclusion:

  • while expression:
    suite
    else:
    suite
    Photo của tác giả
  • Chỉ có một bộ sẽ được thực thi trong câu lệnh IF. [Hoặc If-suite hoặc elif-suite hoặc duite khác).if statement.[either if-suite or elif-suite or else-suite).
  • mệnh đề khác được thực thi nếu không có ngoại lệ nào được nêu trong khối thử
  • mệnh đề khác trong vòng lặp được thực thi khi vòng lặp chấm dứt thông qua sự cạn kiệt của điều đó và không bị chấm dứt bởi một tuyên bố phá vỡ.break statement.
  • Trong vòng lặp trong khi, mệnh đề khác được thực thi sau điều kiện là sai. Nếu câu lệnh
    for i in [1,2,3,4,5]:
    print (i)
    else:
    print ("for loop is done")

    print ("Outside the for loop")
    '''
    1
    2
    3
    4
    5
    for loop is done
    Outside the for loop
    '''

    4 được thực thi trong bộ
    for i in [1,2,3,4,5]:
    print (i)
    else:
    print ("for loop is done")

    print ("Outside the for loop")
    '''
    1
    2
    3
    4
    5
    for loop is done
    Outside the for loop
    '''

    5, nó sẽ chấm dứt vòng lặp mà không cần thực thi bộ điều khoản
    for item in iterable:   
    suite
    else:
    suite
    1.else clause is executed after the condition is False. if
    for i in [1,2,3,4,5]:
    print (i)
    else:
    print ("for loop is done")

    print ("Outside the for loop")
    '''
    1
    2
    3
    4
    5
    for loop is done
    Outside the for loop
    '''

    4 statement is executed in the
    for i in [1,2,3,4,5]:
    print (i)
    else:
    print ("for loop is done")

    print ("Outside the for loop")
    '''
    1
    2
    3
    4
    5
    for loop is done
    Outside the for loop
    '''

    5 suite, it will terminate the loop without executing the
    for item in iterable:   
    suite
    else:
    suite
    1 clause’s suite.

Xem không gian này để biết thêm các bài viết về Python và Datascience. Nếu bạn thích đọc thêm các hướng dẫn của tôi, hãy theo dõi tôi trên Medium, LinkedIn, Twitter.Medium, LinkedIn, Twitter.

Cảm ơn vì đã đọc!

Tại sao chúng ta sử dụng khác trong Python?

Câu lệnh IF-Else được sử dụng để thực thi cả phần thực và phần sai của một điều kiện nhất định.Nếu điều kiện là đúng, mã khối được thực thi và nếu điều kiện là sai, mã khối khác được thực thi.to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.

Việc sử dụng khác trong một vòng lặp là gì?

Khối khác chỉ sau khi/trong khi chỉ được thực thi khi vòng lặp không bị chấm dứt bởi một câu lệnh break.Khối khác được thực thi trong Python 3 dưới đây.executed only when the loop is NOT terminated by a break statement. Else block is executed in below Python 3.

Hai loại điều khoản khác trong Python là gì?

Hai loại mệnh đề Python khác là: Tôi khác trong câu lệnh IF II khác trong câu lệnh LOOP, mệnh đề khác của câu lệnh IF được thực thi khi điều kiện của câu lệnh IF dẫn đến sai.else in an if statement ii else in a loop statement The else clause of an if statement is executed when the condition of the if statement results in false.

Điều gì là cho người khác và trong khi khác trong Python?

Điều gì là cho người khác và trong khi khác trong Python?For-Else và While là các tính năng hữu ích được cung cấp bởi Python.Nói một cách đơn giản, bạn có thể sử dụng khối khác ngay sau khi và trong khi vòng lặp.Khối khác sẽ chỉ được thực thi nếu vòng lặp không bị chấm dứt bởi một câu lệnh break.useful features provided by Python. In simple words, you can use the else block just after the for and while loop. Else block will be executed only if the loop isn't terminated by a break statement.