Home  >  Article  >  Backend Development  >  Write your first Python program in 3 minutes

Write your first Python program in 3 minutes

Tomorin
TomorinOriginal
2018-08-23 17:47:381856browse

The content of this article is to write your first Python program. Write the program on the interactive command line of Python. The advantage is that you can get the result in one go, but the disadvantage is that there is no way. Save, you have to type it again next time you want to run it.

So, during actual development, we always use a Python text editor to write code. After writing, save it as a file, so that the program can be run repeatedly.

Now, we will write out the last 'hello, world' program in a text editor and save it.

Then the question comes: Which text editor is better?

Recommend two text editors:

One is Sublime Text, which is free to use, but if you don’t pay, a prompt box will pop up:

Write your first Python program in 3 minutes

One is Notepad , which is free to use and has a Chinese interface:

Write your first Python program in 3 minutes

Please note that you can use either one, but it must not be used. Notepad that comes with Word and Windows. Word does not save a plain text file, but Notepad will cleverly add a few special characters (UTF-8 BOM) at the beginning of the file, which will cause inexplicable errors when the program runs.

After installing the text editor, we can write our first Python program:

print('hello, world')

Note that there should be no print in front of it any spaces. Then, select a directory, such as C:\work, save the file as hello.py, you can open the command line window, switch the current directory to the directory where hello.py is located, and then run the program:

C:\work>python hello.py
hello, world

It can also be saved as another name, such as first.py, but it must end with .py, and nothing else will work. In addition, file names can only be a combination of English letters, numbers, and underscores.

If there is no hello.py file in the current directory, running python hello.py will report an error:

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

The above introduces how to write your first Python program.

The above is the detailed content of Write your first Python program in 3 minutes. 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