Learning about Common operators in Python

Written by Jacqueline Lim

What am I covering today?

Today, I'll be covering the various operators in python and how they can be used.

What are the types of operators?

There are various types of operators that can be grouped into different categories, the main categories are Arithmetic, Comparision, Assignment, and Logical operators.

These will be the most common kind of operators that are in a programming language. in this case, it's Python.

Arithmetic operators:

  • Addition (+)

  • Subtraction (-)

  • Multiplication (*)

  • Division (/)

  • Modulus (%)

  • Exponent (**)

  • Floor division (//)

Here's how to use the addition operator:

In the example below, the addition (+) operator adds 2 values (x and y) and gives their sum (z).

x = 2
y = 3
z = x + y
#output = 5 (sum)

Here's how to use the subtraction operator:

In the example below, the subtraction (-) operator takes away the 2nd value (y) from the 1st value (z) and gives their difference (a).

z = 5
y = 1
a = z - y
#output = 4 (difference)

Here's how to use the multiplication operator:

In the example below, the multiplication operator multiplies the 2 values (a and c) together and gives their product (b).

a = 5
c = 2
b = a * c
#output = 10 (product)

Here's how to use the division operator:

In the example below, the division (/) operator divides the first value (y) by the 2nd value (e) and gives their quotient (t).

y = 20
e = 4
t = y / e
#output = 5 (quotient)

Here's how to use the modulus operator:

In the example below, the modulus (%) operator divides the 1st value (x) by the 2nd value (f) and gives the remainder (c).

x = 10
f = 3
c = x % f
#output = 3 (remainder)

Here's how to use the exponent operator:

In the example below, the exponent (**) operator raises one value (x) to the power of the 2nd value (y).

x = 10
y = 2
z = x ** y
#output = 100

Here's how to use the floor division operator:

In the example below, the floor division (//) operator divides one value (s) by the second value (t) and gives the quotient rounded to the smallest whole number.

s = 4
t = 3
f = 4 // 3
#output = 1 (smallest whole number)

Relational/Comparision operators:

  • Greater than (>)

  • Less than (<)

  • Equal to (==)

  • Not equal to (!=)

  • Greater than or equal to (>=)

  • Less than or equal to (<=)

Here's how to use the greater than operator:

In the example below, the greater than (>) operator compares 2 values (x and y) and gives whether the value is greater than another value.

x = 5
y = 3
Is x > y or is y > x?
#output: x is greater than y, but y is not greater than x.

Here's how to use less than operator:

In the example below, the less than (<) operator compares 2 values (w and u) and gives whether the value is less than another value.

w = 10
u = 7
Is w < u or is u < w?
#output: u is less than w, but w is not less than u.

Here's how to use equal to an operator:

In the example below, the equal to (==) operator compares 2 values (a and b) and gives whether the value is the same as another value.

a = 8
b = 8
a == b
Is a equal to b?
#output: a is equal to b

Here's how to use not equal to an operator:

In the example below, the not equal to (!=) operator compares 2 values (a and b) and gives whether the value is not the same as another value.

a = 8
b = 9
a != b
Is a equal to b?
#output: As a and b are different values, a is not equal to b.

Here's how to use greater than or equal to the operator:

In the example below, the greater than or equal to (>=) operator compares 2 values (k and l) and gives whether the value is greater than or equal to another value.

k = 10
l = 19
Is l >= k or is k >= l?
#output: l is greater than but is not equal to k

Here's how to use less than or equal to the operator:

In the example below, the less than or equal to (<=) operator compares 2 values (g and h) and gives whether the value is less than or equal to another value.

g = 10
h = 19
Is h <= g or is g <= h?
#output: g is less than but is not equal to h

Assignment operators:

  • Assign (=)

  • Add and assign (+=)

  • Subtract and assign (-=)

  • Multiply and assign (*=)

  • Divide and assign (/=)

  • Modulus and assign (%=)

  • Exponent and assign (**=)

  • Floor divide and assign (//=)

Here's how to use the assign operator:

In the example below, the assigned operator initializes a value to the variable.

x = 4
#x is the variable, and 4 is the value, the assign operator puts a value into a variable.

Here's how to use the add and assign operator:

In the example below, the add and assign (+=) operator adds a value (d) to the variable (a) that is on the left.

a = 10
d = 4
a += d
#output: a will add the value of d to a and the sum will be assigned to a.

Here's how to use the subtract and assign operator:

In the example below, the subtract and assign (-=) operator takes away a value (c) from the variable (d) and their difference will be assigned to d.

d = 10
c = 9
d -= c
#output: d will take away the value of c and the difference will be assigned to d.

Here's how to use the multiply and assign operator:

In the example below, the multiply and assign (*=) operator multiplies the 2nd value (l) by the 1st value (i) and assigns the product to the 1st value (i).

i = 3
l = 10
i *= l
#output: i will multiply its value by l and the product will be assigned to i

Here's how to use the divide and assign operator:

In the example below, the divide and assign (/=) operator divides the 1st value (v) by the 2nd value (s) and assigns the quotient to the 1st value (v).

v = 6
s = 3
v /= s
#output: v will divide its value by s and the quotient will be assigned to v.

Here's how to use the modulus and assign operator:

In the example below, the modulus and assign (%=) operator divides the 1st value (o) by the 2nd value (n) and assigns the remainder to the 1st value (o).

o = 10
n = 3
o %= n
#output: o will divide its value by n and the remainder will be assigned to o.

Here's how to use the exponent and assign operator:

In the example below, the exponent and assign (**=) operator raises the 1st value (k) by the 2nd value (j) and assigns the product to the 1st value (k).

k = 20
j = 3
k **= j
#output: k will raise its value by j and the product will be assigned to k.

Here's how to use the floor divide and assign operator:

In the example below, the floor divide and assign (//=) operator divides the 1st value (g) by the 2nd value (f) and assigns the quotient (rounded to the smallest whole number) to the 1st value (g).

g = 10
f = 4
g //= f
#output: g will divide its value by f and the quotient (rounded to the smallest whole number) will be assigned to g.

Logical operators:

  • and (Logical and)

  • or (Logical or)

  • not (Logical not)

Here's how to use the logical and operator:

In the example below, the logical and operator returns True if both values are True, other than that it will return False.

True and True
False and True
False and False 
#output: True for the 1st statement, False for the other 2 statements

Here's how to use the logical or operator:

In the example below, the logical or operator returns True if one of the values is true, otherwise, it returns False.

True or False
#output: True
False or False 
#output: False
True or True
#output: True

Here's how to use the logical not operator:

In the example below, the logical not operator returns False if the value is True and returns True if the value is False.

x = 4
not 4
#output: False, cause 4 is True

Here's a little challenge for you:

What will be the value of g after using this operator (*=)? Comment your answers below.

g = 10
f = 11
g *= f

Conclusion:

While learning about operators can be challenging, especially since there are so many of them that can be used in programming languages, it is also important to know how all of the operators work and how they function within the programming language.

In this case, the programming language that I am using as a reference is Python, but the operators can be applied to any programming language.

So have fun learning about the various operators and how they work!