Programming Search⌘K

Featureintro

Operators and delimiters

Also called Python operator tokens, Python punctuation, OP token, exact_type, Python delimiters.

Python's operators and delimiters are the symbol tokens, such as +, **, :=, ->, brackets and commas. The tokenizer reads the longest token it can, so ++ is two plus signs.

Language
Python

Synopsis

import io
import tokenize

source = "total += price * 2 ** n // 3 != limit\n"
for token in tokenize.generate_tokens(io.StringIO(source).readline):
    if token.type == tokenize.OP:
        print(f"{token.string:3} {tokenize.tok_name[token.exact_type]}")
Output
+=  PLUSEQUAL
*   STAR
**  DOUBLESTAR
//  DOUBLESLASH
!=  NOTEQUAL

Semantics

r1. The operators and delimiters are the tokens of the generic type OP. The grammar sorts them into groups:1

Group Tokens
assignment += -= *= **= /= //= %= &= |= ^= <<= >>= @= :=
bitwise & | ^ ~ << >>
comparison <= >= < > == !=
enclosing delimiters ( ) [ ] { }
other delimiters , : ! ; = ->
arithmetic + - ** * // / %
other . @ ...

r2. There is "no clear, formal distinction" between operators and delimiters. * is the multiplication operator and the delimiter of unpacking, and @ is matrix multiplication and the start of a decorator. Some operators, such as and, or and not in, are keyword tokens, not OP tokens.1 Example: same-symbol.

r3. Three periods, ..., are the Ellipsis literal.1

r4. Where the text could be read in more than one way, a token is "the longest possible string that forms a legal token, when read from left to right".2 Whitespace is needed between two tokens only when their concatenation would be a different token.3 Example: longest-match.

r5. tokenize reports all these tokens with the type OP, and the exact_type property gives the specific type.4 The token module lists the names, such as LPAR for (.5 Example: op-names.

r6. Some symbols are not tokens at all. Python 3.0 removed the operator <>, to be written !=, and the backticks, to be written as a call of repr().6 The characters $ and ? are in no token of the grammar of rule r1, so outside strings and comments they are an error.1

Examples

One token, two roles (rule r2): * multiplies and unpacks, and @ is a decorator here.

def shout(function):
    return lambda *args: function(*args).upper()

@shout
def greet(name):
    return "hi " + name

first, *rest = [1, 2, 3]
print(greet("ana"), first * 10, rest, ...)
Output
HI ANA 10 [2, 3] Ellipsis

The longest match decides the tokens (rule r4). <<= is one token, and **- is two.

import io
import tokenize

for source in ["x<<=1\n", "2**-1\n", "a--b\n"]:
    strings = [t.string for t in tokenize.generate_tokens(io.StringIO(source).readline) if t.type == tokenize.OP]
    print(source.strip(), strings)
Output
x<<=1 ['<<=']
2**-1 ['**', '-']
a--b ['-', '-']

Pitfalls

++ is not an increment

There is no ++ token (rule r1), so ++count is two unary plus signs, and the value does not change. count++ is a SyntaxError. Write count += 1.

This code runs but gives a wrong answer on purpose.

count = 5
++count
print(count)
Output
5

This code raises SyntaxError, on purpose.

count = 5
count++

= where == is meant

= is a delimiter of assignment, not a comparison (rule r1), so it is an error in a condition.

This code raises SyntaxError, on purpose.

x = 3
if x = 3:
    print("three")

Comparison

Go has increment and decrement statements: x++ and x-- "increment or decrement their operands by the untyped constant 1".7 Python has no such statement; an augmented assignment such as count += 1 takes its place.

References

  1. The Python Language Reference, Python 3.15, 2.7 Operators and delimiters
  2. The Python Language Reference, Python 3.15, 2.2 Other tokens
  3. The Python Language Reference, Python 3.15, 2.1.9 Whitespace between tokens
  4. tokenize, Python 3.15 documentation, tokenize.TokenInfo, exact_type
  5. token, Python 3.15 documentation, token.OP and the table of operator tokens
  6. What's New In Python 3.0, Removed Syntax
  7. The Go Programming Language Specification, IncDec statements

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source