1 快速开始-quick start

2022-11-09  本文已影响0人  taony

打开Power Shell终端,敲命令:python,输出结果:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
PS C:\Users\Administrator> python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

1.1 编码声明

默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串。 当然你也可以为源码文件指定不同的编码:

# -*- coding: cp-1252 -*-

指定UTF-8编码

# -*- coding: utf-8 -*-

1.2 标识符

1.3 保留字

保留字即关键字,我们不能把它们用作任何标识符名称。Python的标准库提供了一个keyword module,可以输出当前版本的所有关键字:

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class',
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda',
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try',
'while', 'with', 'yield']

1.4 行与缩进

python最具特色的就是使用缩进来表示代码块,不需要使用大括号({})。
缩进的空格数是可变的,但是同一个代码块的语句必须包含相同的缩进空格数。

1.5 多行语句

Python 通常是一行写完一条语句,但如果语句很长,我们可以使用反斜杠()来实现多行语句。

# 多行语句
item_one, item_two, item_three = 1, 2, 3;

total = item_one +\
        item_two + \
        item_three;

在 [], {}, 或 () 中的多行语句,不需要使用反斜杠(),例如:

total = ['item_one', 'item_two', 'item_three',
        'item_four', 'item_five']

同一行显示多条语句,使用分号分割

# 同一行显示多条语句

import sys; x = 'runoob'; sys.stdout.write(x + '\n')

1.5.1 单行注释

# 单行注释

1.5.2 多行注释

'''
单引号多行注释
单引号多行注释
单引号多行注释
'''

"""
双引号多行注释
双引号多行注释
双引号多行注释
"""
上一篇下一篇

猜你喜欢

热点阅读