How to Get Start with Python with Visual Studio Code

2023/3/42 min read
bookmark this
Responsive image

PreRequirement

  • macOS, this blog's tools are tested via macOS, most of the case windows should works.
  • already installed Visual Studio Code at macOS.

Install the Python Extension via Visual Studio

At this moment this extension has around 87m download, include lots features, such as intellisense, linting, debugging and code formatting for python.

Create a Python File

create a python file as hello.py, and enter below content.

print('hi')

Python version and Which version should I use?

check Python version

python --version

How to download latest Python

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

How to run Python file

run this should expect to see hi as output.

python hello.py

Or run *python to start with Python command window, and start typing python text to verify.

python

Debug Python File

Unpack a Collection

With Unpack you can do below things, what scenario can be use this? you have to declare same length of variable for the collection.

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

Python's Build-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 Python package manager, use to instal new package.

Run this command to check which versino of pip is downloaded.

pip --version

Run pip list to list all the package at the system.

pip list

https://pypi.org/

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

Do Try Catch like Javascript or C#

Python can do test the code and catch the error by doing below ways.

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

raise Exception("something wrong!")