0 points

For-Else statement


dashboard

Tue Apr 21 2020 05:48:50 GMT+0000 (UTC)

Posted by @Honey Girl #python #python #loops #forloop #forelse

1.# For-Else Syntax

2.for item in seq:
    3.statement 1
    4.statement 2
    5.if <cond>:
        6.break
7.Else:
8.Example:
     9.birds = ['Belle', 'Coco', 'Juniper', 'Lilly', 'Snow']
10.ignoreElse = False


11.for theBird in birds:
    12.print(theBird )
    13.if ignoreElse and theBird is 'Snow':
        14.break
15.else:
    16.print("No birds left.")
                              
                                
content_copy Copy

The above code will print the names of all birds plus the message of "else" part.