Basic operators in Python

Basic operators in Python

posted 9 min read

A. Introduction

Operators, ranging from a simple addition to a more complex operations like bitwise and beyond, serve as indispensable tools in the hands of the programmers to manipulate data and achieve desired outcomes dictated by the code's objectives. Understanding the specific applications of each operator is crucial to build software that are not only functional but also efficient. Also mastering Python scripting requires a solid understanding of the basics.

Part 1 covers Arithmetic, Comparison and Assignment operators. Part 2 which is in a different article covers Logical, Bitwise, Membership and Identity operators.

B. Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic operations on numerical values.

1. Addition (+)

The operator symbol + is used to get the total or sum from the given operands.

I will be using REPL most of the time to show some examples. REPL stands for Read Evaluate Print Loop. Open your terminal or powershell for windows and type Python. You need to install Python first.

>>> a = 10 + 5
>>> a
15
>>>

The variable a is the sum, the 10 and 5 are the operands.

2. Subtraction (-)

In the subtraction operation we use the subtraction operator with symbol -. The result is the difference, the first operand is called the minuend and the second operand is called the subtrahend.

>>> a = 100
>>> b = 50
>>> c = a - b
>>> c
50
>>>

The c is the difference, a is the minuend, and b is the subtrahend.

3. Multiplication (*)

The result of the multiplication of operands is called a product.

>>> a = 4
>>> b = 2
>>> c = a * b
>>> c
8
>>>

c is called the product, a and b are the operands also called the factors.

4. Division (/)

The result of the division operation is called a quotient. The dividend is the numerator or the number to be divided and the divisor is the denominator or the number that divides the dividend.

>>> a = 100  # dividend
>>> b = 2  # divisor
>>> c = a / 2  # quotient
>>> c
50.0
>>>

5. Floor Division (//)

It is a division but rounds the result off to a whole number.

>>> a = 100
>>> b = 3
>>> c = a // b
>>> c
33
>>>

6. Exponentiation (**)

Exponentiation is an operation that involves raising a base number to a certain number called an exponent. It just multiplies the base n times where n is the exponent.

>>> a = 2  # base
>>> b = 2  # exponent
>>> c = a ** b
>>> c
4
>>>

It is similar to 2*2 or multiplying the two twice which is 4.

7. Modulo (%)

The modulo operator is used to calculate the balance or remainder after performing a division.

Integer modulo operation.

>>> a = 5
>>> b = 2
>>> c = a % b
>>> c
1
>>>

If we divide 5 by 2 the answer is 2 with some extra because 2 times 2 is 4. Now 5 - 4 equals 1, that 1 is called the remainder or balance.

Example with zero remainder.

>>> a = 10
>>> b = 5
>>> c = a % b
>>> c
0
>>>

Since 10 is divisible by 5 or no excess number, the remainder is zero.

Float modulo operation.

>>> a = 6.5
>>> b = 4.3
>>> c = a % b
>>> c
2.2
>>>
Note: The result of addition is a sum, the result of subtraction is a difference, the result of multiplication is a product and the result of division is a quotient.

C. Comparison Operators

Comparison operators are used to compare two operands which results to either True or False value.

Here is a summary of these operators.

Operation Meaning
<</td> strictly less than
<=</td> less than or equal
> strictly greater than
>= greater than or equal
= equal
!= not equal

1. Equal to (==)

The operator symbol == is used to compare if the two operands are equal.

>>> a = 75
>>> b = 75
>>> c = a == b
>>> c
True
>>> a = 'tuesday'
>>> b = 'wednesday'
>>> c = a == b
>>> c
False
>>>

The variables a and b are called operands.

Here is an example script.

age.py

romeo_age = 36
lando_age = 32

if romeo_age == lando_age:
    print('Romeo and Lando do have the same age.')
else:
    print('Romeo and Lando do not have the same age.')

Run

python age.py

output

print('Romeo and Lando do have the same age.')

2. Not equal to (!=)

The operator != means not equal. It compares if two operands are not equal. If it is not equal the result is True otherwise the result is False.

>>> a = 45
>>> b = 50
>>> c = a != b
>>> c
True
>>>

Since 45 is not equal to 50 the result of the operation is True.

Here is another example comparing two strings or str.

>>> a = 'diamond'
>>> b = 'Diamond'
>>> c = a != b
>>> c
True
>>>

Although the meaning of variables a and b are the same (diamond) they are not equal. This is because Python follows the rule of case sensitivity. In other words a lower case is different from upper case. D is the upper case and d is the lower case.

3. Greater than (>)

The greater than operator with symbol > allows us to know if the value in the left-hand side is greater than the value in the right-hand side. If it is, it would return True otherwise False.

>>> a = 500
>>> b = 450
>>> c = a > b
>>> c
True
>>>

In a > b, the left-hand side is a and the right-hand side is b.

Since 500 is more than 450, the result is True.

>>> a = 1.25
>>> b = 1.26
>>> c = a > b
>>> c
False
>>>

The value 1.25 is below 1.26 that is why the result of the operation is False.

4. Less than (<)</h3> The less than operator with symbol "<" is used to check if the value in the left-hand side is below the value in the right-hand side. >>> a = 36 >>> b = 45 >>> c = a < b >>> c True >>> The value of the variable "a" is 36 while the value of the variable "b" is 45. The result is True because 36 is below 45. 5. Greater than or equal to (>=)

The greater than or equal to operator with symbol >= compares if the operand in the left is greater than or equal to the operand in the right. If so the result is a True otherwise it is False.

>>> 100 >= 50
True
>>> 50 >= 75
False
>>> 25 >= 25
True
>>>

6. Less than or equal to (<=)</h3> The comparison operator with symbol <=</code> checks if the operand in the left is less than or equal to the operand in the right. >>> 50 >= 75 False >>> 25 >= 25 True >>> 12 <= 13 True >>> 14 <= 10 False >>> 8 <= 8 True >>> Tip: In comparison operators, the result is always True or False. D. Assignment Operators These operators primarily assign the value in the right side to the variable in the left side based on the operator used such as =, += and more. 1. Simple assignment (=)

The equal symbol = assigns the value in the right side to the variable in the left side.

>>> a = 45
>>> a
45
>>>

2. Addition assignment (+=)

The operator += is a combination of arithmetic operator + and assignment operator =. It adds the value in the right side to the existing value in the left side.

>>> a = 10
>>> a += 4
>>> a
14
>>>

That is the same as:

>>> a = 10
>>> a = a + 4
>>> a
14
>>>

3. Subtraction assignment (-=)

This is similar to item 2 except that the arithmetic operation is subtraction.

>>> a = 10
>>> a -= 4
>>> a
6
>>>

4. Multiplication assignment (*=)

The assignment operator *= is a combination of multiplication and assignment.

>>> a = 2
>>> a *= 2
>>> a
4
>>>

This is similar to:

>>> a = 2
>>> a = a * 2
>>> a
4
>>>

5. Division assignment (/=)

The assignment operator /= is a combination of division and assignment.

>>> a = 100
>>> a /= 2
>>> a
50.0
>>>

It is similar to:

>>> a = 100
>>> a = a / 2
>>> a
50.0
>>>

6. Modulus assignment (%=)

The modulo operator % is used to get the remainder. Together with assignment = operator, they are used to assign the remainder in the left side by taking the remainder between the left side value and right side value.

>>> a = 100
>>> a %= 3
>>> a
1
>>>

This is similar to:

>>> a = 100
>>> a = 100 % 3
>>> a
1
>>>

7. Exponentiation assignment (**=)

The **= operator uses the value in the left side as a base and raises to the power by the value in the right side then assigns the result to the variable in the left side.

>>> a = 2
>>> a **= 4
>>> a
16
>>>

This is the same as:

>>> a = 2
>>> a = 2 ** 4
>>> a
16
>>>

The 2**4 is just 2*2*2*2 which is equal to 16. Also the 2**4 is just 2^4 or 2 raised to power 4.

8. Floor division assignment (//=)

It is a combination of floor division and assignment operations. It takes the number in the left side and applies a floor division with the number in the right side and assigns it back to the left side. Floor division is just a division where the remainder is disregarded.

>>> a = 10
>>> a //= 2
>>> a
5

This is similar to 10//2 where the answer is 5 and then assign the 5 back to variable a. Note there is no remainder because 10 divided by 2 is perfectly 5.

Consider another example with remainder.

>>> a = 3
>>> a //= 2
>>> a
1

The first step is to divide 3 by 2 and the answer is 1 with a remainder of 1.

3 / 2 = 1 remainder 1

value = 2 * 1
remainder = 3 - value
remainder = 3 - 2
remainder = 1

In floor division // we ignore the remainder so the final answer is 1.

FAQ 1. What is REPL??
Answer: REPL stands for Read Evaluate Print Loop. It is used to test the code interactively. The user inputs the code and Python will READ, EVALUATE and PRINT the results. Install Python, open terminal and type Python to start the REPL.

2. What happens to the remainder in floor division?
Answer: The remainder is discarded or not used.

E. Summary

Arithmetic operators such as `+, -, *, /, //, **, and %` are used for basic operations like addition, subtraction, multiplication, division, floor division, exponentiation, and modulo respectively.

Comparison operators such as `<, <=, >, >=, ==, !=' are used to compare operands that may yield True or False results.

Assignment operators such as `=, +=, -=, *=, /=, %=, **=, and //=` are used to assign values to variables. They allow for direct modification of variables by performing arithmetic or other operations on them.

It is important for a programmer to learn this basic operators as a foundation to learn advanced topics such as reading files in directory. You should also learn Python syntax, which helps in avoiding errors in your code.

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

Exploring Python Operators: Beyond the Basics

Ferdy - Jun 22, 2024

Python Variables and Data Types

Ferdy - Mar 23, 2024

Python Syntax

Ferdy - Mar 5, 2024

Multithreading and Multiprocessing Guide in Python

Abdul Daim - Jun 7, 2024

You should know these f-string tricks in Python

mouyuan123 - May 28, 2024
chevron_left