Using a text editor py (5)----Another way to keep love going

Source: Internet
Author: User
Tags sublime text

Left you so long, the tenderness in the palm of the memory of the wound, in a way to beautify all those wounds

------------Hashlinux

In Python's interactive command line to write programs, the advantage is that you can get results, the disadvantage is unable to save, the next time you want to run, you have to knock again.

So, in the actual development, we always use a text editor to write code, finished, saved as a file, so that the program can be run repeatedly.

Now, let's write the last ‘hello, world‘ program in a text editor and save it.

So here's the question: where is the text editor strong?

Two text editors recommended:

One is sublime Text, free to use, but no charge will pop up the prompt box:

650) this.width=650; "Src=" http://www.liaoxuefeng.com/files/attachments/ 0014316432749618f6c01e3df674e4db44799536ce37531000/l "alt=" sublime "/>

One is notepad++, free to use, with Chinese interface:

650) this.width=650; "Src=" http://www.liaoxuefeng.com/files/attachments/ 0014316432852715b0ecf4b452c4648a60059f4cfa21059000/l "alt=" notepad++ "/>

Note that you can use whichever, but never use Notepad with Word and Windows . Word does not save plain text files, and Notepad will be smart to add a few special characters (UTF-8 BOM) to the beginning of the file, resulting in an inexplicable error in the program running.

After you have installed the text editor, enter the following code:

Print (' Hello, World ')

Notice that print there are no spaces ahead. Then, select a directory, for example C:\work , save the file as hello.py , you can open the Command Line window, the current directory to switch to the hello.py directory, you can run the program:

C:\work>python Hello.pyhello, World

It can also be saved as a different name, for example first.py , but it has to .py end, and none of the others. In addition, the file name can only be a combination of English letters, numbers, and underscores.

If the current directory does not have hello.py this file, the operation python hello.py will be error:

C:\users\ieuser>python hello.pypython:can ' t open file ' hello.py ': [Errno 2] No such file or directory

The error means that the file cannot be opened hello.py because the file does not exist. At this point, you should check the current directory for this file. If you are hello.py storing in another directory, you must first cd switch the current directory with a command.

Video Demo:


Run the py file directly

A classmate asked, can you like the. exe file to run the .py file directly? It's not working on windows, but it's OK on Mac and Linux by .py adding a special comment to the first line of the file:

#!/usr/bin/env python3print (' Hello, World ')

Then, give hello.py the Execute permission by command:

$ chmod a+x hello.py

You can run it directly hello.py , such as running on a Mac:

650) this.width=650; "Src=" http://www.liaoxuefeng.com/files/attachments/ 001387104176573bc75768a559c474a8a69755828c1930a000/0 "alt=" Run-python-in-shell "/>

Summary

Using a text editor to write a python program and then save it as a suffix .py file, you can run the program directly with Python.

What is the difference between Python's interactive mode and running .py files directly?

Direct input python into interactive mode is equivalent to launching the Python interpreter, but waiting for you to enter the source code one line at a line to execute each row.

Running the .py file directly is equivalent to launching the Python interpreter and then .py executing the file's source code at once, and you don't have the opportunity to enter the source in an interactive way.

With the Python development program, you can write the code in the text editor, while opening an interactive command window, in the process of writing code, the part of the code to stick to the command line to verify, less! The premise is to have a 27 ' big monitor!

Reference source
#!/usr/bin/env Python3

#-*-Coding:utf-8-*-



Print ('Hello, World')


This article is from the "lake and Laughter" blog, please make sure to keep this source http://hashlinux.blog.51cto.com/9647696/1792648

Using a text editor py (5)----Another way to keep love going

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.