Expressions vs Statements
Expression
A representation of something. Python evaluates expressions, resulting in a value.
Statement
Does something. Python executes statements, not necessarily resulting in a value.
Integer Operations
+, -, , /,
*, unary -
Operations on integers must yield integers
Example:
/ is not an integer operation
Float Type
- Values are approximations of real numbers
- Numbers with ”.” are float literals
- Numbers without ”.” are int literals
Floats have finite precision because they are represented as an integer mantissa times a power of 2. This results in an approximation error.
0.1 + 0.2
Str type
- Any sequence of characters
- + operation concatenates strings (cannot add
'string' + 2
) - String literal: a sequence of characters in quotes
Bool type
- True, False
- operations: not, and, or
- comes from comparing other types (>=, ⇐, ==, !=, etc.)
Converting between types
type(value)
- explicit conversion through casting (
int(2.0)
)
Short circuiting
- boolean expressions stop evaluating if the truth value is already determined
-
False and 1/0 # 1/0 by itself would produce an error
- False
-