Python has 30 keywords, which are identifiers that Python reserves for special syntactic uses. Keywords contain lowercase letters only. You cannot use keywords as regular identifiers. Some keywords begin simple statements or clauses of compound statements, while other keywords are operators. All the keywords are covered in detail in this book, either in this chapter, or in Chapters 5, 6, and 7. The keywords in Python are:
break class continue def
else except exec finally
global if import in
not or pass print
try while with (2.5) yield
The identifier with is a new keyword starting with Python 2.5 (up to Python 2.4, it is a completely normal identifier). The identifier as, which is not, strictly speaking, a keyword, is used as a pseudokeyword as part of some statements (specifically, the statements from, import, and, in Python 2.5, with). In Python 2.5, using with or as as normal identifiers produces warnings. To enable with usage as a keyword (and therefore to enable the new with statement) in Python 2.5, begin your source file with the statement:
from __future__ import with_statement
This "import from the future" enables use of the with statement in this module.
Python uses nonalphanumeric characters and character combinations as operators. Python recognizes the following operators, which are covered in detail in "Expressions and Operators" on page 50:
Python uses nonalphanumeric characters and character combinations as operators. Python recognizes the following operators, which are covered in detail in Expressions and Operators on page 50:
+ - * / % ** // << >> & | ^ ~ < <= > >= <> != ==
Python uses the following symbols and symbol combinations as delimiters in expressions, lists, dictionaries, various aspects of statements, and strings, among other purposes:
The period (.) can also appear in floating-point literals (e.g., 2.3) and imaginary literals (e.g., 2.3j). The last two rows list the augmented assignment operators, which lexically are delimiters but also perform an operation. I discuss the syntax for the various delimiters when I introduce the objects or statements with which they are used.
The following characters have special meanings as part of other tokens:
' " # \
The characters $ and ?, all control characters except whitespace, and all characters with ISO codes above 126 (i.e., non-ASCII characters, such as accented letters) can never be part of the text of a Python program, except in comments or string literals (to use non-ASCII characters in comments or string literals, you must start your Python source file with a "coding directive," as covered in "Character Sets" on page 34). This also applies to the character @ in Python 2.3; however, in Python 2.4, @ indicates decorators, as covered in "Decorators" on page 115.
A literal is a number or string that appears directly in a program. The following are all literals in Python: