角色使用Python

时间:2016-07-30 19:18:12

标签: python syntax

Python 3.5.2

我试图找出如何在python(!,@,$ etc)中使用字符,但是我无法找到任何帮助或任何细节。

以下是一个例子:

>>> $
SyntaxError: invalid syntax

我正在寻找的是为什么会发生这种情况以及语法的一个例子。或者这只是什么都不做,并不是一个功能和python,但如果不是我会认为你可以使用该字符作为变量,但你不能。

>>> $ = 'Example'
SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:2)

通常,Python标识符只能使用letters (from various languages), numbers, or underscores。与JavaScript不同,$不是有效的标识符字符。同样,标识符中不允许使用大多数标点符号,包括!@

  

标识符的有效字符[在Python中]是大写和小写字母AZ,下划线_和除第一个字符之外的数字{{1} }通过0

因此,有效的作业可能看起来像

9

表示标识符spam = 'Example'

答案 1 :(得分:2)

python中的命名约定规则只允许使用数字,字母和下划线。这里特别是规则

Variables names must start with a letter or an underscore, such as:
    _underscore
    underscore_
The remainder of your variable name may consist of letters, numbers and underscores.
    password1
    n00b
    un_der_scores
Names are case sensitive.
    case_sensitive, CASE_SENSITIVE, and Case_Sensitive are each a different variable.

Here您可以了解有关python变量赋值和命名约定的更多信息