Python基础语法,python语法规则,编码Python源码


编码

Python源码文件以UTF-8编码 字符串是Unicode编码

# -*- coding: cp-1252 -*-指定源码文件编码方式


标识符

使用字母数字下划线

首字符为大小写字母或下划线

大小写敏感


保留字

>>> 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‘]

注释

单行注释:#开头

多行注释:‘‘‘ 或 """

# 注释 ‘‘‘注释注释‘‘‘ """注释注释"""

缩进

缩进来代表代码块而不使用{}和其他语言不同

缩进的空格数可变 但同一个代码块必须缩进相同的空格数


多行语句

#使用反斜杠\实现多行S = 1 +     2 + \        3#在 [], {}, 或 () 中的多行语句,不需要使用反斜杠(\)Y  = [‘S‘, ‘Y‘,     ‘Z‘]   

Python基础语法

评论关闭