Basic Python Semantics: Operators - jude-lindale/Wiki GitHub Wiki

Arithmetic Operations

Python implements seven basic binary arithmetic operators, two of which can double as unary operators. They are summarized in the following table:

These operators can be used and combined in intuitive ways, using standard parentheses to group operations. For example:

# addition, subtraction, multiplication
(4 + 8) * (6.5 - 3)
# True division
print(11 / 2)
# Floor division
print(11 // 2)

Bitwise Operations

Python includes operators to perform bitwise logical operations on integers. These are much less commonly used than the standard arithmetic operations, but it's useful to know that they exist. The six bitwise operators are summarized in the following table:

These bitwise operators only make sense in terms of the binary representation of numbers, which you can see using the built-in bin function:

bin(10)
'0b1010'

The result is prefixed with '0b', which indicates a binary representation. The rest of the digits indicate that the number 10 is expressed as the sum 1⋅2^3+0⋅2^2+1⋅2^1+0⋅2^0.

bin(4)
'0b100'

Now, using bitwise OR, we can find the number which combines the bits of 4 and 10:

4 | 10
14
bin(4 | 10)
'0b1110'

Assignment Operations

a = 24
print(a)
24

We can use these variables in expressions with any of the operators mentioned earlier. For example, to add 2 to a we write:

a + 2
26

If we want to update the variable a with a new value we could combine the addition and the assignment and write a = a + 2 Because this type of combined operation and assignment is so common, Python includes built-in update operators for all of the arithmetic operations:

a += 2  # equivalent to a = a + 2
print(a)
26

There is an augmented assignment operator corresponding to each of the binary operators listed earlier;

Each one is equivalent to the corresponding operation followed by assignment: that is, for any operator "", the expression a ■= b is equivalent to a = a ■ b, with a slight catch. For mutable objects like lists, arrays, or DataFrames, these augmented assignment operations are actually subtly different than their more verbose counterparts: they modify the contents of the original object rather than creating a new object to store the result.

Comparison Operations

Another type of operation which can be very useful is comparison of different values. For this, Python implements standard comparison operators, which return Boolean values True and False. The comparison operations are listed in the following table:

Comparison operators can be combined with arithmetic and bitwise oporatores to express a wide range of tests for numbers. For example, we can check if a number is odd by checking that the modulus with 2 returns 1:

# 25 is odd
25 % 2 == 1
True
# 66 is odd
66 % 2 == 1
False

We can string-together multiple comparisons to check more complicated relationships: # check if a is between 15 and 30 a = 25 15 < a < 30 True

Recall that ~ is the bit-flip operator, and evidently when you flip all the bits of zero you end up with -1. -1 == ~0 True

Boolean Operations

Python provides operators to combine the values using the standard concepts of "and", "or", and "not". Predictably, these operators are expressed using the words and, or, and not:

x = 4
(x < 6) and (x > 2)
True
(x > 10) or (x % 2 == 0)
True
not (x < 6)
False

Boolean algebra aficionados might notice that the XOR operator is not included; this can of course be constructed in several ways from a compound statement of the other operators. Otherwise, a clever trick you can use for XOR of Boolean values is the following:

# (x > 1) xor (x < 10)
(x > 1) != (x < 10)
 False

These sorts of Boolean operations will become extremely useful when we begin discussing control flow statements such as conditionals and loops.

One sometimes confusing thing about the language is when to use Boolean operators (and, or, not), and when to use bitwise operations (&, |, ~). The answer lies in their names: Boolean operators should be used when you want to compute Boolean values (i.e., truth or falsehood) of entire statements. Bitwise operations should be used when you want to operate on individual bits or components of the objects in question.

Identity and Membership Operators

Like and, or, and not, Python also contains prose-like operators to check for identity and membership.

The identity operators, is and is not check for object identity. Object identity is different than equality,

a = [1, 2, 3]
b = [1, 2, 3]
a == b
True
a is b
False
a is not b
True

What do identical objects look like? Here is an example:

a = [1, 2, 3]
b = a
a is b
True

Membership operators check for membership within compound objects.

1 in [1, 2, 3]
True
2 not in [1, 2, 3]
False