Getting Started with Python

Source: Internet
Author: User
Tags virtual environment install matplotlib

Getting started with Python

Let's start by creating the simplest "Hello world" python application.

Prerequisites

To successfully complete this tutorial, you must do the following:

    1. installation Python extensions .

    2. install any version of Python that you want to use. options include:

    • Built-in Python installation on Linux.
    • Install Python on MacOS by installing home-made software using MacOS brew install python3 (not supported by the system).
    • download from python.org .
    • from Anaconda Download (for data science purposes).
    1. from the VS code, use Python with the Python version selected : select the Command command panel for the interpreter (?? P, or by using) to select a python environment . If the status bar is available options:

      This command lists the list of available interpreters that the VS code can automatically find. If you do not see the interpreter you want, see Configure a python environment .

Create a folder and source code file

Create an empty folder named "Hello", navigate to the folder??, and then code in this folder ( . ) , open vs Code ():

mkdir hellocd hellocode .

On the File Explorer toolbar, press the new File button:

Name the file hello.py and automatically open it in the editor:

by using .py file extension, VS code interprets this file as Python and uses the Python extension to evaluate the content.

Next, start entering the following source code:

"Hello World"print(msg)

When you start typing, print , note IntelliSense How to render auto-complete options.

IntelliSense and AutoComplete are available for standard Python modules and other packages that you have installed into a python environment. It also provides completion for the methods provided by the object type. For example, because the msg variable contains a string, IntelliSense provides a string method, and then you type msg. :

feel free to try IntelliSense, and then revert the changes so that only msg variables and print call, and save the file ( ? S).

for complete details on editing, formatting, and refactoring, see Editing code . Python extensions also fully support linting.

Run Hello world

hello.pyusing Python very simple to run . from external terminal input python3 hello.py (macos/linux) or python hello.py (Windows, assuming Python is in path). Either way, you should see "Hello world" as the output.

you can also Use view> Integrated Terminal ( with the anti-quote character " ^ ") open vs Code The integration terminal remains in the context of VS code. then you can run it directly : python hello.py

Debugging Hello World

Now let's try to debug our simple Hello World application.

First, hello.py by placing the cursor in the print in the call and press F9 to set Breakpoints . or, just click the gutter to the left of the editor next to the number. a red circle appears in the sink.

Next, select the Debug view in the sidebar:

Now you need to launch.json Configure Python by selecting the Settings icon on the Debug toolbar :

The command automatically creates a launch.json A configuration that contains multiple Python configurations, which are displayed in the Configuration drop-down list:

These different configurations are fully explained in the commissioning ; now, just select "Python: Current File", which runs the current file using the current Python environment.

If you want the debugger to stop on the first line automatically when the program starts, use the following stopOnEntry settings are added to the configuration:

"stopOnEntry": true

by selecting the green arrow in the Debug toolbar or by pressing the F5 Run the debugger . because stopOnEntry set to True, the debugger stops on the first line of the file. If you View The local variables window here, you will see that only the automatic Dunder variable is defined:

Also note that a debug toolbar appears that contains commands for running, stepping, restarting, and stopping programs, and the status bar turns orange to indicate debug mode. the terminal also appears automatically in the lower right pane.

Select the green arrow to continue running the program ( F5), the debugger stops at the breakpoint. The variables defined now msg appear in the local pane, which you can use in the debug console (In the lower right pane instead of the terminal selection):

Select the green arrow again to run the program complete. "Hello World" appears in the debug console, and once the program is complete, VS code exits debug mode.

for complete details, see debugging .

Tip : Although the debug console can be used well for output, it is currently unable to input raw_input get input from a Python program through or through a function . In these cases, you need to use an external terminal to run the debugger. This is easily done by selecting the python:terminal (external) Debug Configuration:

Install packages

Now let's use Matplotlib and NumPy to run a more interesting example.

return to the Explorer , create a new file named standardplot.py , and paste it into the following source code:

import matplotlib.pyplot as pltimport matplotlib as mplimport numpy as npx = np.linspace(0, 20, 100)plt.plot(x, np.sin(x))plt.show()

try running the file in the debugger as described in the previous section. if you run the program to completion, Matplotlib and numpy may fail if they are not installed in the current environment.

this is easy to solve. go to terminal and enter pip3 install matplotlib (mac/linux) or pip install matplotlib (Windows), VS Code installs the package and its dependencies, including NumPy, into your project. Note that if you do not want to install matplotlib and its dependencies globally, use a virtual environment .

Now rerun the program, a graphics window appears and the output is displayed:

You can configure VS code to use any Python environment that you have installed, including virtual environments. You can also use a separate environment for debugging. for complete details, see Environment .

Next Step

There are a number of ways to explore using Python in Visual Studio code:

    • Python environment -controls which Python interpreter is used for editing and debugging.
    • Edit Code -Learn about Python autocomplete, IntelliSense, formatting, and refactoring.
    • Linting -Enable, configure and apply various Python linters.
    • Debug -Learn to debug Python locally and remotely.
    • Unit tests -Configures the unit test environment and discovers, runs, and debugs tests.
    • Settings Reference -Explore all the Python-related settings in VS code.

Getting Started with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.