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


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 17-Jun-2020

595 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements