If you have the Python extension, you can use the Python Interactive feature (this is a IPython/Jupyter console, which can run parts of your code as 'cells', i.e., snippets of code executed in one go).
This article writes about the interactive window and many more possibilities of using Jupyter in Visual Studio Code: Python Interactive window
Best option
When editing Python code, select something and press Shift + Enter. This will open the interactive window and run your selection as a single cell.
I have personally been annoyed many times by accidentally pressing Shift + Enter instead of Enter on a line of code which (alone) would be a syntax error. Then, one day I decided to start using this feature consciously.
Terminal fallback
Press: Ctrl + `
This opens a system shell (Bash, or on Windows: PowerShell or cmd.exe).
Run python
, or ipython
if you have it (pip install ipython
), and use the interactive window. In the terminal, you can also execute your script, by typing python myscript.py arguments
.