Python

Python Operators: Arithmetic, Comparison, Logical and more

Operators refer to special symbols in Python that carry out arithmetic or logical computation. An operand is generally a variable or a value on which we operate.

1. Python Operators

There different types of python operators and we will discuss some of them below.

  • Python Arithmetic Operator
  • Python Comparison Operator
  • Python Logical Operator
  • Python Identity Operator
  • Python Bitwise Operator
  • Python Membership Operator
  • Python Assignment Operator

1.1 Python Arithmetic Operator

Arithmetic Operators are used for basic mathematical operations which include

  1. Addition (+) which adds values on either side of the operator
  2. Subtraction (-) which automatically subtracts the value on the right from the one on the left
  3. Multiplication (*) which multiplies the values on either side of the operator
  4. Division (/) which divides the value on the left by one on the right. Division results typically in a floating-point value
  5. Floor division which divides and returns the integer value of the quotient. The digits after the decimal are lost. The division results into whole number being adjusted to the left in the number line
  6. Modulus (%) it divides and returns the value of the remainder
  7. Exponentiation (**) raises the first number typically to the power of the second

1.2 Python Logical Operator

Refer to conjunctions which you can easily use to combine more than one condition. There are three Python Logical Operator-and, or, and not that come under python operators.

  1. And if the conditions on each side of the operator are true, then the expression as a whole is true.
  2. The or-the expression is false only if both statements around the operator are false. Otherwise, it is true.
  3. Not- It converts True to False, and False to True. So, not inverts it to True.

1.3 Python Comparison Operator

This carries out the comparison between operands. The operator tells whether an operand is higher than the other, lesser equal or a combination of all of them. It either brings the results as True or False depending on the condition. They include

  • Less than (<)

The operator normally checks if the value on the left of the operator is lesser than the one on the right.

  • Greater than (>)

It usually checks if the value on the left of the operator is lesser than or equal to the one on the right.

  • Less than or equal to (<=)

Checks if the value on the left of the operator is lesser than or equals to the one on the right.

  • Greater than or equal to (>=)

Checks if the value on the left of the operator is greater or equal to that one on the right.

  • Equal to (==)

The operator checks if the value on the left of the operator is equal to the one on the right.

  • Not equal to (! =)

It checks if the value on the left of the operator is not equal to the one on the right.

The Python operator <> does the same task. When the condition for a relative operator is fulfilled, it returns true, if not it returns false. The return value can be used further in a statement or expression.

1.4 Membership Python Operator

The Operators check whether a value is a member of a sequence. The sequence may be a list, a set, a string or a tuple. There are two membership python operators-in’ and ‘not in.’

a) In

This checks whether a value is a member of a sequence. Hence true if value/variable is found in the sequence.

b) Not in.

Unsimilar to ‘in’ ‘not in’ checks if a value is not located as a member of a sequence. True if value or variable is not located in the sequence.

1.5 Python Identity Operator

The operators test if the two operands share an identity. They check if two values or valuables are located on the same part of the memory. Two variables that are equal does not mean they are identical. There are two identity operators-is’ and ‘not.’

a) Is

If two operands share an identity, the outcome is true. Otherwise, the outcome is false.

b) Is not

True if the operands are not identical if they do not refer to the same object hence false. Read more from here.

1.6 Python Bitwise Operator

With the operands, it operates bit and bit hence the name. Bitwise operators act on operands as if they were a string of binary digits.

a) BinaryAND (&)

It performs bit and bit AND operation on the two values.

b) BinaryXOR (^)

It performs bit and bit XOR (exclusive-OR) on the two values.

c) BinaryOnesComplement (~)

It returns the ones complement of a numbers binary. It flips the binary.

d) Binary Left-Shift (<<)

It moves the value of the left operand the number of places to the left that the right operand specifies.

e) Binary Right Shift (>>)

It moves the value of the left operand the number of places to the right that the right operand specifies.

1.7 Python Assignment Operator

An assignment operator attaches a value to a variable. It may manipulate the value by a factor before attaching it. There are various compounds in python assignment operator.

a) Assign (=)

Attaches a value to the expression on the left. == is used for comparing, but =is used for attaching.

b) Add and assign (+=)

Adds the value on either side and assigns it to the expression on the left.

c) Subtract and assign (- =)

It subtracts the value on the right from the value on the left. Then it attaches it to the expression on the left.

d) Divide and assign (/=)

e) Multiply and Assign (*+)

It Multiplies the values on either side. Then it attaches it to the expression on the left.

f) Modulus and Assign (%-)

It shows modulus on the values on either side. Then it attaches it to the expression on the left.

g) Exponent and Assign (**)

It calculates exponentiation on the values given in either side. Then assigns it to the expression on the left.

h) Floor-Divide and Assign (//=)

It Performs floor-division on the digits on either side. Then assigns it to the expression on the left.

If you wish to improve python skill then you can learn it from here.

2. Uses of Python Operators

It is essential to learn about python operator since you can apply the concepts in very different areas of life.

  • To create web pages during web development
  • Large standard libraries that require areas like string operations, internet, web service tools, operating system interfaces, and protocols have used Python operators.
  • Python operators have robust process integration features. Hence they increase speed and productivity for most applications.
  • Creation of excellent applications and games
  • Creation of great projects using different Python operator.
  • Software development companies prefer using python operators because of its versatile and fewer programming codes.
  • For data scientists, Python Operators will help them store most of their data and even processing it.
  • Has been used for scripting to automate complicated tasks easily

Python operators have provided an easy usage of the code lines, they have made maintenance very easy and debugging too. It has gained importance worldwide, and Google is making it one of its official programming languages.

Saurabh Hooda

I have worked globally for telecom & finance giants in various capacities. My latest venture Hackr.io recommend the online programming courses for every programming language.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Todayus
4 years ago

Good information provided this network

KSHITIJ ANAND
3 years ago
Back to top button