Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.

The special symbols in Python to perform arithmetic operations, comparison operations, logical operations, bitwise operations, assignment operations, identity operations, and membership operations on variables and values, are called operators in Python. The value on which the operation is being performed is called an operand in Python. 

Arithmetic Operators in Python:

Arithmetic operators are used, to perform arithmetic operations on numerical values. The arithmetic operators in Python are:

  •       +

The arithmetic operator + is used for addition.

  •       -

The arithmetic operator – is used for subtraction.

  •       *

The arithmetic operator * is used for multiplication.

  •       /

The arithmetic operator / is used to perform division.

  •       %

The arithmetic operator % is used for finding the modulus.

  •       **

The arithmetic operator ** is used for exponentiation.

  •       //

The arithmetic operator // is used for floor division.

Example 1:Python Program to Demonstrate Arithmetic Operators

firstvar = 20

secondvar = 4

print('firstvar + secondvar =',firstvar+secondvar)

print('firstvar - secondvar =',firstvar-secondvar)

print('firstvar * secondvar =',firstvar*secondvar)

print('firstvar / secondvar =',firstvar/secondvar)

print('firstvar // secondvar =',firstvar//secondvar)

print('firstvar ** secondvar =',firstvar**secondvar)

The output of the above program is shown in the snapshot below:

OperatorsInPython_Exp_1

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Assignment Operators in Python: 

Assignment operators are always used to assign values to variables. The assignment operators in Python are:

  •       =

The value is assigned to the variable using =

  •       +=

The value is assigned to the variable after adding the existing value of the variable.

  •       -=

The value is assigned to the variable after subtracting the existing value of the variable.

  •       *=

The value is assigned to the variable after multiplying the existing value of the variable.

  •       /=

The value is assigned to the variable after dividing the existing value of the variable.

  •       %=

The value is assigned to the variable after finding the modulus of the existing value of the variable.

  •       //=

The value is assigned to the variable after performing the floor division of the existing value of the variable.

Example 2: Python Program to Demonstrate Assignment Operators

firstvar = 20

secondvar = 4

firstvar+=5

print('firstvar = firstvar + 5 =>',firstvar)

firstvar-=5

print('firstvar = firstvar - 5 =>',firstvar)

firstvar*=5

print('firstvar = firstvar * 5 =>',firstvar)

firstvar/=5

print('firstvar = firstvar / 5 =>',firstvar)

firstvar%=5

print('firstvar = firstvar % 5 =>',firstvar)

firstvar//=5

print('firstvar = firstvar // 5 =>',firstvar)

firstvar**=5

print('firstvar = firstvar ** 5 =>',firstvar)

The output of the above program is shown in the snapshot below:

OperatorsInPython_Exp_2 

Comparison Operators in Python:

Comparison operations can be performed using comparison operators. The comparison operators are:

  •       ==

Checks if the values on either side are equal or not.

  •       !=

Checks if the values on either side are not equal.

  •       > 

Checks if the value on the left side is greater than the value on the right side. 

  •       < 

Checks if the value on the right side is greater than the value on the left side. 

  •       >=

Checks if the value on the left side is greater than or equal to the value on the right side. 

  •       <=

Checks if the value on the right side is greater than or equal to the value on the left side.

Example 3: Python Program to Demonstrate Comparison Operators

firstvar = 20

secondvar = 4

print('firstvar == secondvar=>',firstvar == secondvar)

print('firstvar != secondvar =>',firstvar != secondvar)

print('firstvar > secondvar =>',firstvar > secondvar)

print('firstvar < secondvar =>',firstvar < secondvar)

print('firstvar >= secondvar =>',firstvar >= secondvar)

print('firstvar <= firstvar =>',firstvar <= secondvar)

The output of the above program is shown in the snapshot below:

OperatorsInPython_Exp_3

Logical Operators in Python: 

Logical operators, as the name indicates are used to perform logical operations. The logical operators are:

  •       AND

AND operation returns true if both the operands are true.

  •       OR

OR operation returns true if one of the operands is true.

  •       NOT

NOT operation returns true if the operand is false.

Example 4: Python Program to Demonstrate Logical Operators

firstvar = True

secondvar = True

print('firstvar AND secondvar =>',firstvar and secondvar)

print('firstvar OR secondvar =>',firstvar or secondvar)

print('Not firstvar =>',not (firstvar))

The output of the above program is shown in the snapshot below:

OperatorsInPython_Exp_4 

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

Identity Operators in Python: 

The memory locations of the two objects can be compared using identity operators. The Identity operators are:

  •       is

Returns true in case the variables on either side of the ‘is’ operator point to the same object.

  •       is not

Returns false in case the variables on either side of the ‘is’ operator point to the same object.

Example 5: Python Program to Demonstrate Identity Operators

firstvar = True

secondvar = True

print('firstvar is secondvar =>',firstvar is secondvar)

print('firstvar is not secondvar =>',firstvar is not secondvar)

The output of the above program is shown in the snapshot below:

OperatorsInPython_Exp_5

Membership Operators in Python:

The presence of an element in the sequence can be checked using membership operators. The membership operators are:

  •       in

Returns true if the variable is present in the specified sequence.

  •       not in

Returns true if the variable is not present in the specified sequence.

Example 6: Python Program to Demonstrate Membership Operators

firstvar: str = 'Simplilearn'

print('Letter S in Simplilearn =>','S' in firstvar)

print('Letter Z in Simplilearn =>','Z' not in firstvar)

The output of the above program is shown in the snapshot below:

OperatorsInPython_Exp_6 

Bitwise Operators in Python: 

The bit-by-bit operation can be performed using bitwise operators. The bitwise operators are:

  •       &

AND operator returns 1 if both the operands are 1 otherwise 0.

  •       |

OR operator returns 1 if one of the operands is 1 otherwise 0.

  •       ^

XOR operator returns 1 if both the operands are 1 or both the operands are 0 otherwise 0.

  •       ~

One’s complement operator returns 1 if the operand is 0 and returns 0 if the operand is 1.

  •       << 

The left shift operator shifts the value on the left side to the left by the number of bits specified by the right operand.

  •       >> 

The right shift operator shifts the value on the left side to the right by the number of bits specified by the right operand.

Example 7: Python Program to Demonstrate Bitwise Operators

firstvar = 10

secondvar = 20

print('firstvar AND secondvar =>',firstvar & secondvar)

print('firstvar OR secondvar =>',firstvar | secondvar)

print('firstvar XOR secondvar =>',firstvar ^ secondvar)

print('~firstvar =>',~firstvar)

print('firstvar << 2 =>',firstvar << 2)

print('firstvar >> 2 =>',firstvar >> 2)

The output of the above program is shown in the snapshot below:

OperatorsInPython_Exp_7 

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Conclusion

In this Operators in Python tutorial, you looked into the concept of operators in Python. Simplilearn offers the Python Certification Course designed to help you learn everything in Python to kick start your career and is a very flexible way to acquire skills in Python.

Do you have any questions for us? Feel free to leave them as comments at the end of this page, and our experts will get back to you on the same, right away. Happy learning!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.