Presentation is loading. Please wait.

Presentation is loading. Please wait.

The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.

Similar presentations


Presentation on theme: "The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use."— Presentation transcript:

1 The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use just if statements. There are 3 conditional commands: There are 3 conditional commands: –if –elif –else

2 Syntax if expression: statement elif expression: statement statementelse:statement

3 Example x = 5 if x < 0: print "x is negative" print "x is negative" elif x % 2: print " x is positive and odd" print " x is positive and odd"else: print "x is even and non-negative" print "x is even and non-negative"

4 To check if something is true the only thing that needs to be done is To check if something is true the only thing that needs to be done is if x: Nothing else is needed Nothing else is needed

5 True and False can be used to define boolean variables True and False can be used to define boolean variables All of the normal conditional operators are present: “ ”, “==“, “!=“, “ =“ All of the normal conditional operators are present: “ ”, “==“, “!=“, “ =“ And, Or and XOr comparisons are used as well: And is a single &, Or is a single |, and XOr is a single ^. And, Or and XOr comparisons are used as well: And is a single &, Or is a single |, and XOr is a single ^. In a chain of & statements, if the first comparison is false then the second comparison is not conducted. In a chain of & statements, if the first comparison is false then the second comparison is not conducted.


Download ppt "The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use."

Similar presentations


Ads by Google