Subscribe Contact

Programming Basics
Basic Operators

Overview

Operators in programming are symbols that we use to perform various mathematical or other actions between variables and values. Python supports a large number of operators that are used in many different scenarios. It would be difficult to try to memorize all of them at one time. So instead, we will take a look at some now, work with them, and then add more along the way as we learn more about Python.

The first operators we'll study are those used for simple mathematics. Most of these operators are used for math concepts you should be familiar with, such as addition, subtraction, etc. I'll provide a list of the arithmetic operators and then we'll look at how to use them in Python.

Arithmetic Operators

Operator Operation Description
+ Addition Adds two values together. Examples:
x + 4
y + 6 + x
- Subtraction Subtracts one value from another value. Examples:
x - 4
y - 6 - x
* Multiplication Multiples two values together. Examples:
x * 4
y * 6 * x
/ Division Divides one value by another value. Be careful not to attempt to divide by zero. Examples:
x / 4
y / 6 / x
% Modulus Returns the remainder of dividing one value by another value. Examples:
x % 4
y % 6 % x
** Exponentiation Raises the power of one value by another value. Examples:
x ** 4
y ** 6 ** x
// Floor Division Divides one value by another value, then returns the floor value of the result. The floor value is the greatest integer less than or equal to the resulting value. Examples:
x // 4
y // 6 // x


Before we work on some examples, a word about precedence...


Order of Operations (Precedence) Rules

Precedence in mathematics means that when we have a mathematical expression with more than one operator, there is an established precedent that instructs us in what order to perform the calculation. Starting with the arithmetic operators, here is the order of precedence that is followed by Python:

  1. Any operations enclosed inside parentheses ( ) are performed first.
  2. If there are nested parentheses ( ( ) ), then start with the inner-most set of parenthesis and work outward.
  3. Next, all exponentiations are performed. If there is more than one, they are performed left to right.
  4. Next, all multiplication and division operations are performed from left to right.
  5. Last, all addition and subtraction operations are performed from left to right.

Combining Operators

Given the precedence rules above, we can combine arithmetic operators to create formulas and complex expressions as needed. For example, we could write the following:

x = 5
y = 8
z = 6
answer = (x + y) * z**2
print(answer)

Let's step through this formula: (x + y)*z**2


  1. Start with the formula. Note the placement of the operators and parenthesis.
  2. Substitute the values from the variable assignments above.
  3. Since 5 + 8 is enclosed inside parentheses, perform that addition first.
  4. Since exponentiation has a higher precedence than multiplication, raise 6 to the power of 2.
  5. Last, finish with the last multiplication.
Manually stepping through complex formulas and expressions is an important skill to master, the more complex, the more important it is. As we develop solutions to complex problems, it is important that we know that our coded solutions are functioning correctly by having a set of tested examples.


 

«  Previous : User Input
Next : Simple Math  »




© 2023 John Gordon
Cascade Street Publishing, LLC