If you want to use Visual Studio code to create and run Python programs, this tutorial will definitely help you a lot.
The default Python IDLE app is a complicated one for beginners. But don't worry. There are a lot of third-party Python IDEs available, like Pycharm community edition, Sublime Text, PyDev, etc.
VS Code is one of the best, free, and popular Python IDLEs to create and run Python apps on your desktop.
So, How do I set up Visual Studio Code for Python?
Example:-
print("Good Morning")
6. Now click the play button on the top right corner of the VS code application.
7. It will print the “Good Morning” message in the VS code terminal.
8. You can use VS code terminal like Windows command prompt and install any Python packages if you want using pip command.
.If you are using Visual Studio code to write a Python application, you may think it does not offer many tools. VS code is not built to create Python applications. But if you want the freedom to customize & a lightweight IDE, you can choose VS code.
Here I am listing some shortcut keys, and setups to write your code faster in Visual Studio code.
Tip1: Turn selected code into a comment (VS Code Python – Single line comment)
Ctrl+/ – To comment & uncomment multiple lines
Write a multiline comment with a shortcut key
To comment on multiple lines in a Python file,
Select the code lines and press the Shift + Alt + A shortcut key.
That's it you will get multi-line comments like this
""" excel = client.Dispatch("Excel.Application") sheets = excel.Workbooks.Open(r'D:\Chromedriver\output.xlsx') work_sheets = sheets.Worksheets[0] """
Tip 2: Change indentation from 2 spaces to 4 spaces for Python file only
When you write code like function, loop, if-else statement, etc. You have to use 4-space indentation. But by default, Visual Studio Code applies 2 2-space indent when you press the Tab key.
To change it to 4 spaces, go to VS code Extensions.
Search and install the EditorConfig for the VS Code plugin in the VS code editor.
Now in VS Code workspace, select your Python project folder -> right click and choose “Generate .editorconfig”.
It will create .editorconfig file in that folder.
Now add the following line in .editorconfig file.
[*.py] indent_style = space indent_size = 4
Save the file.
Now in your python file, select a particular line of code you want to indent and press the Tab key on your keyboard. That's it. It will apply 4 spaces for the indentation.
for conten in content: title = conten.get_text() content_title.append(title)
Tip 3: Set keyboard shortcut to run python file in VS code Terminal
Extra Tip: Kite Plugin for VS Code
Visual Studio Code offers Code Autocomplete and IntelliSense. But Kite, a third-party plugin for VS Code, gives a super-fast auto-complete experience.
Multi-line Editing
You can do more things in VS Code when you create Python apps like,
VS Code Tips: If you are Creating flask-based backend Web Apps
Remove all CSS classes at once
Install Class Killer VS code extension.
To use this extension, press ‘Ctrl +Shift +P”.
Search “Remove all classes – Class Killer”. Apply this option. It will remove all CSS classes.
Remove All comments at once
Install Remove comments VS code extension. This extension supports multiple programming languages like Python, PHP, etc.
To use this extension, press ‘Ctrl +Shift +P”.
Search “Comments: Remove All Comments”. Apply this option. It will remove all comments at once.
© 2024 Webapptiv. All rights reserved.