Operators

To be honest, there are plenty of good tutorials about the usage of operators in Python on the web, e.g.

which is why we will merely touch the surface here. Briefly, as defined on the latter web page,

Operators are the constructs which can manipulate the value of operands. Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator. </i>

Just like in any other programming language, the type of operation to be carried out determines the set of operators to be used. For our purposes, the most important operations are

  • assignments,
  • arithmetic operations (i.e. math calculations),
  • comparisons, and
  • logical (or Boolean) operations.

The following section introduces the most important operators associated with each of these groups and also provides a set of examples in order to get you going.


In [1]:
%%html
<!--You can skip this HTML chunk, it is just used for left-aligning the below tables-->
<style>
  table {margin-left: 0 !important;}
</style>



Assignment operators

In contrast to other programming languages like C++ (int i = 0;) and Fortran (INTEGER :: i = 1), there is no need to explicitly declare variables in Python. The declaration, which basically means "reserve some space in memory for a variable of a certain type", happens automatically when assigning any kind of value to a variable.

For that purpose, Python uses the equal sign (=), with the name of the target variable to the left and the value to be assigned to the right of the assignment operator.


In [2]:
a = 5
a


Out[2]:
5

Unlike R, where most users stick to the traditional arrow assignment (<-) that also works in the reverse direction (i.e. ->), such a syntactical construct is not permitted in Python and will likely result in an error.


In [3]:
3 = b


  File "<ipython-input-3-c1d3231ca441>", line 1
    3 = b
         ^
SyntaxError: can't assign to literal

Of course, multiple assignments are possible, too. This applies for assigning one value to multiple variables


In [4]:
a = b = 5
print(a, b, sep = ", ")


5, 5

as well as assigning multiple values to multiple variables


In [5]:
c, d = 5, 2
print(c, d, sep = ", ")


5, 2

Comparison operators

Comparisons return True if a condition is met, and False otherwise. Be aware that in contrast to R, which requires either an abbreviated T (F) or a fully capitalized TRUE (FALSE), in Python only the first letter is capitalized.

Operator Description Example
< (<=) smaller than (or equal) a < b (a <= b)
> (>=) larger than (or equal) a > b (a >= b)
== is equal a == b
!= is not equal a != b

In [6]:
c < d


Out[6]:
False

In [7]:
c > d


Out[7]:
True

In [8]:
c == d


Out[8]:
False

In [9]:
c != d


Out[9]:
True

Arithmetic operators

Just like in other programming languages, arithmetic operations are straightforward to perform in Python. The list of readily available arithmetic operators thereby includes

Operator Description Example
+ Summation a + b
- Subtraction a - b
* Multiplication a * b
** Power calculation a ** b
/ Division a / b
// Floor division a // b
% Modulus a %% b

In [10]:
5 + 2


Out[10]:
7

In [11]:
5 - 2


Out[11]:
3

In [12]:
5 * 2


Out[12]:
10

In [13]:
5 ** 2


Out[13]:
25

In [14]:
5 / 2


Out[14]:
2.5

In [15]:
5 // 2


Out[15]:
2

In [16]:
5 % 2


Out[16]:
1

Of course, this also works for non-integer numeric values (i.e. float, of which more later on), for example


In [17]:
5.2 + 2.5


Out[17]:
7.7