8.2. else Statement

Like other languages, Python features an else statement that can be paired with an if statement. The else statement identifies a block of code to be executed if the conditional expression of the if statement resolves to a false Boolean value. The syntax is what you expect:

						if
						expression:
						expr_true_suite
						else:
						expr_false_suite
					

Now the obligatory usage example:

						if passwd == user.passwd:
    ret_str = "password accepted"
    id = user.id
    valid = 1
else:
    ret_str = "invalid password entered… try again!"
    valid = 0

8.2.1. Dangling else Avoidance

Python's design of using indentation rather than braces for code block delimitation not only helps to enforce code correctness, but it even aids implicitly in avoiding potential problems in ...

Get Core Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.