Home / Posts / Basic Operators in Python

Basic Operators in Python

Like other programming languages, Python has operators which help us to perform operations.
As we know the operators perform operations on operands.

A + B Here “+” is the operator and A and B are the Operands.

The basic types of operators in Python:

  1. Arithmetic Operators
  2. Relational Operators (Comparision)
  3. Logical operators
  4. Assignment operators
  5. Bitwise operators
  6. Identity operators
  7. Membership operators

Let us have look at one by one type of operators

  1. Arithmetic Operators:

As we know those operators are useful to perform arithmetic operations such as addition, subtraction, multiplication, and division they are called Arithmetic operators.

  • + (Plus) – Addition
  • (Minus) – Subtraction
  • * (Asterisk) – Multiplication
  • / (slash) – Division – it gives the quotient
  • % (Percentage) – Modulus remainder – It gives the remainder as a result.
  • ** (Double asterisk) – Exponential – It is nothing but exponential of two operands.
  • // (Double Slash) – Floor division – It gives the result as quotient as a whole number. This means that it removes the number after the decimal in case a result is a decimal number.

See the below example for a better understanding.

>>> a = 10
>>> b = 2
>>> print("addition of a+b", a+b)
addition of a+b 12
>>> print("subtraction of a-b", a-b)
subtraction of a-b 8
>>> print("Multiplication of a*b", a*b)
Multiplication of a*b 20
>>> print("Division of a/b", a/b)
Division of a/b 5.0
>>> print("Modulus Remainder of a%b", a%b)
Modulus Remainder of a%b 0
>>> print("Exponential of a**b", a**b)
Exponential of a**b 100
>>> print("Floor Division of a//b", a//b)
Floor Division of a//b 5
>>> a = 19
>>> b = 3
>>> print("Floor Division of a//b", a//b)
Floor Division of a//b 6
>>> print("Division of a/b", a/b)
Division of a/b 6.333333333333333
>>> print("Modulus Remainder of a%b", a%b)
Modulus Remainder of a%b 1
>>> 

In the above example, we are able to understand the floor division, modulus division, and normal division.

Relationship Operators (Comparision Operators):

These operators are useful to compare both side values either which are bigger, smaller, or equal, etc to be determined using these operators.

  1. == (Double Equal) – This operator is useful to compare the equality between two values. for example, a == b if a and b are both are having same values it is true otherwise false.
    a = 5 and b = 6 then a == b is false.
  2. != (Exclamation equal) – This operator is useful to compare inequality between two values. For example, a != b if a and b are both are not having the same values.
    a = 5 and b = 6 then a != b is True.
  3. < (Less than) – This operator is useful when comparing two values, when the left-hand value is smaller than the right-hand side value then it is true.
    a = 5 and b = 6 then a < b is true.
  4. > (Greater than) – This operator is useful when comparing two values, when the left-hand value is bigger than the right-hand side value then it is true.
    a = 6 and b = 5 then a > b is True.
  5. >= (Greater than or Equal) – This operator is useful in performing to find the relation between two values, If the value of the left-hand side is greater than or equal then it is true.
    a = 6 and b = 6 then a >= b is True
  6. <= (Less than or equal) – If the left-hand side value of the operator is less than or equal to the right-hand side value then it is true otherwise it is false.
    a = 5 and b = 6 then a <= b is True
>>> a = 6
>>> b = 8
>>> print(a==b)
False
>>> print(a!=b)
True
>>> print(a<b)
True
>>> print(a>b)
False
>>> print(a>=b)
False
>>> print(a<=b)
True
>>> 

About GSK

Hi, i am Santosh Gadagamma, a tutor in Software Engineering and an enthusiast for sharing knowledge in Computer Science and other domains. I developed this site to share knowledge to all the aspirants of technologies like, Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts and all levels of software project design, development, deployment, and maintenance. As a programmer I believe that, "The world now needs computers to function." Hope, this site guides you as a learning tool towards greater heights. I believe that Education has no end points and i wish to learn more in the process of teaching you.

Check Also

Design your Logo

Logo Maker Design your organization or firm or company logo by following simple steps.