0

been trying to wrap entire code in a comment, how do i do that? i tried #, """, with no success, and as a question, is this even possible? i guess im stacking comments on top of other comments but im sure there is a way, im wrapping this code because i want to keep it in one file along with other projects in the same file but i dont want to activate ALL of the code. here's the code i want to wrap as a comment:

"""Artithmetic expressions"""

addition = 1 + 1;
subtraction = 2-1;
miltiplication = 2*2;
division = 5/3; """5/3 = 1"""

"""Variables and Assignment"""

a, b = addition, subtraction; """a = addition, b = subtraction"""

""" prints 2 1"""
print a, b 

"""Strings, indexing strings"""

string1 = "hello world hell"

string2 = string1[2] 
"""prints 1"""
print string2 

"""string extraction"""

string3 = string1[0:5]
""" hello """
print string3 

"""Finding"""

stringfind1 = string1.find("hell", 4)
""" prints 12 """
print stringfind1 






"""Python 2"""
"""If statement"""
if (3 < 10):
print "true"

else:
print "false"


""" Logical Operators"""

if (3 and 4 < 10): 
print "true"
"""may use or"""


"""Loops, ex prints 10 iterations"""
count = 0
while (count < 10):
print 'The count is: ', count
count = count + 1

print "Good bye!"

"""converting between numbers and strings: str(one) converts int to     string"""
"""use 'ord' for string --> int, lastly chr = """
one = 1
convert = str(one) 
if convert == 1:
print "true"

else:
print "false"

'''returns one character string from number input'''
var1 = chr(65)
print var1


"""splitting strings: () prints all words in a string"""
""" ' ', 1   prints all except the last word?"""
string10 = "fucking hell i hate your life"
var2 = string10.split()

print var2
print string10.split(' ', 1)


"""Looping through strings with 'for' loop, ex prints all chars in 'string10' in new lines"""

for fuckoff in string10:
print 'Current letter :', fuckoff
Adan
  • 453
  • 1
  • 10
  • 27
  • 2
    A comment is a line that begins (ignoring leading whitespace) with a `#`, period. The other forms create dead code or strings that are ignored. Any decent editor should allow you to easily prefix each line in a block of code with `#`. – chepner Aug 24 '15 at 16:04
  • im using visual studio 2015 enterprise, gona check if theres a button that does this – Adan Aug 24 '15 at 16:10
  • You _can_ use `"""` to turn a lot of lines into one big string that doesn't do anything, but only if your code doesn't already have any triple quotes inside it. In your case, you'd have to replace every single one of your """comments""" with #comments. – Kevin Aug 24 '15 at 16:11
  • 1
    This doesn't answer your question, but if you write using smallish functions rather than the monolithic code you show, then controlling what gets run is much easier, just comment out the function calls. You mention you have more then one project in a file, well maybe you should put one project in one file and have multiple modules, or at the very least multiple functions. – cdarke Aug 24 '15 at 16:24
  • See the right answer to your question and don't forget to accept it in order to mark this question as answered. – Claudio Nov 17 '22 at 19:21

2 Answers2

1

You can't: Python comments are single line. And docstrings are not comments. However, during development if you need to "switch off" a block of code you can put it into an if False: block.

Eg:

if False:
    addition = 1 + 1;
    subtraction = 2-1;
    miltiplication = 2*2;
    division = 5/3;
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • 1
    This is more work than simply commenting each line, as you need to adjust the indentation of each line to include the body of the `if False` statement. (Using a variable, rather than the constant `False`, is reasonable if you expect to toggle the code on and off during development.) – chepner Aug 24 '15 at 16:06
  • Anecdote: my editor (Notepad++) has an "indent every line" keyboard shortcut, but not a "put a '#' at the beginning of every line" shortcut. So YMMV for which approach is easier. – Kevin Aug 24 '15 at 16:08
  • 1
    @chepner: True, it's a little bit more work than commenting a block of lines, but neither should be that much work in a halfway decent editor. The advantage of doing it with `if` is that it preserves the syntax highlighting, which may be helpful. But I agree that using a variable rather than `False` (or `0`) is a good idea if you want to be able to toggle multiple blocks. – PM 2Ring Aug 24 '15 at 16:09
  • Good point about preserving syntax highlighting as a benefit of `if False` over commenting. – chepner Aug 24 '15 at 16:19
0

After seven years of being not right answered here the right answer to the question above:

You can 'switch off' a block of code using appropriate triple quotes.

It will not always be possible (if the code uses both kinds of triple quoted strings), but in the case of code in the question it is possible to use ''' single quoted triple quoted string to achieve what you are after.

Below how it looks like:

'''
"""Artithmetic expressions"""

addition = 1 + 1;
subtraction = 2-1;
miltiplication = 2*2;
division = 5/3; """5/3 = 1"""

"""Variables and Assignment"""

a, b = addition, subtraction; """a = addition, b = subtraction"""

""" prints 2 1"""
print a, b 

"""Strings, indexing strings"""

string1 = "hello world hell"

string2 = string1[2] 
"""prints 1"""
print string2 

"""string extraction"""

string3 = string1[0:5]
""" hello """
print string3 

"""Finding"""

stringfind1 = string1.find("hell", 4)
""" prints 12 """
print stringfind1 






"""Python 2"""
"""If statement"""
if (3 < 10):
print "true"

else:
print "false"


""" Logical Operators"""

if (3 and 4 < 10): 
print "true"
"""may use or"""


"""Loops, ex prints 10 iterations"""
count = 0
while (count < 10):
print 'The count is: ', count
count = count + 1

print "Good bye!"

"""converting between numbers and strings: str(one) converts int to     string"""
"""use 'ord' for string --> int, lastly chr = """
one = 1
convert = str(one) 
if convert == 1:
print "true"

else:
print "false"

'''returns one character string from number input'''
var1 = chr(65)
print var1


"""splitting strings: () prints all words in a string"""
""" ' ', 1   prints all except the last word?"""
string10 = "fucking hell i hate your life"
var2 = string10.split()

print var2
print string10.split(' ', 1)


"""Looping through strings with 'for' loop, ex prints all chars in 'string10' in new lines"""

for fuckoff in string10:
print 'Current letter :', fuckoff
'''

You can see the evidence that it works as expected from the kind of highlighting of the above piece of code in its code textbox.

Claudio
  • 7,474
  • 3
  • 18
  • 48
  • What you can also do with triple quotes combined with line comments see here: https://stackoverflow.com/q/75695010/7711283 (miracle-SWITCH). As I experienced this switch by chance for the first time I was really surprised what it does and needed a bit time to understand how and why it works. – Claudio Mar 12 '23 at 22:57