CONTROL STATEMENTS
Python provides three selection statements that execute code based on a condition that evaluates to either True or False:
- The if statement performs an action if a condition is True or skips the action if the condition is False.
- The if... else statement performs an action if a condition is True or performs a dif erent action if the condition is False.
- The if... elif... else statement performs one of many different actions, depending on the truth or falsity of several conditions.
Anywhere a single action can be placed, a group of actions can be placed.
Python provides two iteration statements—while and for:
Python provides two iteration statements—while and for:
- The while statement repeats an action (or a group of actions) as long as a condition remains True.
- The for statement repeats an action (or a group of actions) for every item in a sequence of items.
Keywords
The words if, elif, else, while, for, True and False are Python keywords. Using a keyword as an identifier such as a variable name is a syntax error. The following table lists Python’s keywords.
Python keywords
and
as
assert
as
assert
async
await
break
break
class
continue
def
del
elif
else
except
False
finally
for
for
from
global
if
import
in
in
is
lambda
None
nonlocal
not
not
or
pass
raise
return
True
True
try
while
with
yield