05_Python标识符

一、标识符

python 中的标识符主要指作为:变量、函数、类、模块以及其他对象的名称

二、定义合法标识符的规则

  • 由数字、字符,下划线组成,不能数字开头
  • 严格区分大小写,age 和 Age 是两个不同的标识符
  • 不能使用 python 保留的关键字,通过 keyword 模块中的 keyword.kwlist 属性查看有哪些关键字
# 标识符由数字、字符,下划线组成,不能数字开头
name_1 = "tfos"
print(name_1)

# 1_name = "tfos"
# print(1_name)

# 标识符严格区分大小写
age = 12
Age = 39
print(age)
print(Age)

# 查看 python 保留关键字
import keyword
print(keyword.kwlist)

'''
python 保留关键字:
['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']
'''

# 标识符不能使用关键字
# True = "tfos"
# print(True)

三、定义标识符的规范

  • 见名知意
  • 遵守必定的命名规范
    • 小驼峰命名法:当多个单词拼在一起时,第一个单词首字符小写,后来每个单词的首字符大写,示例:userNameAndAge
    • 大驼峰命名法:当多个单词拼在一起时,每个单词的首字母大写,示例:UserNameAndAge
    • 下划线命名法:当多个单词拼在一起时,单词之间使用 _ 拼接,示例:user_name_and_age
  • 注意:在 python 中的变量,函数,模块名一般使用下划线命名法,python 的类名一般使用大驼峰命名法
© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
柒柒笑语盈盈的头像 - 鹿快
评论 抢沙发

请登录后发表评论

    暂无评论内容