Python Operators

Operators are special symbols that perform operations on variables and values. Python has various types of operators, including arithmetic, comparison, logical, assignment, bitwise, and membership operators.


Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

  • Addition (+):

Adds two values.


  • Subtraction (-):

Subtracts the second value from the first.


Tutorials dojo strip
  • Multiplication (*):

Multiplies two values.


  • Division (/):

Divides the first value by the second.


  • Floor Division (//):

Divides the first value by the second and returns the largest integer less than or equal to the result.


  • Modulus (%):

Returns the remainder of the division.


  • Exponentiation ()**:

Raises the first value to the power of the second.




Comparison Operators

Comparison operators compare two values and return True or False.

  • Equal to (==):

Checks if two values are equal.


  • Not equal to (!=):

Checks if two values are not equal.


  • Greater than (>):

Checks if the first value is greater than the second.


  • Less than (<):

Checks if the first value is less than the second.


  • Greater than or equal to (>=):

Checks if the first value is greater than or equal to the second.


  • Less than or equal to (<=):

Checks if the first value is less than or equal to the second.




Logical Operators

Logical operators are used to combine conditional statements.

  • and:

Returns True if both conditions are true.


  • or:

Returns True if at least one condition is true.


  • not:

Reverses the result of the condition.




Assignment Operators

Assignment operators are used to assign values to variables.

  • Assign (=):

Assigns the value 5 to x.


  • Add and assign (+=):

Adds the right operand to the left operand and assigns the result to the left operand.


  • Subtract and assign (-=):

Subtracts the right operand from the left operand and assigns the result to the left operand.


  • Multiply and assign (*=):

Multiplies the left operand by the right operand and assigns the result to the left operand.


  • Divide and assign (/=):

Divides the left operand by the right operand and assigns the result to the left operand.


  • Modulus and assign (%=):

Takes the modulus using two operands and assigns the result to the left operand.


  • Exponentiation and assign (=)**:

Raises the left operand to the power of the right operand and assigns the result to the left operand.




Python Operators Example Code

Explanation of Code:

This code demonstrates various Python operators. It includes arithmetic, comparison, logical, and assignment operations. The results are printed for each operation type.




Python Labs

Tutorials dojo strip