The most comprehensive Pycharm tutorial (31) -- Pyhcarm practice

Source: Internet
Author: User

The most comprehensive Pycharm tutorial (31) -- Pyhcarm practice

1. Subject

This section describes how to use Pycharm to create, run, and Debug programs.

2. Preparations

Pycharm version 2.7 or later.

Install at least one Python interpreter, 2.4 to 3.3

3. Download and install Pycharm

: This page

4. Start PyCharm

Double-click mode (pycharm.exe or pycharm. bat in windows; pycharm. sh in MacOS and Linux) to go to the Welcome page. Welcome screen:

5. Create a simple project

Click the Create New Project link to go to the Create Project dialog box and set the Project.

Of course, you can also use the main menu command File → New Project to create a New Project at the same time:

First, name the project. Here, name it MySimplePythonApplication. Then, you can change the project location, either by default or by clicking the Browse button.

Next, select the project type. Pycharm presets several types of templates (Django, Google AppEngine, and so on) and creates related files by default.

Here we chooseEmpty projectType (suitable for simple Python projects), no Pycharm presets are required.

Finally, specify the Python interpreter and select from the drop-down list.

Click OK to complete the project creation.

6. Browse the project directory structure

The initial Project directory (in Project tool window) is as follows:

Only the project root directory and the External Libraries directory that defines the Python interpreter exist.

Click the button in the main toolbar and select the Project Structure page to view the detailed Project directory information:

The MySimplePythonApplication. iml file is stored in the idea directory under the project root directory to record the current project structure. There are several XML files in the directory, saving the relevant configuration information. The idea directory is invisible in the Project tool window.

Next, add a working directory to the root directory. On the Project Structure page, right-click the Project root directory and select New Folder:

Enter the Directory Name:

Finally, mark the Directory and ask the source file root directory: select the src directory and click.

Click OK to close the Settings dialog box.

Of course, the method of adding a directory is not unique. You can also create a Python file directly under the project root directory. In this case, the project directory is the source file root directory by default.

7. Create a Python class

In the project tool window, select the src directory and press Alt + Insert:

Select Python file and enter the name (Solver ):

Class created. Open and edit:

8. Edit source code

First, there are two lines of default generated code in the file:

This is generated by Pycharm Based on the template file template and the form variables $ PROJECT_NAME and $ USER are automatically replaced.

Next we will compile a small program for solving quadratic equations.

Pycharm provides various prompts during code writing. For example, if you only need to enter keywords when creating a class, a prompt list is displayed:

Select the keyword class and enter the class name (Solver). Pycharm will prompt you to continue the input:

At this time, the right slot will display an error mark, and an error message will be displayed when you hover your mouse over it. The indicator on the top of the right slot marks the current code check body. Green indicates that everything is normal, yellow indicates a warning, and red indicates an error.

Continue to create the "demo" member function and realize the automatic completion function of Pycharm code:

Continue the input. unused variables are displayed in Gray:

Next, the math module is used to calculate the discriminant. Because the math module has not been imported, Pycharm reports an error (Red wavy lines and red bulbs ).

For the usage mechanism of the red bulb, see intention actions and quick fixes. If the format is incorrect, the red bulb is incorrect.

Press Alt + Enter to view the prompt given by Pycharm:

Select import math library, and then the demo function in the Solver class at night, and calculate the discriminant:

Press Ctrl + Shift + F10 to run the script file. a console is displayed. Enter the values of a, B, and c. The Pycharm encountered an error:

The program reports an error when d (discriminant) is negative. To avoid this situation, add the judgment statement Ctrl + Alt + T (Code → Surround ):

Pycharm automatically creates an if statement structure. Finally, if you want to execute the program multiple times, you need to nest a while loop in the outer layer. The final effect of the Code is as follows:

Next, prepare for debugging.

9. Run the program

Three methods to run script files:

(1) Ctrl + Shift + F10 shortcut

(2) Use shortcut menu options

  

(3) Use the running button of the Main Menu

View the running result:

10. Running/debugging configurations

Each script file is run and debugged according to the specified configuration file (run/debug configuration), including the Script Name, working directory, preprocessing, and so on.

Pycharm has preset some common configuration file types (for Python scripts, Django applications, tests, and so on). You can view these Configurations in the Run/Debug Configurations dialog box. You can Run the Run → Edit operations... command or click the drop-down list in the Run area of the main toolbar to open this dialog box:

View the Edit statements dialog box in detail. The dialog box consists of Python and Default.

The content in the default run/debug list is the default configuration information. They do not have a specific name, but are automatically loaded and used according to the type.

The node named Python above only contains a gray configuration file.Solver. It is a temporary configuration file temporary profile, which is the default configuration of the Python type created by Pycharm.

You can save your configuration files permanently.

11. debug the program

Before debugging, set the breakpoint breakpoints and click the left slot:

Right-click the editing area and select Debug 'solver ':

The Debug tool window is displayed and Debugging starts. The default layout of the debugging window is as follows:

The frame, variable, and control lamp are displayed here. If you want to keep the console visible, drag it to the specified area:

Use stepping toolbar buttons for single-step debugging:

When a breakpoint is hit, the corresponding line turns blue:

12. navigation functions

If your programming process is interrupted halfway and you don't know where to start when you come back, this uses an important navigation function: Jump to the last editing position. Press Ctrl + Shift + Backspace.

Quickly view the symbol definition. For example, place the cursor at the place where sqrt is called. Press Ctrl + B and Pycharm will jump to the specified position of math. py:

Quickly search for symbols, classes, and files. Press Ctrl + Alt + Shift + N and enter the name:

For more information, see here.

13. Code Reconstruction

Assume that you have changed the name of a function demo and theoretically all of its calls need to be changed. Here, Pycharm provides the code refactoring function.

Press Shift + F6 and enter the new name in the dialog box:

Click Refactor to display the search result in the Find tool window:

Click Do Refacto to complete replacement:

Of course, we can also make more changes, such as moving the file location, changing the parameter structure of the function, and extracting variables. These are all types of refactoring. We will introduce it in detail in future tutorials.

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.