Home >Backend Development >Python Tutorial >How to run the program after writing it in python

How to run the program after writing it in python

(*-*)浩
(*-*)浩Original
2019-07-02 11:31:5753024browse

After the installation and configuration are completed, we can write the first python program. Brothers and sisters who have learned other languages ​​​​all know that the introductory program for a language is hello world. Well, here we also use hello world to introduce ideas and open the door to python learning.

How to run the program after writing it in python

There are two ways to run python, one is to run under the python interactive command line; the other is to use a text editor in the command line Run directly.

Note: The above two running methods are compiled and run with the CPython interpreter. Of course, you can also write python code into eclipse and run it using the JPython interpreter. You need to configure the environment yourself. (Recommended learning: Python video tutorial)

1. Command line and interactive command line

First of all, you must understand this command OK concept.

1. Command line

Definition: If something like "C:\>" appears, it is in the command line mode provided by Windows

How to enter the mode: Windows , directly enter the win r key

2. Interactive command line

Definition: ">>>" appears, which is the interactive command line of python

How to enter the mode: Enter python in the Windows command line

2. Run the python code in the interactive command line

In the cmd window, enter python and enter python interactive command line. Directly enter the code:

print ‘hello world!’

How to run the program after writing it in python

You can see the running results on the interface, which means the operation is successful!

The advantage of writing a program on the interactive command line of Python is that you can get the result in one go, but the disadvantage is that it cannot be saved and you have to type it again next time you want to run it.

So, when actually developing, we always use a text editor to write code. After writing, save it as a file, so that the program can be run repeatedly.

3. Run python code from the command line

1.Writing and saving python code

We will use a text editor for the "hello world!" program Write it out, save it, and name it. Here we name it hello.py and save it to F:\workspace.

When naming, please note:

1) The file must end with .py, nothing else is allowed

2) The file name can only be English letters, numbers and underscores The combination.

Recommended text editors: Notepad, Sublime Text

2. python code running

In command line mode, enter python F:\workspace\hello.py, that is Can run successfully.

When running, please note:

1) The storage path of the python file is a relative path. When running, you must specify the storage path of the python file,

Otherwise, an error will be reported:

can't open file 'hello.py'

How to run the program after writing it in python

Of course, when developing a program in Python, you can write code in a text editor while opening an interactive command window. During the process of writing code, Paste part of the code to the command line for verification, and get twice the result with half the effort!

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to run the program after writing it in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn