Comments

Single Line Comments

  1. # this is a single line comment
  2. x = 5
  3. #ans: another comment
  4. y = 10

Inline Comments

  1. # comment after code
  2. x = 5 # inline comment
  3. name = "Alice" # another inline

Multi-line Comments

  1. """
  2. This is a
  3. multi-line comment
  4. or docstring
  5. """
  6. x = 5

Multi-line with Single Quotes

  1. '''
  2. This is also
  3. a multi-line comment
  4. '''
  5. y = 10

Comments Don't Execute

  1. # x = 10
  2. x = 5
  3. #ans: x is 5 (commented line ignored)

Exercises - Part 1

  1. # what is x?
  2. #ans: x = 10
  3. x = 5
  4. #ans: 5
  5. # what gets executed?
  6. x = 5 # x = 10
  7. #ans: x is 5

Exercises - Part 2

  1. # multiline comment?
  2. """
  3. x = 10
  4. """
  5. x = 5
  6. #ans: x is 5

Exercises - Part 3

  1. # can you do this?
  2. x = 5 """comment"""
  3. #ans: SyntaxError
  4. # what about this?
  5. x = 5 # comment
  6. y = x
  7. #ans: y is 5

Exercises - Part 4

  1. # nested comments?
  2. #ans: # this is a comment
  3. #ans: valid, just a comment
  4. # multiline string as comment?
  5. '''
  6. This is also
  7. a comment
  8. '''
  9. #ans: valid if not assigned to anything

Exercises - Part 5

  1. # what is x?
  2. x = 5
  3. #ans: x = 10
  4. #ans: x is 5
  5. # inline multiline?
  6. x = 5 """ this won't work """
  7. #ans: SyntaxError

Exercises - Part 6

  1. # comment with backslash?
  2. x = 5 # this is \
  3. #ans: a comment
  4. #ans: two separate single-line comments

Exercises - Part 7

  1. # doc string vs comment?
  2. def func():
  3. """This is docstring"""
  4. # This is comment
  5. pass
  6. #ans: both valid, different purposes

Google tag (gtag.js)