3.6 if
…else
and if
…elif
…else
Statements
The if
…else
statement performs different suites, based on whether a condition is True
or False
. The pseudocode below displays 'Passed' if the student’s grade is greater than or equal to 60; otherwise, it displays 'Failed':
If student’s grade is greater than or equal to 60 Display 'Passed' Else Display 'Failed'
In either case, the next pseudocode statement in sequence after the entire If…Else is “performed.” We indent both the If and Else suites, and by the same amount. Let’s create and initialize (that is, give a starting value to) the variable grade
, then show and execute the Python if
…else
statement for the preceding pseudocode:
In [1]: grade = 85
In [2]: if grade >= 60:
...: print( ...
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud 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.