Basic Python Operators

Basic Python Operators

From your daily mathematical calculations, to your daily comparison of items or conditions, all of these require the use of mathematical operators to perform these calculations whether you are using a device or want to write the codes to perform these actions.

In this article, I would work you through the basic operators in python

piii.jpeg

Let's go ahead;

What are Operators?

Operators are simply symbols used in performing arithmetic or logical operations on one or more operands. Operands on the other hand, are the values on which these operations are performed to produce a result.

Classification of Python Operators

For the purpose of this article, we would be looking at the three(3) main classifications of python operators, which include;

2nd.png

1). Arithmetic Operators:

The arithmetic operators takes one or more operands and performs mathematical operations on them then it returns a value whose type is determined by the type of the operands. Further details of python data types would be seen in my preceding articles.

Let's see the types of arithmetic operators in python:

  • Exponent ** used for your usual math raise to power calculations
  • Modulus % used to get the remainder part of division
  • Integer division/floored quotient // gets the whole number part of division
  • Division / gives both the whole number and remainder of division
  • Multiplication *
  • Subtraction -
  • Addition +

let's see some examples of the above operators, enter the code below in your python shell

first.png

From the above notice that, 3**4, gave 81,this is same as your usual 3^4 done in mathematics. 7%2 resulted in 1 this is because when 2 divides 7, the answer you get is 3 remainder 1, %(modulus) gives the remainder and not the whole number, while // (integer divisor) gives the whole number part, also / (division operator) gives the whole result both the whole number and remainder part.

Also, - Parentheses () is used for grouping values
let's see how this can be implemented, Assume you want to add 3 to 2 then multiply it by 3, now 2+3=5 and 5*3=15, in the code below see how using the () helps group your values to obtain accurate results and what happens if otherwise.

2nd.png

2nd.png

2). Comparison Operators:

These operators evaluate two values and returns a boolean value either true or false.

The comparison operators are:

  • == Equal to checks that value a is same as value b
  • != Not equal to checks that value a is not same as value b
  • '>=' Greater than or Equal to checks that value a is greater than or equal to value b
  • <= Less than or equal to checks that value a is lesser than or equal to value b
  • '>' Greater than checks that value a is greater than value b
  • < Lesser than checks that value a is lesser than value b

Let's see some examples;

3rd.png

Next is;

2nd.png

3). Logical Operators:

These operators take two (2) operands, perform operations on them and returns a boolean value which could be either True or False

Operators in this category are used to compare or combine operands, they include;

  • and returns True if both operands are True else it returns False
  • or returns True if one or the both operands are True, else it returns False
  • not returns the negative value of an operands

Let's see some examples,

444.png

I hope this article was helpful.

Thank you for reading this, Feel free to leave a comment and/or share with your contact.

Let's connect on Twitter.