More Basic Python Operators

We have already learned multiple Python operators such as +,=,-,*,/,>,<,>=,<=,and,or,not,in,not in and so on. In this section we will learn some more. Create a new Jupyter Notebook and name it more_basic_operators to follow along.

Modulus Operator %

The operator % is called the modulo operator. It gives the remainder when performing division between two numbers. For example, if you divide 4 by 2 the remainder is 0. Therefore, 4 % 2 would return 0. Another example is when you do 9 divided by 4. The remainder is 1 so 9 % 4 would return 1.

4 % 2
0
print(9 % 4)
1

We can use the % operator to check if a number is even. For example, the number n is even if n % 2 returns 0 because the number 2 would divide evenly into n.

6 % 2 # 6 is even because remainder is 0
0
10 % 2 # 10 is even because remainder is 0
0
13 % 2 # 13 is odd because remainder is not 0.
1

You can also use % to check if a number is a multiple of any given number. If you do a % b and get a remainder of 0, then it means that a is a multiple of b. This just means that b divides into a evenly with a remainder of 0.

123 % 3 # returns 0 so 123 is a multiple of 3
0
256 % 32 # returns 0 so 256 is multiple of 32
0
99 % 45 # does not return 0 so 99 is not a multiple of 45
9

Assignment Operators

We have already learned about the = operator.

a = 1
print(a)
1

But there are some other operators which you will often see coders use. For example, you will often see the pattern:

i = 0
for x in range(4):
    print(i)
    i = i + 1
0
1
2
3

Another way of writing i = i + 1 is i += 1

i = 0
for x in range(4):
    print(i)
    i += 1 # is the same as i = i + 1
0
1
2
3

In general, a += b is the same as a = a + b. It is good to know about this because many Python coders will use these little shortcuts operators. Here are some others ones:

  • a += b is the same as a = a + b

  • a -= b is the same as a = a - b

  • a *= b is the same as a = a * b

  • a /= b is the same as a = a / b

Feel free to use these when you want.