(8)基本用法

Python标识符的命名规则详解
本文介绍了Python中标识符的命名规范,包括大小写敏感、首字符要求、可用字符范围以及避免使用关键字。还提到了双下划线开头和结尾的特殊含义,并提示如何利用Python帮助系统查看关键字。

基本用法

标识符:用于变量、函数、类、模块等的名称。具体的规则为:

  1. 区分大小写;
  2. 第一个字母必须是字母、下划线。其后字符是:字母、数字、下划线。
  3. 不能使用关键字;
  4. 以双下划线开头和结尾的名称通常有特殊含义,尽量避免这种使用方法。

使用python的帮助系统可以查看关键字:

>>> help()

Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> keywords

Here is a list of the Python keywords.  Enter any keyword to get more help.

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

help> 

举例

>>> sxt=123
>>> _sxt=456
>>> 3sdf=33
SyntaxError: invalid syntax
>>> 

Tips:

  1. 按”F1“可以打开python的官方文档;

python标识符命名规则

类型规则例子
模块和包名全小写字母,尽量简单,若多个单词之间用下划线math,os,sys
函数名全小写字母,多个单词之间用下划线隔开phone,my_name
类名首字母大写,采用驼峰命名原则,多个单词时,每个单词第一个字母大写,其余部分小写MyPhone,MyClass,Phone
常量名全大写字母,多个单词使用下划线隔开SPEED,MAX_SPEED
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值