how do I run only selected lines of my code in vs code

117 views Asked by At

As I am following online course and working on python topics, there is a moment when I write short lines of coding and than another coding all in the same page. Even though I separate them with # comments, there is a need I need to run the specific line of code in terminal window and then my issue raises here. Becouse when running, python run the code from top to the bottom. And I don't want that. All I want while I practised def function with an example to only be able to run those three lines and be able to separate from other codes which were previously finished and were let say loop iteration. So is that possible or I have to create for each short code each file.

2

There are 2 answers

0
scotscotmcc On

In vscode, if you hit Shift+Enter, it will run the highlighted code (from your main editor window) in a python terminal.

0
JFCorleone On

For using PyCharm you can have a look here: Pycharm: run only part of my Python file For other use-cases the most popular approach is to use jupyter notebook that has a concept of cells where you can experiment with portions of your code.