How to Get Started with Python with Visual Studio Code

2023/03/043 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Install the Python Extension via Visual Studio Code
  4. Create a Python File
  5. Python Version and Which Version Should I Use?
  6. How to Download the Latest Python
  7. How to Run a Python File
  8. Debug Python File
  9. Unpack a Collection
  10. Python's Built-in Data Types
  11. Multiline String in Python
  12. Create a Function in Python
  13. What's PIP
  14. PyPI
  15. Try-Except Like JavaScript or C#
  16. Throw Exception
  17. Conclusion

Introduction

This blog describes how to get started with Python quickly so you can use Python with your work tomorrow.

Prerequisites

  • macOS — this blog's tools are tested via macOS; in most cases, Windows should work too.
  • Already installed Visual Studio Code on macOS.

Install the Python Extension via Visual Studio Code

At this moment, this extension has around 87m downloads and includes lots of features, such as IntelliSense, linting, debugging, and code formatting for Python.

Create a Python File

Create a Python file as hello.py, and enter the content below.

print('hi')

Python Version and Which Version Should I Use?

Check your Python version:

python --version

How to Download the Latest Python

Go to this page and download the latest version of Python. After downloading the latest version, restart the terminal and try again. Python Download

How to Run a Python File

Run this command and you should expect to see hi as output.

python hello.py

Or run python to start the Python interactive shell, and start typing Python code to verify.

python

Debug Python File

Unpack a Collection

With unpacking, you can do the following. What scenario can this be used in? You have to declare the same number of variables as the length of the collection.

numbers = [1,2,3]
x1,y1,z1 = numbers
print(x1) # 1
print(y1) # 2

Python's Built-in Data Types

  • str
  • int, float, complex
  • list, tuple, range
  • dict
  • set, frozenset
  • bool
  • bytes, bytearray, memoryview
  • NoneType

Multiline String in Python

a = """my test is
more than one line"""
print(a)

Create a Function in Python

def myFunc():
    return True;
print(myFunc())

What's PIP

PIP is a Python package manager, used to install new packages.

Run this command to check which version of PIP is installed.

pip --version

Run pip list to list all the packages on the system.

pip list

PyPI

At pypi.org, you can find and publish Python packages.

https://pypi.org/

Try-Except Like JavaScript or C#

Python can test code and catch errors in the following way.

try:
    print("code to test")
except
    print("error will come to here")
else
    print("this is interesting, no error come to here")
finally
    print("either else and exception come here")

Throw Exception

Conclusion

This blog covered the basics of getting started with Python in Visual Studio Code, including installing the Python extension, running and debugging Python files, understanding data types, using PIP for package management, and handling exceptions. With these fundamentals, you should be ready to start using Python in your daily work.