程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

From novice to expert! Full introduction, hands-on teaching you to turn vs code into a python development artifact!

編輯:Python

Visual Studio Code It's a powerful 、 Extensible and lightweight code editor , After years of development , Has become a Python One of the community's preferred code editors

Next we will learn how to install Visual Studio Code And set it to Python development tool , And how to use it VS Code Improve programming efficiency

Let's do it!

install Visual Studio Code

Next, we will step by step introduce how to macOS Installation on VS Code

because Windows and macOS The essential difference , If the partner is Windows user , Then you need to make some small changes to install VS Code. But in Windows Installation on VS Code It's very simple , It is completely similar to installing other Windows Applications , All the way Next that will do

  1. Download from its official website for macOS or Windows Of Visual Studio Code. The download page will automatically detect our operating system and display a large button , Used to download the latest version of the installer on your computer . without , You can click the down arrow button and select a stable device that matches the operating system installed on our computer VS Code edition

  1. Double click the downloaded file , Extract archived content

  1. take Visual Studio Code The application moves to Application Folder to make it in macOS Available in the launchpad

  1. start-up Visual Studio Code, Then open the Python The folder where the script is located or create a new folder . for example , Create a new folder on our desktop , And named it py_scripts, Then try to VS Code Open this folder on . Generally speaking ,VS Code We need permission to access Desktop Files in folder

Besides , It may also be necessary to declare that our trust is stored in Desktop The author of the file in the folder

  1. Create an extension called .py The new document of . For example, create a new file and name it prog_01.py.VS Code detected .py Extension and want to install Python Expand

To be in VS Code Use in Python, We need to install Python Expand , It brings many useful functions , For example, with code complement 、 debugging 、 Unit test support and other functions

Click on the install

We can also browse the extension to install Python Expand . Click on VS Code Left side Extensions Icon

This will show VS Code market The most popular VS Code Extended list . Now we can choose Python Expand and install it

  1. After installing the extension , We have to choose by hand Python Interpreter , Click Select Python Interpreter

Then select the recommended... From the list Python Interpreter

If our Mac Multiple Python edition , You need to choose here

stay VS Code Create and run Python file

Now we have in VS Code Write and run in Python Everything the code needs , Next let's go to VS Code To write the following code , Then run it

def palindrome(a):
        a = a.upper()
        return a == a[::-1]
name = input("Enter a name: ")
if palindrome(name):
        print("It's a palindrome name.")
else:
        print("It's not a palindrome name.")

By clicking VS Code In the top right corner of the ️ Button to run the code , We can see the corresponding output on the terminal . First ask for the name , Enter a name , Then press enter . It outputs It's a palindrome name. If the name entered is palindrome , Otherwise output It's not a palindrome name..

Palindromes are a sequence of letters , Read the same way before and after , for example Hannah、Anna and Bob

As we can see , All outputs appear in the integrated terminal , Let's talk more about this wonderful function

VS Code By embedding this nice feature into IDE It brings great convenience to developers , Because executing terminal commands is almost an integral part of writing code . To view the terminal , We can do it in macOS or Windows Type... On the machine Ctrl + `, Or use View > Terminal The menu command . Besides , If we want to kill the integrated terminal , You can click... In the upper right corner of the terminal window bin Icon . Technically speaking , The integrated terminal uses the installed on the computer shell — for example ,Windows Upper PowerShell Or command prompt , as well as macOS and Linux Upper bash or zsh

Visual Studio Code Allows us to customize the appearance of the terminal . Open the terminal settings page , Click the down arrow button in the upper right corner of the terminal window , Then select the configure terminal settings option , You can easily customize the font 、 Spacing and cursor style

VS Code Another nice feature of is that we can easily work in multiple shell Switch between , You can even change the default used in the integration terminal shell. Please click the down arrow button in the upper right corner of the terminal window , And then choose Select Default Profile Options

A pre populated list of available shell list , You can select one of them as the default terminal shell. Let's choose bash shell

Create a new terminal by clicking the plus icon in the upper right corner of the terminal window , It will use bash shell, As shown below

Use REPL

VS Code Another very useful function in is to run one or more lines of code , Just select them and choose from the context menu Run Selection/Line in Python Terminal Options

Created above Python In file , Write the following statement

print("Hello, world!")

Then select the statement , And right-click and select Run Selection/Line in Python Terminal Options , As shown below

The output appears in the integrated terminal , But in different forms , be called REPL. Let's take a closer look at REPL And its advantages

REPL For read 、 assessment 、 Print 、 loop . It's a use Python The interactive mode of interpreter and running commands directly in the terminal . stay REPL in , Three right arrow symbols indicate the input line

stay VS Code Start in REPL Another way to do this is as follows : Open the command panel , Search for REPL, And then click Python: Start REPL

An interactive screen will appear Python shell, We can do it in >>> Enter the command... At the prompt , Then just press Enter or return Key to execute them , As shown below

REPL A great feature of is that we can immediately see the result of running the command , So if we want to try some lines of code or try to use API,REPL It's a good way

format Python Code

We should get into the habit of writing code in an appropriate format immediately after we start writing programs ,Python There is a famous Python Code style guide , be called PEP 8, It makes our code easy to read and understand . We can do it in Python Check the style guide on the official website

https://www.python.org/dev/peps/pep-0008/

Next we will learn how to use Autopep8 The package automatically applies the format to our code . This package can be used pip Command to install , Auto format Python Code to match PEP 8 Style guide . without doubt VS Code Support use Autopep8 Package for automatic code formatting

Let's see how to install the package and install it in VS Code Enable it in

First , Execute the following commands in the integration terminal Autopep8 package

pip3 install autopep8

After installation , Turn off the terminal . Now open VS Code Set up , Search for “Python formatting”,Autopep8 Path and Provider All fields need to be filled in as autopep8

The last step is to enable automatic formatting when saving

When we save files , Enabling this feature will result in Python Apply all... On the source file PEP 8 The rules

restructure Python Code

In discussion VS Code Refactoring in Python Before code , Let's first look at the definition of refactoring

Code refactoring is the process of refactoring existing computer code —— Change factorization —— Without changing its external behavior to make it easier to read and maintain —— Wikipedia

Python Extensions provide basic refactoring capabilities , For example, rename the symbol 、 Extraction method 、 Extract variables, etc . for example , To put palindrome() Change the method name to check_palindrome(), Please right-click the method name , And then choose Rename Symbol Options :

Enter a new name in the text box check_palindrome, Then press Enter rename

Now we can see that the relevant names of all palindromes have been changed to check_palindrome

Now let's try extract method function , Create a new Python File and paste the following code into it

height = 5
width = 4
area = height * width
print("Room's area =", area, "square meters")

Select the third line , Right click , Then select the refactoring option from the context menu

And then click Extract method Button , Enter the new name in the text box that appears calc_area, Then press enter to rename

Python Interaction window

A very important function is Visual Studio Code Support use Jupyter Notebooks. To run the current file in an interactive window , You can right-click the file name... In the Explorer pane , Then select from the context menu “ Run the current file in an interactive window ” Options , As shown below

If not already installed Jupyter package , It will display a dialog box and ask to install it

After installation, an interactive window will appear , At this time, you need to enter a name to judge whether it is a palindrome

Last , You can see the results in the interactive window , as follows

Besides , To be in VS Code Create a new Jupyter Notebook, The command panel needs to be opened , And then choose Jupyter: Create New Jupyter Notebook, As shown below

It will create a new Jupyter Notebook, Can be in VS Code Simply create markdown And code units

good , That's all I share today , Just order one if you like Looking at Well ~

 Recommended reading :
introduction :  The most complete zero Foundation Python The problem of   |  Zero Basics 8 Months Python  |  Actual project  | learn Python That's the shortcut
dried food : A short comment on crawling Douban , The movie 《 The rest of us 》 | 38 year NBA Best player analysis  |    From people's expectation to public praise ! Tang Dynasty detective 3 disappointing   |  Laugh at the story of the new Yitian dragon slaying  |  Riddle answer King  | use Python Make a massive sketch of my little sister  | Mission impossible is so hot , I use machine learning to make a mini recommendation system movie
Interest : Pinball game   |  squared paper for practicing calligraphy   |  Beautiful flowers  |  Two hundred lines Python《 Cool run every day 》 game !
AI:  A robot that can write poetry  |  Color the picture  |  Forecast revenue  |  Mission impossible is so hot , I use machine learning to make a mini recommendation system movie
Gadget : Pdf turn Word, Easily handle forms and watermarks ! |  One touch html Save the page as pdf!|   bye PDF Withdrawal charges ! |  use 90 Lines of code create the strongest PDF converter ,word、PPT、excel、markdown、html One click conversion  |  Make a nail low-cost ticket reminder ! |60 Line of code to do a voice wallpaper switcher, look at my little sister every day !|

Annual hot money copy

  • 1). Oh my god !Pdf turn Word use Python Easy to handle !

  • 2). learn Python It's delicious ! I use 100 Line of code to make a website , Help people PS Travel pictures , Earn a chicken leg to eat

  • 3). Premiere billions , Hot all over the net , I analyzed 《 My sister 》, Discovered the secrets  

  • 4).80 Line code ! use Python Make a dorai A Dream separation  

  • 5). What you have to master 20 individual python Code , short , Useful  

  • 6).30 individual Python Strange sexual skills Collection  

  • 7). I summed up 80 page 《 Rookie Science Python Select dry goods .pdf》, Is dry  

  • 8). bye Python! I have to learn Go 了 !2500 Word depth analysis !

  • 9). Found a licking dog welfare ! This Python Reptile artifact is great , Automatically download sister pictures

Click to read the original , see B My station 20 A video !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved