Python 3 study notes: environmental structures

Python Overview

development path

1989 Dutchman Guido van Rossum invented by
1991 the first public release of the advent of
1994 Python 1.0 released
released Python 2.0 2000
2008 Python 3.0 released
the current version

2.x Python
Python 3.x
applications

Web site development
of large data processing
artificial intelligence,
automated operation and maintenance
of cloud computing
reptiles
game development
environment to build

System Environment

After all the code in the Windows environment configurations are complete system environment, system environment is as follows:

Windows 7 Ultimate x64
i5 - 3320M
DDR3L 16G memory
Python 3.7.4
installed Python

First, go to the official website to download the latest version of Windows Installer can be.

Secondly, the most important is the first step in the system environment variable added to the Python installed, as shown below:

Python 3 study notes: environmental structures![]

Of course, if this step is not added to the Python system environment variables, we can also attribute the computer, select Advanced System Settings - Environment Variables - System environment variables, adding the path where the Python, such as C: \ Python37 and C: \ Python37 \ Scripts two paths.

Finally, we can test whether the installation is successful, enter the command line tool:

. 1
Python -V
copy
if the result obtained as follows (shown Python version), it means that the installation was successful.

Python 3 study notes: environmental structures

至此,基本的学习环境已经搭建成功,我们可以开始愉快的学习 Python 了。

战前准备

注释

单行注释

在 Python 编程中,使用井号(#)作为注释符号,即与此符号处于同一行的所有内容即为代码注释,将被编译器忽略,不参与代码实际运行。

多行注释

如果我们想要书写如作者、编写时间、修改时间、程序说明等这类需要很多行的注释信息,当然,我们可以在每一行的开头加上井号(#)。不过在 Python 中为我们提供了一种其他方法,使用一对三引号(’’’ 或者 “””)将这些内容括起来,即表示这些内容为注释,将被编译器忽略,不参与打码实际运行。

中文编码声明

在 Python 2.x 中,为了解决其不支持直接书写中文的问题,规定使用如下方式解决此问题:

1

-- coding:utf-8 --

复制
或者

1

coding = uft-8

复制
此问题在 Python 3.x 中已经得到解决,不过为了规范页面的编码,还是建议加上此声明注释,以便其他人能够即使了解文件使用的编码规则。

缩进

在 Python 编码过程中,采用缩进和冒号(:)区分代码之间的层次结构,所以,Python 对代码块的缩进要求非常严格,同一级别的代码块的缩进必须一致!如果采用不同的缩进,轻则抛出异常提示,重则得到完全不一样的运行结果!

在 Python 中,一般采用四个空格或者一个 Tab 键作为一个缩进。

编码规范

Python 采用 PEP8 作为编码规则:

每个 import 语句只导入一个模块
不要再行尾加上分号(;),也不要使用分号(;)将两条代码放在同一行
建议每行不超过 80 个字符,如果超过,建议使用小括号将多行内容隐式连接起来
使用必要的空行增加代码的可读性
推荐在运算符的两侧、函数的参数之间、逗号(,)的两侧使用空格分隔
应避免在循环中使用加号(+)或者加赋值(+=)累加连接字符串;推荐将每个子字符串加入列表,使用 join() 方法连接
适当的使用异常处理语句提供代码的容错性,但不能过多依赖此结构
命名规范

名词理解

变量,存储实际数据的一个标签,通过这个标签可以快递定位其代表的实际数据
常量,一经指定就不会变化的量,如 π
函数,通过一些代码,对输入的数据进行加工处理后,得到目标数据的一系列代码的集合
模块,组织代码的一个实际的,存储在计算机中的文件,如 demo.py 文件
包,组织模块的特殊目录,其中包含一个 init.py 文件,和若干个模块

属性
方法
命名规则

Letters, numbers and underscores, not starting with a digit which
case sensitive
can not use the reserved word
module name short as possible, and all lowercase letters, the use of multiple words separated with an underscore
package name short as possible, and all lowercase letters, words used between a plurality of points (.) separated by
class name in Pascal style name, i.e., the first letter of each word, the remaining letters in lower case
type internal modular underlined + Pascal style named
functions, classes, properties and methods the same module naming and
constant name in all caps, use underscores to separate multiple words between the
variables used in the module begins with an underscore, the function is protected (protected), when using from ... import * statement can not be imported module introducing
class attributes used at the beginning of double-underlined, it is a kind of private (private)
reserved word

Reserved words, that a number of Python language has been given a special meaning words. In the development process, it may not be as variables, functions, modules, or other object name, or identifier.

Python 3 study notes: environmental structures

Identifier

Identifier, i.e., a name (code), primarily used to identify names of variables, constants, functions, or other objects. Identifier naming rules described above with reference to the content of naming.

Code Editor

Preparation with Windows built-in command line to write simple code, interim use Sublime Text to write a module needs to function like to know the code context structure, the latter if the code files and more, it is to use PyCharm as an editor for the time being so want.

Guess you like

Origin blog.51cto.com/14499640/2429602