
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- How to optimize nested if...elif...else in Python?
- How to use #if..#elif…#else…#endif directives in C#?
- What is the syntax of Python if...elif...else statement?
- How to put comments inside a Python dictionary?
- How to indent an if...else statement in Python?
- How to put multi-line comments inside a Python dict()?
- How to write an if-else statement in a JSP page?
- What is the ‘if...else if...else’ statement in Java and how to use it?
- How can we put comments in a column of existing MySQL table?
- How to implement MySQL ORDER BY x where (x=col3 if col3!=null, else x=col2)?
- If-Else in Dart Programming
- PHP if else elseif
- Java if-else statement
- Java if-else-if ladder statement
- If-then-else in Lua Programming

Advertisements