Python3 Syntax Symbol Uses

^C ^D \t \n \r <space> ! != " " # $ % %= %% & &= ' ' ( ) * *= ** **= + += , - -= . ... / /= // //= : ; < <= << <<= = op= == > >= >> >>= ? @ [ ] \ ^ ^= _ __ __ __ ` { } {{,}} | |= ~

Introduction

The ASCII character set includes non-printable control characters (designated below with a '^' or '\' prefix), space, letters and digits, and other printable symbols. A few of the control characters, space, and most of the symbols are used in Python code as operators, delimiters, or other syntactic units. Some symbols are used alone, some in multi-symbol units, and some in both. There are separate entries for each syntactic unit and for each different use of a unit. In total, there are nearly 100 entries for 61 symbols and combinations. Entries are in ASCII collating (sorting) order except that ?= entries (where ? is a symbol) follow the one for ? (if there is one) and the general 'op=' entry follows the one for =. The two lines after the entry for '\r' are entries for the invisible blank space ' '.

Most entries start with P, I, or S to indicate the syntactic unit's use as a prefix, infix, or suffix. (These terms are here not limited to operators.) If so, a template follows, with italicized words indicating the type of code to be substituted in their place. A description follows. Some syntactic units are split into two subunits that enclose code. Entries for these are the same except that two initials are used, either PS or IS, depending on whether the first subunit is a prefix or infix relative to the entire syntactic construct. A few entries specific to the interactive interpreter start with II. They are not really code uses but are handy to know.

Many of the templates and meanings of operators are specific to the current set of built-in classes. New classes, whether built-in or user-defined, can expand their meaning. For example, the addition of classes set and frozenset as built-ins expanded some of the entries from what they once would have been. And in 3.0, the word 'set' includes the new set-like dictview classes.

The words in the templates and explanations should help direct one to entries in the Python manuals. Some have been selected for that purpose. Most can be found by searching (using ^F) in the Lexical, Expression, or Simple Statement chapters of the Language Reference or the Built-in Types chapter of the Library manual. Omitted here are encoding declarations, details about '\' ecapes within string and bytes literals, and special symbol uses within % and {} format specifications.

Entries

Symbol

Kind

Template

Description

^C

II

control-C

Interrupt execution on Microsoft Windows systems.

^D

II

control-D

Interrupt execution on Unix-derived systems.

\t

P

tab(s) code

Syntactically significant indentation. Do not mix with spaces.

I

token_a whitespace(s) token_b

Whitespace is tab, newline, and space. Optional unless token_atoken_b run together would mean something else.

\n

S

code \n

Physical line, also logical line unless preceded by '\' or inside (), [], or {} pairs.

I

token \n token

Whitespace when preceded by '\' or inside (), [], or {} pairs; see infix \t.

\r

S

code \r\n or \r

Deleted or converted to \n.

P

space(s) code

Syntactically significant indentation. Do not mix with tabs.

I

token token

Whitespace; see infix \t.

!=

I

object1 != object2

Comparison operator for inequality of object values.

" "

PS

" text "

Single-quoted* string or bytes literal, cannot contain a newline (\n) character. *by one double-quote character


PS

""" text """

Triple-quoted* string or bytes literal, possibly spanning multiple lines. *by three double-quote characters

#

P or I

code # any_text

Comment ended by \n and optionally preceded by code.

$

<not used>

%

I

number1 % number2

Remainder (modulo) operator, 2 % 2 == 0, 3 % 2 == 1.

I

format_string % data

%-format format-string using %-format specifications.

P

%format_spec

Format within %-format strings for interpolated value. See Standard-library Built-in-types Sequence-types Old-string-formatting section for more.

%=

I

target %= expression

See op=.

%%

' x %% y '

Insert % rather than formatted value when used within %-format strings.

&

I

intset1 & intset2

Integer bitwise-and or set union operator.

&=

I

target &= expression

See op=.

' '

PS

' text '

Single-quoted string or bytes literal, cannot contain a newline (\n) character.

PS

''' text '''

Triple-quoted string or bytes literal, possibly spanning multiple lines.

( )

()

Empty tuple.

PS

( expression )

Separate expression from surrounding code as a syntactic atom.

PS

( comprehension )

Generator expression, with ( ) optional in one-arg calls.

IS

name ( base_classes )

Header for class definitions.

IS

name ( parameters )

Header for function definitions.

IS

callable ( arguments )

Call operator for functions, classes, and other callables.

*

*

In function parameter list, make following unstarred names keyword only.

P

*parameter_name

Function parameter bound to a tuple of extra positional arguments, any following unstarred names are keyword only.

P

*iterable

In a call, unpack iterable items as arguments.

P

*assignment_target

Starred target in assignment statement gets extra items from iterable.

I

number1 * number2

Number multiplication operator.

I

n * sequence or sequence * n

Operator to concatenate n copies of sequence.

S

from module import *

Import all public (non-private) names from the module.

*=

I

target *= expression

See op=.

**

P

**parameter_name

Function parameter bound to dict of extra keyword arguments.

P

**mapping

In a call, treat the key:value pairs as extra keyword arguments.

I

number1 ** number2

Exponentiation operator, number1 to the power number2.

**=

I

target **= expr

See op=.

+

P

+ number

The same as number, unless a class decides otherwise (rare).

I

number1 + number2

Number addition operator.

I

sequence1 + sequence2

Sequence concatenation operator.

+=

I

target += expression

See op=.

,

I

expression1, expression2

Delimiter for expression list that becomes a tuple unless appearing within [ ], { }, or func( ) to become list, set, dict, or param/arg list.

-

P

-number

Minus operator, negates the number.

I

number1 - number2

Number subtraction operator, number1 minus number2.

I

set1 - set2

Set difference operator, members of set1 not in set2.

-=

I

target -= expression

See op=.

.

P

.module

Used in relative imports but not well documented.

I

integer.fracton

Float literal, with either integer or fraction part (but not both) optional.

I

name.attribute

Dotted name that references attribute of object denoted by name.

I

package.sub_mod

Import a subpackage or module from package in import statement.

...

Ellipsis literal.

/

I

number1 / number2

Number division (quotient) operator, 1/2 == 0.5.

/=

I

target /= expression

See op=.

//

I

number1 // number2

Floor division operator, 1/2 == 0.

//=

I

target //= expression

See op=.

:

I

int1 : int2 : int3

Start, stop, and step for slicing, with in1, int2, and :int3 optional.

I

key : value

Delimiter for dict display items.

I

lambda arglist : expression

Lambda expression, abbreviates function definition.

I

param : expression

In parameter lists, add entry to function.__annotations__.

S

header :

Delimiter for header of compound statements, followed by a statement suite.

;

I

statement ; statement

Delimiter separating multiple statements on one physical line.

<

I

value1 < value2

Comparison operator for value1 less than value2.

<=

I

value1 <= value2

Comparison operator for value1 less than or equal to value2.

<<

I

integer << count

Left shift operator, i << j == i * 2**j.

<<=

I

target <<= expr

See op=.

=

I

param = value

In parameter lists, make the value the default argument for the parameter.

I

target = expression targets = iterable

Delimiter for assignment statements.

op=

I

target op= compatible_object

Delimiter for augmented assignment statements. Op can be infix +, -, *, /, //, %, **, &, |, ^, <<, or >>. Similar to 'target = target op value' except that target is evaluated only once and mutable targets get mutated instead of replaced.

==

I

value1 == value2

Comparison operator for equality of object values.

>

I

value1 > value2

Comparison operator for value1 great than value2.

>=

I

value1 >= value2

Comparison operator for value1 greater than or equal to value2.

>>

I

integer >> count

Right shift operator, i >> j == i // 2**j.

>>=

I

target >>= expr

See op=.

?

<not used>

@

P

@decorator

Wrap or modify the following class or function by calling the decorator.

[ ]

PS

[ display ]

With display an expression list or comprehension, make a new list.

IS

seqmap [ index ]

Subscript a sequence with an integer or slice or subscript a map with a hashable object.

\

P

\char(s)

Escape character changes meaning of following char(s) in string or bytes literals.

P

\newline-char

Outside quotes, allows logical line to continue on next physical line.

^

I

intset1 ^ intset2

Integer bitwise exclusive-or or set symmetric-difference operator.

^=

I

target ^= expression

See op=.

_

An 'honorary' letter when used in identifiers (names).

II

Default name bound to expressions used as statements.

P

_name

Private identifier, meaning is conventional except in * imports.

__

P

__name

Private identifier that gets mangled.

__ __

PS

__name__

Special, reserved name used by Python interpreters.

`

<not used>

{ }

PS

{ display }

With display an expression list or comprehension, make a new set, unless the display items are key:value pairs, in which case make a new dict, or unless there is no display, in which case make a new empty dict.

IS

{ format-spec }

Format within str.format() string for interpolated value. See Standard-library String-services string Format String section for more.

{{,}}

'x {{ y }} z'

Insert { or } rather than formatted value when used within str.format strings.

|

I

intset1 | intset2

Integer bitwise inclusive-or or set union operator.

|=

I

target |= expression

See op=.

~

P

~integer

Bitwise inversion operator, ~n == -(n+1).