Ανακεφαλαίωση Python
- Previous Page Γλώσσα Προγραμματισμού Python
- Next Page Μεταβλητές Python
The comments can be used to explain Python code.
The comments can be used to improve the readability of the code.
When testing code, comments can be used to prevent execution.
Create Comments
Comments are created with #
At the beginning, Python will ignore them:
Example
#This is a comment print("Hello, World!")
Comments can be placed at the end of a line, and Python will ignore the rest of the line:
Example
print("Hello, World!") #This is a comment
Comments do not have to be text that explains the code; they can also be used to prevent Python from executing code:
Example
#print("Hello, World!") print("Cheers, Mate!")
Multi-line Comments
Python actually does not have a syntax for multi-line comments.
To add multi-line comments, you can insert one for each line #
:
Example
#This is a comment #written in #more than just one line print("Hello, World!")
Or, in a way that does not quite meet expectations, you can use multi-line strings.
Since Python will ignore the string literals not assigned to variables, you can add multi-line strings (triple quotes) in the code and add comments within them:
Example
""" This is a comment written in more than just one line """ print("Hello, World!")
As long as the string is not assigned to a variable, Python will read the code, then ignore it, and you have already completed multi-line comments.
- Previous Page Γλώσσα Προγραμματισμού Python
- Next Page Μεταβλητές Python