python标识符,关键字,注释

标识符

所谓的标识符,其实指的就是我们对高级语言中的变量,常量,函数,类,对象这些数据起的名称

注:在python中,任何场景下的标识符都区分大小写

>>> abc = 1
>>> ABC = 2
>>> Abc = 3
>>> abc + ABC + Abc
6
  • 标识符第一个字符必须是字母表中的字母或下划线
  • 标识符的其他部分由字母,数字和下划线组成,必须连在一起是一个词
  • 标识符对字母大小写敏感
  • 变量名全部用小写,常量名全部大写
>>> name = "zs"
>>> age = 12
>>> PI = 3.14
  • 函数名用小写加下划线的方式
  • 类名用大写驼峰:如果类由多个单词组成,没给单词首字母大写即可
    MyAge YourGirlFriend
  • 模块和包名 名字用小写

关键字

也称保留字,是python语言官方确定的用作语法功能的专用标识符,已经被赋予了特殊的含义因此不能再被使用
常用关键字

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']

关键字不能素衣是用,否则会报错
对于python中的一些内置函数(自带的函数而言),其函数名不算是关键字,就有可能被误用,例:

>>> print(3)       # print是函数名
3
>>> print = 10     # print变成变量名
>>> print + 1
11
>>> print(3)       # 如果print内置函数一旦被给予新的定义 则原先的定义消失
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable

因此,为了避免内置函数名被误用,所以标识符也不能随意使用内置函数的名称

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

注释

一般在脚本文件中使用注释,注释是具有说明性的,有帮助性的帮助阅读者理解,不参与任何运行工作,主要是给程序员看,不给计算机看

  • 单行注释:# 注释内容直到换行为止
  • 多行注释:其实就是多个单行注释 快捷键 Ctrl+/
  • 文档注释:“”" 注释内容 “”"
# 这是一个单行注释 在没换行之间我都还是注释

# 这是个多行注释
#这是个多行注释

"""
文档注释 文档注释的内容 可以由后续一个doc的工具进行提取
能够生成一份关于该程序代码的说明书!
"""
print("Hello World!")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值