Python: Selection - If-else Basic selection statement ...

Python: Selection - If-else ? Basic selection statement ? Simplest form syntax:

? Schematically: if Boolean expression: block else: block

? Semantics: ? if Booleanexpression is true, execute block after if ? if Booleanexpression is false, execute block following else: if the else: is included

1

Python: Selection - If-else Blocks ? Python blocks

? A block is a sequence of statements that are part of a control structure ? Syntax:

? Blocks are delimited solely by indentation ? Every statement in a block must be indented by the same amount

This is why it's so important that statements at the top level have no leading white space

? The only thing that is important is that every statement in a block has exactly the same amount of leading white space Four spaces is the usual convention Do not mix and match spaces and tabs!

? Note that the blocks must be indented more than the if and else, and that the if and else must have the same indentation

2

Python: Selection - If-else Examples ? Simple if:

pay = hours * rate if hours > 40:

pay = pay + (hours - 40) * rate * 0.5 print("Pay =", pay) ? Simple if-else example: if hours ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download