How to Get Started with Python with Visual Studio Code
Table of Contents
- Introduction
- Prerequisites
- Install the Python Extension via Visual Studio Code
- Create a Python File
- Python Version and Which Version Should I Use?
- How to Download the Latest Python
- How to Run a Python File
- Debug Python File
- Unpack a Collection
- Python's Built-in Data Types
- Multiline String in Python
- Create a Function in Python
- What's PIP
- PyPI
- Try-Except Like JavaScript or C#
- Throw Exception
- 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.
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.