Getting started Python using Visual Studio Code
Check Python is already installed or not
- To check if you have python installed on a Windows PC. Go to the Terminal and type
python --version
- If Python is installed, you will see the version of Python installed.
- Otherwise, install python using the following steps.
Installing Python
- Download and install Python from the official website: https://www.python.org/downloads/
- Check python version using the following command in the terminal.
python --version
Sample Output:
Python 3.12.0
- Install the Python extension for Visual Studio Code: https://marketplace.visual studio.com/items?itemName=ms-python.python
Setting up the project
- Open Visual Studio Code
- Click on the “File” menu and select “New File”
- Save the file with the extension “.py”. For example - fibonacc.py
- Write your Python code in the file. Here I write the following code.
# The function fib(n) is defined. This function generates the Fibonacci sequence up to the nth term.
# The function takes an integer n as input and prints the first n Fibonacci numbers separated by spaces.
def fib(n):
a = 0
b = 1
if n == 1:
print(a)
else:
print(a)
print(b)
# Run a loop from 2 to n
for i in range(2,n):
c = a+b
a = b
b = c
print(c)
n = int(input("Enter the number of fibonacci numbers you want to print "))
fib(n)
- Save the file
- Open the command palette (Ctrl+Shift+P)
- Type “Python: Create Terminal”
- Click on the terminal that opens
- Type
python fibonacci.py
in the terminal. You will see the ouput.
Or.
- Run the application using
Run
button on upper right corner of Visual Studio Code.
Or.
- Set a debug point and click Debug python file using upper right corner of Visual Studio Code.