![]() If you found this article helpful, consider sharing it with your friends and family. This is because docstrings are meant for documentation, and not for commenting out code. Still, you should generally stick to using regular Python comments using a hash ( #), even if you have to use it for multiple lines. Since there's no built-in support for multi-line comments in Python, this article demonstrates how you can use docstrings as a workaround. So your IDE will throw the error " IndentationError: expected an indented block". If you use 4 spaces (or a tab) for indentation, you will get an indentation error.įor example, this will work: def addNumbers(num1, num2, num3):īut this won't work: def addNumbers(num1, num2, num3): NB: One thing to note is that while using doctsrings for commenting, indentation still matters. This is a multi-line comment with docstrings If you use a docstring to comment out multiple line of code in Python, that block of code will be ignored, and only the lines outside the docstring will run. The real workaround for making multi-line comments in Python is by using docstrings. With this approach, you're technically making multiple single-line comments. To comment out multiple lines in Python, you can prepend each line with a hash ( #). See all options you can pass to plt.text here: valid keyword args for plt.txt. Unlike other programming languages such as JavaScript, Java, and C++ which use /*.*/ for multi-line comments, there's no built-in mechanism for multi-line comments in Python. Add text to plot Add labels to line plots Add labels to bar plots Add labels to points in scatter plots Add text to axes Used matplotlib version 3.x. How to Make Multi-line Comments in Python # print("Hello world")Īs you can see, the commented line wasn't printed in the output. ![]() ![]() ![]() To make single-line comments in Python, prepend each line with a hash ( #). How to Make Single Line Comments in Python So in this article, I won't just show you how to make single-line comments in Python – I'll also show you the workaround for making multi-line comments. The problem is that Python doesn't have a built-in mechanism for multi-line comments. Just like other programming languages, Python supports comments. With comments, you get a better understanding of your own code, make it more readable, and can help team members understand how it works.Ĭomments are ignored by compilers and interpreters, so they don’t run.Īpart from making your code more readable, comments can also help while you're debugging – if you have two lines of code, you can comment out one to prevent it from running. Commenting is an integral part of every programming language. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |