A literal is a constant value for some of the built-in types. Here are some examples of literals.
>>> a = 1 # 1 is an integer literal >>> b = 2.3 # 2.3 is a floating point literal >>> c = False # False is a boolean literal >>> d = 5L # 5L is a long integer literal >>> e = 8 + 6j # 8 + 6j is a complex literal >>> f = "Hello" # "Hello" is a string literal
An expression is composed of variables and operators. The simplest expression is just a variable. The value of an expression is evaluated before it is used.
These are the arithmetic operators that operate on numbers (integers or floats). The result of applying an arithmetic operator is a number.
There are 6 comparison operators. The result of applying the comparison operators is a Boolean - True or False.
You may apply the comparison operator between two operands like so (a > b). You can also apply the comparison operators in sequence like so (a > b > c). However, this is a practice that is not recommended.
In Python, we have two Boolean literals - True and False. But Python will also regard as False - the number zero (0), an empty string (""), or the reserved word None. All other values are interpreted as True. There are 3 Boolean operators:
a | not a |
F | T |
T | F |
a | b | a and b |
F | F | F |
F | T | F |
T | F | F |
T | T | T |
a | b | a or b |
F | F | F |
F | T | T |
T | F | T |
T | T | T |
The bitwise operators include the AND operator &, the OR operator |, the EXCLUSIVE OR operator ^ and the unary NOT operator ~. The bitwise operators applies only to integer types.
a | b | a ^ b |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Shift operators are applied only to integer types.
Python evaluates an expression from left to right except for assignment. In an assignment, the right-hand side is evaluated before the left-hand side. Refer to the operator precedence to determine the order in which the operations are performed.