Arithmetic & Assignment Operators

Acadestine

Learning Objectives
    • Understand what "operators" are in programming.
    • Learn how to perform everyday math using Arithmetic Operators.
    • Discover how to update variables quickly and cleanly using Assignment Operators.

What Are Operators?

In programming, operators are special symbols that perform specific tasks on one or more values.

If variables are the "nouns" of a programming language (storing information), operators are the "verbs." They make things happen. They let you add numbers together, compare two different values, or assign a completely new value to a variable.

Whenever you use a calculator, you are already using operators! The plus sign (+) and the equals sign (=) are operators you have known since elementary school. Python uses these exact same symbols, plus a few special ones, to manipulate data and build logic.

Arithmetic Operators: Doing Math

Computers are essentially giant, incredibly fast calculators. Naturally, Python makes it very easy to do math. We call the symbols used for math Arithmetic Operators.

Here are the most common ones you will use every day:

  • Addition (+): Adds values together.
  • Subtraction (-): Subtracts one value from another.
  • Multiplication (*): Multiplies values. (Notice we use the asterisk, not an 'x'!)
  • Division (/): Divides the first value by the second. In Python, this always gives you a decimal number.

Let's look at how this might work if you are building an application connecting local food vendors with nearby customers. You would use these basic operators to calculate a customer's final bill:

Console

        

The Special Math Operators

Python also has a few math operators that you might not find on a standard basic calculator, but they are incredibly useful in programming:

  • Floor Division (//): Divides the numbers but chops off the decimal, leaving you with just the whole number. (e.g., 10 // 3 gives 3).
  • Modulo (%): This gives you the remainder of a division. (e.g., 10 % 3 gives 1 because 3 goes into 10 three times, with 1 left over).
  • Exponentiation (**): Raises a number to the power of another. (e.g., 2 ** 3 is 2 to the power of 3, which is 8).
Why is Modulo (%) so important?

At first glance, finding the remainder seems useless. But it is a programmer's secret weapon! For example, if you want to know if a number is even or odd, you just use % 2. If the result is 0, the number is even. If the result is 1, it is odd. You will use this trick often!

Assignment Operators: Updating Values

We already know the most famous assignment operator: the equals sign (=). It assigns the value on the right to the variable on the left.

But what happens when you want to update a variable that already exists?

Imagine you are developing a digital mobile game built around a 9 by 9 grid. When a player successfully clears a block or a row, you need to increase their current score by 10 points.

You could write it the long way:

score = score + 10

This tells Python: "Take the current value of score, add 10 to it, and then assign that new value back to the score variable."

However, programmers love efficiency. Instead of typing the variable name twice, Python provides a shortcut called Compound Assignment Operators. You can combine the math operator and the equals sign into one clean step:

Console

        

You can do this with almost any arithmetic operator to update a value in place:

  • += (Add and assign)
  • -= (Subtract and assign)
  • *= (Multiply and assign)
  • /= (Divide and assign)

Using these shortcuts keeps your code clean, highly readable, and professional!

Lesson Summary

Great job! You now know how to make Python do the heavy numerical lifting. Let's review the core concepts:

  • Operators: Special symbols (like + or =) that perform actions on variables and values.
  • Arithmetic Operators: Used for standard math calculations (+, -, *, /). Don't forget the handy special operators like Modulo (%) for remainders and Floor Division (//) for whole numbers.
  • Assignment Operators: Used to assign or update values. The standard = sets a value, while shortcuts like += and -= let you easily and cleanly update a variable based on its current state.
Previous Lesson Next Lesson