How to comment multiple lines of code in visual studios - python

I'm very new to coding so I decided to try Python and downloaded Visual Studio, but I am not sure how you can comment out multiple lines of code.

Normally in Visual Studio Community 2019, multi line comments are done by using CTRL+K CTRL+C and CTRL+K CTRL+U to remove comments. But this is unavailable when using python for some reason.
Personally, for learning to write Python I would recommend using Visual Studio Code, instead of Visual Studio. VS Code is a much more light weight package (install size approx 300MB compared to several GB) This means that it opens in a fraction of the time.
In VS Code the Python extension will provide all of your python needs and allow you to toggle block comments by using CTRL+/

In python, you can use triple quotes to comment multiple lines of code. Example:
"""
comment
still comment
"""
print('Hello world!')
The text in the quotes is commented, but the print statement below it is not and will work.

there is a short cut for most of the languages in VS code :
Ctrl+/
edit: you need to select the lines that you intend to comment out

to comment out multiple lines in python you can use triple quotes like this:
"""
this is line 1 of my comment
and this is line 2 of my comment
this is link 3
"""
you get the point as long as there are triple quotes at the beginning of the first line and at the end of the last line

Ctrl + Shift + A did the job for me. It will put you """ at the beginning and at the end of your selected text.

In my case, I use the shortcut key ctrl + k and ctrl + c to comment. To uncomment just hit the ctrl + k and ctrl + u

Clicking the edit button in Visual Studio at the bottom is how you can comment lines out.
"toggle line comment" for me done with ctrl + ',
but could be something else for you
"toggle block comment" for me is done with shift + alt + A,
but could be something else for you
In both cases does the first click comment and clicking again uncomments

Related

Keyboard shortcut to comment code in Thonny IDE

Whats is the keyboard short cut to comment single line of code and slsected lines of code in Thony IDE for python?
Ie, The Thony equivalent of ctrl + / in VS Code
Single line comment.Ctrl + 1.
Multi-line comment select the lines to be commented. Ctrl + 4.
Unblock Multi-line comment. Ctrl + 5.
To comment single line:
Put a cursor on the line which you want to comment. Press Alt+3 to comment, Alt+4 to un-comment.
To comment single line:
Select the desired lines of code to comment. Follow same keys i.e. press Alt+3 to comment, Alt+4 to un-comment.
Note:
For further shortcuts, please click on Edit menu option next to File and you'd see various shortcuts available in Thonny IDE.
Use Ctrl+3 for toggling between "commented" and "uncommented".
Use Alt+3 to comment.
Use Alt+4 to uncommment.
(Screenshot)
If you want to change the default shortcuts, you can follow the instructions on this official github-page:
https://github.com/thonny/thonny/wiki/Custom-shortcuts
With the latest update of Thonny IDE you can do
ctrl + 3
for both single line and multiline commenting and uncommenting.

comment python code in visual studio code

My visual studio code comment python code with ''' instead of using # when try to comment a block of code with the key combination ctrl + shift + a. I have ubuntu 16.04
For VS Code and Python ,
Select the block of code
For Commenting press CTRL + k + c
For Un comment press CTRL + k + u
Under Windows environment this work for me :
select block text
press CTRL + /
This has nothing specifically to to with Visual Studio, but a result of the python commenting styles. Generally, single line comments are done with the pound (or hash) symbol:
# This is a comment
In contrast, three quotation marks (either ''' or """) can be used to easily produce multi-line comments.
'''
This is also a comment.
However, this comment crosses multiple lines
'''
or
"""
This is yet another multiline comment
For long comments the quotation marks are much easier than # comments.
"""
Hope this helps.

how to indent block of python code without using tabs

I am just learning python and need to know how to indent a block of code without using the tab button (because, as I have read, tab should not be used).
Example:
in a simple print function
def test(string):
print(string)
print("'" + string + "'")
test('test')
IF now, I want to put the print functions in an if statement
def test(string):
if len(string) > 2:
print(string)
print("'" + string + "'")
test('test')
How can I indent the two print statements without using the 'tab', or having to click on every line and insert 4 spaces? I am very used to selecting all the lines I need to move to the right and pressing tab regardless of program (geany, ipython, notepad++).
I would like to set off following the PEP8 style guide from the introduction into Python.
My concern is not this particular example, but if I have a code block I want to move left or right that is many more lines.
Thanks,
Ivan
It depends on what text editor you're using. I use Notepad++, which is one of the ones you mention, and it has an option to use spaces in place of tabs. So I just enable that for .py files, then I can indent a block by hitting tab exactly as you're used to (and unindent with shift-tab).
Go to settings > preferences > tab settings, select "python" from the list on the right and check the "replace by space" checkbox. Other text editors that offer the same feature will presumably each have their own way of enabling it, and their own way of making it language-specific.
Be aware that pressing tab to change the indentation of a selection is just a UI convention, albeit a common one. It doesn't work for example in Notepad, where hitting tab while text is highlighted behaves the same as typing anything else: replaces the selection with a tab. If you were using Notepad then I'm pretty sure the answer would be "it's not possible". If you use lots of different editors then I think unfortunately you're going to have to investigate each one in turn.
As you have mentioned, PEP8 recommends four spaces for each level of indentation. Many text editors allow you to set tabs to be replaced by a certain number of spaces. So in many cases it is still ok to use tab to program in python, just make sure that it is replaced by four spaces.
I personally use Sublime Text and there seems to be an option to customize Tabs:
{
"tab_size": 4,
"translate_tabs_to_spaces": true
}
In the Packages/User/Preferences.sublime-settings. Maybe worth trying that.

How to yank an entire block in Vim?

Is it possible to yank an entire block of Python code in Vim?
Be it a def, for, if, etc. block...
You can yank a paragraph with y}. This will not yank all the methods if you have a blank line though.
If you want to yank everything except the { use yi{ (or yi}). If you to include the curly braces use ya{ (or ya}).
The i and a modifiers mean in and all.
To yank a word no matter where in the word you are: yiw
To yank the contents of parentheses: yi); if you want to include them, use ya(
You can do the same for " or ' with yi", ya" or yi' and ya'.
Of course, you're not limited to yanking. You can delete a word with diw or change it with ciw, etc... etc...
The excellent add-on suite Python-mode includes some key commands to navigate classes, methods, and function blocks.
To yank a method: yaM (inner method: yiM)
To yank a class: yaC
There are other handy motions, like moving from function-to-function (]]). See the complete list of keys for more.
There's a vim add-on script python_fn.vim which has, as one of its functions, a key binding to visually select a block of Python code using ]v. You could then yank it with y as normal.
I usually just use visual block mode. Shift-V, move, and 'y'ank the highlighted block. There's only so many shortcuts I can keep in memory at once :)
You can combine a search with yank, so if your function ends with return retval you can type y/return retval
Enter visual line selection by pressing 'V'
When finished selecting the block pres 'y'
Paste it somewhere with 'p' or 'P'
I made a plugin named spacebox which does a Visual selection of all lines with the same or more indentation as the current line.
With Python whitespace being the way it is, you could place your cursor on the line below def or if, and issue the command :SpaceBox to select your "block".
vim-indent-object works pretty well. Worth a shot.
Just fold the class using za and then use visual mode ( V ) to select the collapsed class. This way you don't have to scroll too much. Then just yank with y. When you are done yanking unfold the class with za again.
In .py file, press Esc
Press shift V to enter visual line mode
Highlight with up and down arrow keys
Press d to delete the selected rows
Go to the row you would like to place the lines and press p to paste lines after a row or press shift P to paste before a row
Hope that helps.

How to comment out a block of code in Python [duplicate]

This question already has answers here:
Why doesn't Python have multiline comments?
(18 answers)
Closed 9 years ago.
Is there a mechanism to comment out large blocks of Python code?
Right now, the only ways I can see of commenting out code are to either start every line with a #, or to enclose the code in triple quotes: """.
The problem with these is that inserting # before every line is cumbersome and """ makes the string I want to use as a comment show up in generated documentation.
After reading all comments, the answer seems to be "No".
Python does not have such a mechanism. Prepend a # to each line to block comment. For more information see PEP 8. Most Python IDEs support a mechanism to do the block-commenting-with-hash-signs automatically for you. For example, in IDLE on my machine, it's Alt+3 and Alt+4.
Don't use triple-quotes; as you discovered, this is for documentation strings not block comments, although it has a similar effect. If you're just commenting things out temporarily, this is fine as a temporary measure.
Hide the triple quotes in a context that won't be mistaken for a docstring, eg:
'''
...statements...
''' and None
or:
if False: '''
...statements...
'''
The only cure I know for this is a good editor. Sorry.
The only way you can do this without triple quotes is to add an:
if False:
And then indent all your code. Note that the code will still need to have proper syntax.
Many Python IDEs can add # for you on each selected line, and remove them when un-commenting too. Likewise, if you use vi or Emacs you can create a macro to do this for you for a block of code.
In JetBrains PyCharm on Mac use Command + / to comment/uncomment selected block of code. On Windows, use CTRL + /.
M-x comment-region, in Emacs' Python mode.
At least in VIM you can select the first column of text you want to insert using Block Visual mode (CTRL+V in non-windows VIMs) and then prepend a # before each line using this sequence:
I#<esc>
In Block Visual mode I moves to insert mode with the cursor before the block on its first line. The inserted text is copied before each line in the block.
In vi:
Go to top of block and mark it with letter a.
Go to bottom of block and mark it with letter b
Then do
:'a,'b s!^!#!
comm='''
Junk, or working code
that I need to comment.
'''
You can replace comm by a variable of your choice that is perhaps shorter, easy to touch-type, and you know does not (and will not) occur in your programs. Examples: xxx, oo, null, nil.
In Visual Studio using the Python Tools for Visual Studio, blocks can be commented out by Ctrl+K, Ctrl+C and uncommented by Ctrl+K, Ctrl+U.
I use Notepad++ on a Windows machine, select your code, type CTRL-K. To uncomment you select code and press Ctrl + Shift + K.
Incidentally, Notepad++ works nicely as a Python editor. With auto-completion, code folding, syntax highlighting, and much more. And it's free as in speech and as in beer!
In Eclipse + PyDev, Python block commenting is similar to Eclipse Java block commenting; select the lines you want to comment and use Ctrl + / to comment. To uncomment a commented block, do the same thing.
Yes, there is (depending on your editor). In PyDev (and in Aptana Studio with PyDev):
Ctrl + 4 - comment selected block
Ctrl + 5 - uncomment selected block
The only mechanism to comment out Python code (understood as code ignored by the interpreter) is the #.
As you say, you can also use string literals, that are not ignored by the interpreter, but can be completely irrelevant for the program execution.
In Eclipse using PyDev, you can select a code block and press Ctrl + #.
Triple quotes are OK to me.
You can use ''' foo ''' for docstrings and """ bar """ for comments or vice-versa to make the code more readable.
Another editor-based solution: text "rectangles" in Emacs.
Highlight the code you want to comment out, then C-x-r-t #
To un-comment the code: highlight, then C-x-r-k
I use this all-day, every day. (Assigned to hot-keys, of course.)
This and powerful regex search/replace is the reason I tolerate Emacs's other "eccentricities".
On Eric4 there is an easy way: select a block, type Ctrl+M to comment the whole block or Ctrl+alt+M to uncomment.
Use a nice editor like SciTe, select your code, press Ctrl + Q and done.
If you don't have an editor that supports block comments you can use a triple quoted string at the start and the end of your code block to 'effectively' comment it out. It is not the best practice though.

Categories