BASIC PYTHON OPERATORS

Welcome to my blog series for beginners in python, I'd be take you through on the basics of programming in python. In this article, the focus is on operators.

piii.jpeg

From your daily mathematical calculations, to your daily comparison of items or conditions, the automation of these tasks can be done by a simple python code. 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 OPERATOR:

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

There is more to using logical operators, but that would be treated in the next article.

I hope this article was helpful.

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

Comments (2)

Rohit singh's photo

The first and Main step to more confident speaking skills a lot of listening. Every time you are communicating with someone in English. If you are searching for the best learning institute then you can contact Pragna class for an English speaking course online. We come up with new and innovative ideas

UDOKAKU UGOCHUKWU's photo

Data Science & M.L Enthusiast ||| Avid Reader & learner ||| Music Lover || Python Developer Mentee @ SheCodesAfrica

That's nice But I don't see how it relates with this. Do you mind elaborating?