Collected expression syntax
Type categories
The data types we are looking at for expression syntax are:
- integer
- real
- ("real" and "integer" expressions are collectively known as "numeric
expressions")
- string
- boolean
- list
This file discusses syntax only.
So for example, it will tell you that 2+3 is an integer expression, but it
won't tell you enough to know that it evaluates to 5.
Literals
Rather than defining the syntax of literals precisely, I just want to give
some examples.
You know what an integer literal looks like.
A sequence of digits, optionally preceded by a minus sign.
Two examples: 38, -38
A real literal additionally has a decimal point in it somewhere,
or a power of ten in scientific notation (see insert),
or both.
Scientific notation in Python (and also most other programming languages):
An 'e' means "times ten to the power of ...". For example,
"3e6" is three million,
or 3 X 106, or 3000000.
Similarly, 2.123e5 is 212300.
A string literal is a sequence of characters enclosed in single
quotes.
There are various "escapes" to indicate hard-to-type characters, such as "\n"
for newline, and "\'" (backslash single-quote) for a single quote itself.
A list literal begins with a left square bracket, has zero or more
comma-separated literals (of any type), then concludes with a right square
bracket.
Variable invocation
A variable name can be used as an expression of whatever type has been
assigned to the variable.
Built-in functions
A built-in-function can be used by stating its name, then a left parenthesis,
zero or more comma-separated parameters, and a right parenthesis.
The required types of the parameters and the resulting type of the
built-in-function expression depend upon the built-in function being used.
Arithmetic (numeric) operators
If X and Y are numeric expressions (of either real or integer type, not
necessarily both the same), then
- X+Y is a numeric expression
- X*Y is a numeric expression
- X-Y is a numeric expression
- X/Y is a numeric expression
- -X is a numeric expression
- (X) is a numeric expression
The type of the expression is "integer" if the one or two
subexpressions are both of type integer; the type of the expression is "real"
if one or more of the subexpressions are of type real.
If X and Y are of integer type, then X/Y requests integer division, and yields
the quotient. The remainder of the integer division is available as the
expression "X%Y" (without the quotes, of course). In a typical spreadsheet
program this is written "MOD(X,Y)".
String operators
If S and T are string expressions and X is an integer expression, then
- (S) is a string expression
- S+T is a string expression
- S*X is a string expression
- len(S) is an integer expression
Boolean operators
If X and Y are numeric expressions and P and Q are boolean expressions, then
- X<Y is a boolean expression
- X<=Y is a boolean expression
- X==Y is a boolean expression (written X=Y in gnumeric)
- X>=Y is a boolean expression
- X>Y is a boolean expression
- X != Y is is a boolean expression meaning "not equal to"
(It is written
X<>Y in gnumeric and some other programming languages
-- mnemonic: think of it as "less than or greater than" --
and it's "#" in some other spreadsheet programs)
- not P is a boolean expression (written "not(P)" in gnumeric)
- P and Q is a boolean expression (written "and(P,Q)" in gnumeric)
- P or Q is a boolean expression (written "or(P,Q)" in gnumeric)
Expression-if
If P is a boolean expression and X and Y are numeric expressions, then
if(P,X,Y) is a numeric expression in gnumeric (and just about all spreadsheet programs).
In recent versions of Python only,
if P is a boolean expression and X and Y are numeric expressions, then
X if P else Y
is a numeric expression. P is evaluated first; if P evaluates to true, then X
is evaluated; if P evaluates to false, then Y is evaluated.
This is not nearly as commonly used as "expression-if" in spreadsheets, which
is why Python made do without it for so long. It often produces
hard-to-understand computer programs and I advise against using it in this
course, or, in most cases, in "real life".
[back to topic list]
[main course page]