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, so 5+3 will return 8
  • - is subtraction, so 5-3 will return 2
  • * is multiplication, so 5*3 will return 15
  • ** is exponentiation, so 5**3 will return 125
  • / is division, so 5/3 will return 1.6666666666666667
  • // is floored division (i.e. result is rounded down), so 5//3 will return 1
  • % is the modulo, modulus or remainder operator, which will return the remainder of a division, so 5%3 will return 2

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, so 1==1 will return True but 1==2 will return False.
  • != is inequality, so 1==1 will return False but 1==2 will return True.
  • > is greater than, so 1>2 will return False but 2>1 will return True.
  • >= is greater than or equal, so 1>=1 and 2>=1 will return True but 1>=2 will return False.
  • < is less than, so 1<2 will return True but 2<1 will return False.
  • <= is less than or equal, so 1<=1 and 1<=2 will return True but 2<=1 will return False.

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 is False and False if the input is True.
  • or is the OR gate. The output is True whenever any of the inputs is True, 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. Thus 1 in [1,2,3] is True but 1 in [2,3,4] is False.
  • is, the identity operator, will check if two objects are the same object using the result returned by their unique ID function id().
  • 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 to a = a + 1.