《Python程序设计》——Python入门(2)

《Python程序设计》——Python入门(2)今天开始为大家带来《Python程序设计》的学习。今天学习第一章——Python入门第二部分的内容,主要内容是程序结构和编码规范。Today b

《Python程序设计》——Python入门(2)

今天开始为大家带来《Python程序设计》的学习。今天学习第一章——Python入门第二部分的内容,主要内容是程序结构和编码规范。

Today begins the study of Python Programming for you. Today learn the first chapter – the second part of Introduction to Python, which focuses on program structure and coding specifications.

01

文件类型

在python中,常用的文件类型有三种:源代码文件,字节代码文件和优化代码文件。

(1)源代码文件:python源代码文件扩展名为p.y,可以使用文本编辑器编辑。

(2)字节代码文件:字节代码文件扩展名为.pyc,是由python源代码文件编译而成的二进制文件,由python加速执行,其速度较快,能够隐藏源代码。可以通过python.exe或脚本模式将python源文件编译成python字节代码文件。

(3)优化代码文件:python优化代码文件是优化编译后的文件,无法用文本编辑器进行编辑,一般用于嵌入式系统。

In python, there are three common file types: source code files, byte code files and optimized code files.

(1) Source code files: python source code files have the extension p.y and can be edited with a text editor.

(2) Byte code files: Byte code files with the extension .pyc are binary files compiled from python source code files, which are accelerated and executed by python, and are faster and can hide the source code. You can compile python source files into python byte code files by python.exe or script mode.

(3) Optimized code file: python optimized code file is a file compiled by optimization, which cannot be edited by text editor and is generally used in embedded system.

02

代码结构

《Python程序设计》——Python入门(2)

简单的python程序只需要很少的代码。但是,一个具有复杂功能的python程序代码量可能非常大,需要具有良好的代码结构。

A simple python program requires very little code. However, a python program with complex functionality can be very large in code and needs to have good code structure.

02

程序编码规范

(1)注释:单行注释:将要注释的一行代码以#开头。

多行注释:将要注释的多行代码以#开头,或将要注释的多行代码放在成对”’(3个单引号)和”””(3个双引号)之间。

批量注释法:在IDLE中,选中要注释的代码块,同时按Alt+3组合键,添加批量注释。选中要注释的代码块,同时按Alt+4组合键,删除批量注释。

在PyCharm中,选中要注释或取消注释的代码块,同时按Ctrl+/组合键,在添加批量注释和删除批量注释之间切换。

(2)代码缩进:python采用严格的缩进方式来体现代码的从属关系,而不使用大括号{}。缩进空格数是可变的,但同一个语句块的语句必须包含相同的缩进空格数。

(3)多行书写一条语句:python可以用反斜杠(\)来实现多行书写一条语句。

(4)空行:空行是程序代码的一部分。例如,函数之间和类的方法之间用空行隔开,表示一段新的代码的开始;类和函数入口之间也用空行隔开,以突出函数入口的开始。空行的作用是分隔两段不同功能或含义的代码。

(5)语句块:缩进相同的一组语句构成一个语句块,又称为语句组。像if,while,def和class这样的复合语句,首行以关键字开始,以冒号(:),该行之后的一行或多行代码构成语句块。同一个语句块中的语句缩进必须相同。

(1) Comments. Single line comment: the line of code to be commented starts with #.

Multi-line comment: the multi-line code to be commented starts with #, or the multi-line code to be commented is placed between pairs of ”’ (3 single quotes) and “”” (3 double quotes).

Batch comment method: In IDLE, select the code block to be commented and press Alt+3 at the same time to add a batch comment. Select the block to be commented and press Alt+4 at the same time to remove the batch comment.

In PyCharm, select the block to be commented or uncommented, and press Ctrl+/ at the same time to toggle between adding bulk comments and deleting bulk comments.

(2) Code indentation: python uses strict indentation to reflect the subordination of code, instead of using curly brackets {}. The number of indented spaces is variable, but statements in the same statement block must contain the same number of indented spaces.

(3) Writing a statement on multiple lines: python can use backslashes (\) to write a statement on multiple lines.

(4) blank lines: blank lines are part of the program code. For example, a blank line separates functions and methods of a class, indicating the beginning of a new piece of code; a blank line also separates classes and function entries to highlight the beginning of a function entry. The role of blank lines is to separate two different functions or meaning of the code.

(5) statement block: indentation of the same set of statements constitute a statement block, also known as a statement group. Like if, while, def and class such a compound statement, the first line to the keyword to start with a colon (:), the line after the line or lines of code constitute a statement block. The indentation of statements in the same block must be the same.

参考资料:文字:百度;图片:微博;翻译:百度翻译

本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。

今天的文章《Python程序设计》——Python入门(2)分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/7846.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注