Basic Python Semantics: Operators

Arithmetic Operations

Operator Name Description
a + b Addition Sum of a and b
a - b Subtraction Difference of a and b
a * b Multiplication Product of a and b
a / b True division Quotient of a and b
a // b Floor division Quotient of a and b, removing fractional parts
a % b Modulus Integer remainder after division of a by b
a ** b Exponentiation a raised to the power of b
-a Negation The negative of a
+a Unary plus a unchanged (rarely used)

In [1]:
# addition, subtraction, multiplication
(4 + 8) * (6.5 - 3)


Out[1]:
42.0

In [2]:
# True division
print(11 / 2)


5.5

In [3]:
# Floor division
print(11 // 2)


5

The floor division operator was added in Python 3; you should be aware if working in Python 2 that the standard division operator (/) acts like floor division for integers and like true division for floating-point numbers.

An additional operator that was added in Python 3.5: the a @ b operator, which is meant to indicate the matrix product of a and b.

Assignment Operations

We've seen that variables can be assigned with the "=" operator, and the values stored for later use. For example:


In [4]:
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:


In [5]:
a + 2


Out[5]:
26
  • We might want to update the variable a with this new value, e.g. a = a + 2.
  • Python includes built-in update operators for all of the arithmetic operations:

In [6]:
a += 2  # equivalent to a = a + 2
print(a)


26

Augmented assignment operator

a += b a -= b a *= b a /= b
a //= b a %= b a **= b a &= b
a |= b a ^= b a <<= b a >>= b

For any operator "", the expression a ■= b is equivalent to a = a ■ b.

One catch:

For mutable objects like lists, arrays, or DataFrames, these augmented assignment operations are subtly different: 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.

Operation Description Operation Description
a == b a equal to b a != b a not equal to b
a < b a less than b a > b a greater than b
a <= b a less than or equal to b a >= b a greater than or equal to b

In [7]:
# 25 is odd
25 % 2 == 1


Out[7]:
True

In [8]:
# 66 is odd
66 % 2 == 1


Out[8]:
False

We can string-together multiple comparisons to check more complicated relationships:


In [9]:
# check if a is between 15 and 30
a = 25
15 < a < 30


Out[9]:
True

Boolean Operations

  • Python provides operators to combine the values using the standard concepts of "and", "or", and "not".
  • They are literally called "and", "or", and "not".

In [10]:
x = 4
(x < 6) and (x > 2)


Out[10]:
True

In [11]:
(x > 10) or (x % 2 == 0)


Out[11]:
True

In [12]:
not (x < 6)


Out[12]:
False

We will come back to the Boolean operations in the control flow statements section.

Boolean vs bitwise operators

When to use Boolean operators (and, or, not), and when to use bitwise operations (&, |, ~)?

  • Boolean operators: when you compute Boolean values (i.e., truth or falsehood) of entire statements.
  • Bitwise operations: when you 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. They are the following:

Operator Description
a is b True if a and b are identical objects
a is not b True if a and b are not identical objects
a in b True if a is a member of b
a not in b True if a is not a member of b

Identity Operators: "is" and "is not"

The identity operators, "is" and "is not" check for object identity. Object identity is different than equality, as we can see here:


In [13]:
a = [1, 2, 3]
b = [1, 2, 3]

In [14]:
a == b


Out[14]:
True

In [15]:
a is b


Out[15]:
False

In [16]:
a is not b


Out[16]:
True

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


In [17]:
a = [1, 2, 3]
b = a
a is b


Out[17]:
True
  • Frist case: a and b point to different objects
  • Second case: they point to the same object.

As we saw in the previous section, Python variables are pointers. The "is" operator checks whether the two variables are pointing to the same container (object), rather than referring to what the container contains.

Often, you might be tempted to use "is" what they really mean is ==.

Membership operators

Membership operators check for membership within compound objects. So, for example, we can write:


In [18]:
1 in [1, 2, 3]


Out[18]:
True

In [19]:
4 in [1, 2, 3]


Out[19]:
False

In [20]:
2 not in [1, 2, 3]


Out[20]:
False

In [21]:
4 not in [1, 2, 3]


Out[21]:
True
  • These membership operations are an example of what makes Python so easy to use compared to lower-level languages such as FORTRAN or C.
  • In FORTRAN, membership would generally be determined by manually constructing a loop over the list and checking for equality of each value.
  • In Python, you just type what you want to know

References

A Whirlwind Tour of Python by Jake VanderPlas (O’Reilly). Copyright 2016 O’Reilly Media, Inc., 978-1-491-96465-1