English example: "good morning hello you" is not syntactically valid. "good morning to you" is syntactically valid".
Programming language example: "hello"3
is not syntactically valid. 2*3
is syntactically valid.
int
: represent integers
, e.g., 3float
: represent real numbers
, e.g., 1.23bool
: represent boolean
values, e.g., True
and False
NoneType
: special
and has one value, None
type()
to see the type of an object
>>> type(5) int >>> type(3.0) float
float(1)
converts integer 3
to float 3.0
int(3.9)
truncates float 3.9
to integer 3
<object> <operator> <object>
. Operators on ints and floats
i+j
sumi-j
differencei*j
producti/j
divisioni%j
remainder when i
is divided by j
i**j
i
to the power of j
.
operator precedence
without parentheses:
+
and -
executed left to write, as appear in expression.Equal sign is an assignment of a value to a variable
pi = 3.14159Value is stored in computer memory. An assignment binds name to value. Retrive value associated with name or variable by invoking the name, by typing
pi
.
Abstracting expressions: give names to values of expressions, to reuse names instead of values. Example:
pi = 3.14159 radius = 2.2 area = pi * (radius ** 2)
Programming vs. Math: In programming, you do not "solve for x"
pi = 3.14159 radius = 2.2 # area of circle area = pi*(radius**2) radius = radius + 1Notice that in an assignment, the expression on the right is evaluated to a value, and a variable name is on the left.
radius += 1
is equivalent to radius = radius + 1
.
Changing bindings: can re-bind variable names using new assignment statements. Previous value may still be stored in memory but lost the handle for it. e.g., radius -> 2.2
now becomes radius -> 3.2
.