Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Python programs

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to write Python programs". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Interactive environment of tools for writing code

We open Windows's command prompt tool, enter the command python and enter to enter the interactive environment of Python. The so-called interactive environment is that when we enter a line of code, the code will be executed immediately, and if the code produces a result, then the result will be displayed in the window. For example:

Python 3.7.6

Type "help", "copyright", "credits" or "license" for more information.

> 2 * 3

six

> 2 + 3

five

Warm reminder: users using the macOS system need to open the "terminal" tool and enter python3 to enter the interactive environment.

If you want to exit the interactive environment, you can enter quit () in the interactive environment, as shown below.

> quit () text Editor-Visual Studio Code

Visual Studio Code (usually referred to as VS Code for short) is a code editing artifact developed by Microsoft that runs on operating systems such as Windows, Linux, and macOS. It supports a series of convenient functions, such as syntax highlighting, automatic completion, multipoint editing, running and debugging, and can support a variety of programming languages. At this stage, it is strongly recommended that you use VS Code to write Python code. About the download, installation and use of VS Code, we recommend that you read the article entitled "VScode installation and use" on Zhihu. The following figure shows the main user interface using VS Code.

Integrated development environment-PyCharm

If you use Python to develop commercial projects, we recommend that you use the professional tool PyCharm. PyCharm is an integrated development environment (IDE) for Python development developed by the Czech company JetBrains. The so-called integrated development environment means that this tool provides various functions such as code writing, code running and debugging, code analysis, code version control, etc., so it is especially suitable for the development of commercial projects. The PyCharm download link is provided on the official website of JetBrains, in which the community version (Community) is free but relatively weak, and the professional version (Professional) is very powerful, but it needs to be used for an annual or monthly fee, and new users can try it for 30 days. We will explain how to use PyCharm for Python development in a later course.

Hello, world

According to industry practice, the first program we learn to write in any programming language is to output hello, world, because this code is the first code written by the great Dennis Ritchie (the father of the C language, who developed the Unix operating system with Ken Thompson) and Brian Koenihan (the inventor of the awk language) in their immortal book The C Programming Language.

Print ('hello, world') runs the program

It is recommended that you use VS Code to write the above code, edit the code and save it in a directory where you can easily find it. We will name the above code hello.py. If you are using the Windows operating system, you can then hold down the shift key on the keyboard and then click the right mouse button in the directory where you saved the code, and then the "command line prompt" option will appear in the right mouse button menu. Click this option to open the "command line prompt" tool, and we enter the following command.

Python hello.py

Reminder: we can also open the "command prompt" or "terminal" tool anywhere, and then drag and drop the Python code that needs to be executed into the "command line prompt" or "terminal", which is equivalent to specifying the absolute path of the file to run the Python code in the file. Again, the macOS system needs to run the program through the python3 command.

You can try to replace the hello and world in the single quotation marks of the above program with something else, or you can try to write a few more such statements to see what the result will be. We need to remind you that the print ('hello, world') in the above code is a complete statement. We write the program in Python, preferably with only one statement in each line of code. Although you can use the; delimiter to write multiple statements in one line of code, it's best not to do so, because the code can become very ugly.

Comment your code

Comments are an important part of the programming language, which is used to explain the role of the code in the source code to enhance the readability of the program. Of course, we can also remove the code snippets from the source code that don't need to be run for the time being by commenting, so that when you need to reuse the code, you can remove the comment symbols. In a nutshell, comments make the code easier to understand without affecting the execution of the program.

There are two forms of comments in Python:

Single-line comments: start with # and a space, and you can comment out the entire line starting with #.

Multiline comments: begin with three quotation marks and end with three quotation marks, usually used to add multiline descriptive content.

"

The first Python program-hello, world

Version: 0.1

Author: Luo Hao

"

# print ('hello, world')

This is the end of print ("Hello, World!") "how to write Python programs". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report