5. 标识符和关键字

标识符

什么是标识符?

标识符是程序员在编写代码时为这些对象赋予的名字。

作用

标识符是用于标识变量名、函数名、类名、模块名和其他对象的名称。

标识符的命名规则

标识符示例

a        a1         abc         ABC      a1b2c3d4
one_hundred        print        input    打印机

getNameAge         get_name_age        GetNameAge
# 小驼峰命名法       匈牙利命名法          大驼峰命名法

1a       %abc       BB@cc       a100$

特殊命名方式的标识符

有四种标识符的写法有特殊含义,建议不要轻易使用

_*          # 如 _global_variable
_
__*__       #  如: __file__
__*         #  如 __abc

什么是关键字

关键字计算机编程语言中保留的标识符,关键字通常用于语法标识,关键字不能当成普通的标识符使用。

Python 3.13中全部的关键字

False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

python语法中没有的字符

以下三个字符不用于 Python中。在字符串字面值或注释外使用时,将直接报错

$       ?       `

练习

以下哪些不是合法的标识符?

100        abc123     else       __next__   dog 

as         a$b        %abc%      def        from       

china      c_h_i_n_a  student    1_0_0      xxx

jupyter_notebook    office@weimingze.com

视频讲解