Python online tutorial
Part 7: Operators
Operators take a number of operands and produce a result. There are three main types we will consider in this tutorial, but first we must talk about the simplest of all: the assignment operator (=
).
Assignment operator
Assignment in Python, as in most programming languages, is done with an equals sign (=
).
The assignment operator’s only, but very important function is to assign whatever is on the right-hand-side or RHS of the symbol (usually an expression) to whatever is on the left-hand-side or LHS of the symbol (usually a variable). If the expression cannot be assigned directly, it is evaluated first.
Careful – this contrasts with the typical meaning of equals and might cause you some confusion. We will see how equality tests are implemented in Python further down.
Arithmetic operators
Arithmetic operators enable us to perform simple mathematical operations in our program.
+
is addition, so5+3
will return8
-
is subtraction, so5-3
will return2
*
is multiplication, so5*3
will return15
**
is exponentiation, so5**3
will return125
/
is division, so5/3
will return1.6666666666666667
//
is floored division (i.e. result is rounded down), so5//3
will return1
%
is the modulo, modulus or remainder operator, which will return the remainder of a division, so5%3
will return2
There are importable modules specifically designed to work with advanced mathematics, some of which are math
, numpy
and scipy
. We will talk about imports, modules and libraries later on in this tutorial.
Comparison operators
Comparison operators compare expressions on either side of them and return a boolean.
==
is equality, so1==1
will returnTrue
but1==2
will returnFalse
.!=
is inequality, so1==1
will returnFalse
but1==2
will returnTrue
.>
is greater than, so1>2
will returnFalse
but2>1
will returnTrue
.>=
is greater than or equal, so1>=1
and2>=1
will returnTrue
but1>=2
will returnFalse
.<
is less than, so1<2
will return True but2
<1 will return False.- <= is less than or equal, so
1<=1
and1<=2
will returnTrue
but2<=1
will returnFalse
.
Comparison operators are typically used for generating a condition to use elsewhere or as a way to control the flow of the program.
Logical operators
Logical operators take booleans and return a boolean. There are 3 implemented in Python, but they can be combined to produce any boolean logic gate.
- not is the NOT gate. It inverts the input - it is
True
if the input isFalse
andFalse
if the input isTrue
. - or is the OR gate. The output is
True
whenever any of the inputs isTrue
,False
otherwise. - and is the AND gate. The output is True whenever all of the inputs are
True
,False
otherwise.
"Input" in this context refers to a condition, for example, 7<5
. This condition is obviously False
, but 7<5 or 7>5
is True
, because at least one of the conditions is True
.
Other operators
There are some other operators which will consider in this tutorial. We will not cover bit-wise operations as they are generally not needed in such a high-level language.
in
, the membership operator, will check if a sequence (typically a list) contains an element. Thus1 in [1,2,3]
isTrue
but1 in [2,3,4]
isFalse
.is
, the identity operator, will check if two objects are the same object using the result returned by their unique ID functionid()
.- Compound operators are formed by joining an arithmetic operator and the assignment operator together. For example, a
+
and the=
would yield+=
.a += 1
is exactly equivalent toa = a + 1
.