Where to put comments in an if...elif..else construct?

PythonProgramming

You can put comments anywhere in an if...elif...else statement, ie before each of these blocks or within each of these blocks. Note that you cannot put multiline comments before elif and else blocks though, as these comments are actually strings which imply a break in the whole construct. For example,

# If check
if True:
   # Another Comment style
   print("If block")
# Else if statement
elif False:
   # Another Comment style
   print("elif block")
# Else
else:
   # Another Comment style
   print("Else block")

This will give the output:

If block
raja
Updated on 17-Jun-2020 12:02:42

Advertisements