DEV Community

oladejo abdullahi
oladejo abdullahi

Posted on

multiline comment in python

Comments

A comment is a message to someone reading your program. Comments are often used to describe what a section of code does or how it works, especially with tricky sections of code. Comments
have no effect on your program i.e it will not run while running the codes. there are two type of comment

1.Single-line comments:

this is comment that is not more than a line. For a single-line comment, use the # character.

Example1

# a slightly sneaky way to write condition statement
x=5
print('x is greater than 3') if x>3 else print('x is not greater than 3')
Enter fullscreen mode Exit fullscreen mode

OUTPUT

x is greater than 3
Enter fullscreen mode Exit fullscreen mode

You can also put comments at the end of a line of your code:

Example2

score=60
if score>=50: #check if score is greater than 50
    print('pass') #print 'psdd' if true
else: #if it is not greater than 50
    print('fail')#print 'fail'
Enter fullscreen mode Exit fullscreen mode

OUTPUT

pass
Enter fullscreen mode Exit fullscreen mode

2.Multi-line comments:

this is used for comments that span several lines, you can use triple quotes.

Example3

""" Program name: Hello world
Author: Maxwizard
Date: 27/12/2020 
"""
print('Hello world')
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Hello world
Enter fullscreen mode Exit fullscreen mode

One nice use for the triple quotes is to comment out parts of your code. sometimes you will want to
modify your program but don’t want to delete your old code in case your changes don’t work. so the best way is to just comment out the old codes so that it is still there if you need it, and it will be ignored when
your new program is run.

Example4

'''
print('this is my old codes')
print('this and the first line are not going to execute.')
'''
print('This is my new codes it will execute')
Enter fullscreen mode Exit fullscreen mode

OUTPUT

This is my new codes it will execute
Enter fullscreen mode Exit fullscreen mode

Example5

'''
x='hello'
y='myfriend'
print(x+y)
'''
print('Hello mate!')
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Hello mate!
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
paddy3118 profile image
Paddy3118

Unfortunately you have it wrong. Python has a multi-line string that can be misused as you describe, but it is best to not use them on code you want to keep.
It makes source harder to read and an automated linter may correctly flag it as a statement that has no effect and so should be removed.
If you really want it as a comment then comment each line individually (your editor might even make easy).
For throw-away code, then who cares, it won't be kept, yes?

Collapse
 
maxwizardth profile image
oladejo abdullahi

thanks for your contribution,I get it! but actually if you try to understand my description well I am not saying one should use it to keep a codes for so long. but sometimes you might not really satisfied with the codes you have written and not sure if the other way you think will work or not then you can comment out the unsatisfied codes and start a new one. if new one worked then you can totally remove the old codes otherwise remove the new code and don't comment out the old code again.