Operators in Python (Operators in Python), lesson, page 724623
https://www.purl.org/stefan_ram/pub/operators_python (permalink) is the canonical URI of this page.
Stefan Ram
Python Course

Operators in Python 

Operators and Operands

An expression in parentheses is again an expression.

“Parentheses” are called “brackets” in British English.

It denotes the same object as the expression in parentheses.

Input of an expression and output of a text representation of its value
(2)
2

Such a group of characters as »(« »)« is called an operator. (The PLR uses as slightly different terminology.)

Since the parens surround  an expression they are an outfix operator.

The expression within the brackets is called operand  (of the operator).

Tokens and white space

Each part of an operator and each literal (i.e., numeral or str literal) is a token.

Tokens can be preceded or followed by spaces, but a line must not begin with a space.

Input of an expression and output of a text representation of its value
( 2    )
2

Within parentheses  a space can be replaced by a new line character.

Input of an expression and output of a text representation of its value
( 
2
)
2

Syntax Diagrams

A railroad diagram without  the »|-|« marker at the beginning and end is called a syntax diagram.

(A railroad diagram with  the »|-|« marker at the beginning and end is called a morphological diagram.)

In a syntax diagram spaces can be added when moving along a line.

Syntax diagram without the »|-|« marker at the beginning and end

expression in parens

.-. .---------. .-.
--->( ( )--->| literal |--->( ) )--->
'-' '---------' '-'

The exact meaning of such terms as expression, operand  and operator  is actually given by the syntax diagrams of language. But in this course we do not have time to sufficiently appreciate all the details of the syntax diagrams!

Exponentiation

An expression can be written as a literal followed by the operator »**« and another literal.

This denotes the value of the first operand raised to the power of the second operand.

Input of an expression and output of a text representation of its value
2 ** 6
64
Syntax diagram
power
.---------. .--. .---------.
--->| literal |--->( ** )--->| literal |--->
'---------' '--' '---------'

The values of the operands have to be numeric (›int‹ or ›float‹) in the case of this operator.

The ₍rɪˈzʌlt₎ result is ›int‹ or ›float‹ (whatever is appropriate).

This is a binary infix operator.

Nesting

Operands can be expressions, too. So expressions can be nested.

Syntax diagram

power
.---------. .--. .---------.
--->| literal |--->( ** )--->| literal |--->
'---------' '--' '---------'

power
.---------. .--. .---------.
--->| literal |--->( ** )--->| power |--->
'---------' '--' '---------'

Input of an expression and output of a text representation of its value
2 ** 2 ** 3
256

?   Practice Question

What is the right operand of the first power operator? How can this be seen in the syntax diagrams?

?   Practice Question

What could one write to have 2² raised to the power of 3?

Signs and the basic arithmetic operations (with syntax diagrams)

If you do not like to read syntax diagrams, please skip this section and read on in the next section “Signs and the basic arithmetic operations (with a table) ”.

Syntax diagram

atom

.------------.
---.------------>| literal |------------.--->
| '------------' |
| .-. .------------. .-. |
'--->( ( )--->| expression |--->( ) )---'
'-' '------------' '-'

power

.-------------------------------------.
| |
.------. | .--. .------------------. v
--->| atom |---'--->( ** )--->| unary expression |---'--->
'------' '--' '------------------'

unary expression

.------------------.
--->.------------>| power |----.---->
| '------------------' ^
| .-. .------------------. |
'--->( - )--->| unary expression |----'
| '-' '------------------' |
| .-. .------------------. |
'--->( + )--->| unary expression |----'
'-' '------------------'

multiplicative expression

.---------------------------.
--->.--->| unary expression |-------------------------------------.---->
| '---------------------------' ^
| .---------------------------. .-. .------------------. |
'--->| multiplicative expression |--->( * )--->| unary expression |----'
| '---------------------------' '-' '------------------' |
| .---------------------------. .-. .------------------. |
'--->| multiplicative expression |--->( / )--->| unary expression |----'
'---------------------------' '-' '------------------'

additive expression

.--------------------------.
--->.--->| additive expression |----------------------------------------------.---->
| '--------------------------' ^
| .--------------------------. .-. .---------------------------. |
'--->| additive expression |--->( + )--->| multiplicative expression |----'
| '--------------------------' '-' '---------------------------' |
| .--------------------------. .-. .---------------------------. |
'--->| additive expression |--->( - )--->| multiplicative expression |----'
'--------------------------' '-' '---------------------------'
expression

.---------------------.
--->| additive expression |--->
'---------------------'

The following practice questions are only intended for advance participants, but you do not have to worry if you can't answer them, because the rest of the course can be understood even then. Use the table from the next section, then.

?   Practice Question

Is »2+3*4« a multiplicative expression?

?   Practice Question

What is the right operand of the plus operator in »2+3*4«?

Signs and the basic arithmetic operations (with a table)

The rules of arithmetic (precedence rules and rules of associativity for signs and the basic arithmetic operations) in Python  are like they taught you in school, so you do not have to worry.

The binary operators are left-associative, only the powering is right-associative. Operators with a higher priority are listed further up in the following table.

Properties of signs and the basic arithmetic operations

M A P A (M = "BODMAS" mnemonics, A = arity [number of operands], P = position, A = associativity)

() B 1 O expression in parens
** O 2 I R exponentiation
+ - 1 P sign expression like "-2"
* / DM I L multiplication and division
+ - AS 2 I L addition and subtraction

Special str operations

The meaning of the addition and multiplication has been extended for str operands. In this case addition requires  that both operands have type ›str‹. Multiplication requires and int and a str operand.

Input of an expression and output of a text representation of its value
"2" + "abc"
'2abc'
Input of an expression and output of a text representation of its value
2 * "abc"
'abcabc'
Input of an expression and output of a text representation of its value
"abc" * 2
'abcabc'
Input of an expression and output of a text representation of its value
2 * "a" + 3 * "b"
'aabbb'
Input of an expression and output of a text representation of its value
1 * "abc"
'abc'
Input of an expression and output of a text representation of its value
0 * "abc"
''

?   Practice Question

What's the value of the expression shown below?

an expression
1 * "hvr" + 0 * "wzy"

?   Practice Question

What's the value of the expression shown below?

an expression
0 * "hvr" + 1 * "wzy"

 

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2020 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram724623 stefan_ram:724623 Operators in Python Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd724623, slrprddef724623, PbclevtugFgrsnaEnz Explanation, description, info, information, note,

Copyright 1998-2020 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/operators_python