Search code examples
pythoncomments

How to write a comment for every term of a sum?


I'm a Python newbie.

I'd like to make my code well commented.
For example, in C I can naturally write single-line comment for every term of an expression:

int hello_world_length =
   5 + // length of "Hello"
   1 + // space between words
   5;  // length of "World"

How can I write the same statement in Python?

The code I've tried:

hello_world_length =
5 + # length of "Hello"
1 + # space between words
5   # length of "World"
hello_world_length =
   5 + # length of "Hello"
   1 + # space between words
   5   # length of "World"
hello_world_length =        \
5 + # length of "Hello"     \
1 + # space between words   \
5   # length of "World"

Neither of the above works.
I've read a Python tutorial but didn't find a solution.


Solution

  • Just wrap the right-hand side in parens. 4-space indentation would be common:

    hello_world_length = (
        5 + # length of "Hello"
        1 + # space between words
        5   # length of "World"
    )
    
    print(hello_world_length)
    11