DEV Community

Angelika Jolly
Angelika Jolly

Posted on

Python Operators Demystified

Python operators are symbols that perform operations on variables and values. They are used in various programming tasks, including arithmetic calculations, comparisons, logical operations, and more. Here's a detailed look at the different types of Python operators:

  1. Arithmetic Operators Arithmetic operators are used to perform mathematical operations.
  • + (Addition): Adds two operands.
  x = 5 + 3   x will be 8
Enter fullscreen mode Exit fullscreen mode
  • - (Subtraction): Subtracts the second operand from the first.
  x = 5 - 3   x will be 2
Enter fullscreen mode Exit fullscreen mode
  • (Multiplication): Multiplies two operands. python x = 5 3 x will be 15 ``
  • / (Division): Divides the first operand by the second. `python x = 5 / 2 x will be 2.5 `
  • % (Modulus): Returns the remainder of the division. `python x = 5 % 2 x will be 1 `
  • `` (Exponentiation): Raises the first operand to the power of the second.
  x = 2  3   x will be 8
Enter fullscreen mode Exit fullscreen mode
  • // (Floor Division): Divides the first operand by the second and returns the largest integer less than or equal to the result.
  x = 5 // 2   x will be 2
Enter fullscreen mode Exit fullscreen mode
  1. Comparison Operators Comparison operators compare two values and return a boolean value (True or False).
  • == (Equal): Returns True if both operands are equal.
  x = (5 == 3)   x will be False
Enter fullscreen mode Exit fullscreen mode
  • != (Not Equal): Returns True if operands are not equal.
  x = (5 != 3)   x will be True
Enter fullscreen mode Exit fullscreen mode
  • > (Greater Than): Returns True if the left operand is greater than the right.
  x = (5 > 3)   x will be True
Enter fullscreen mode Exit fullscreen mode
  • < (Less Than): Returns True if the left operand is less than the right.
  x = (5 < 3)   x will be False
Enter fullscreen mode Exit fullscreen mode
  • >= (Greater Than or Equal To): Returns True if the left operand is greater than or equal to the right.
  x = (5 >= 3)   x will be True
Enter fullscreen mode Exit fullscreen mode
  • <= (Less Than or Equal To): Returns True if the left operand is less than or equal to the right.
  x = (5 <= 3)   x will be False
Enter fullscreen mode Exit fullscreen mode
  1. Logical Operators Logical operators are used to combine conditional statements.
  • and: Returns True if both statements are true.
  x = (5 > 3 and 5 < 10)   x will be True
Enter fullscreen mode Exit fullscreen mode
  • or: Returns True if one of the statements is true.
  x = (5 > 3 or 5 < 3)   x will be True
Enter fullscreen mode Exit fullscreen mode
  • not: Reverses the result, returns False if the result is true.
  x = not(5 > 3)   x will be False
Enter fullscreen mode Exit fullscreen mode
  1. Assignment Operators Assignment operators are used to assign values to variables.
  • =: Assigns a value to a variable.
  x = 5
Enter fullscreen mode Exit fullscreen mode
  • +=: Adds and assigns the result.
  x += 3   x will be 8 if x was 5
Enter fullscreen mode Exit fullscreen mode
  • -=: Subtracts and assigns the result.
  x -= 3   x will be 2 if x was 5
Enter fullscreen mode Exit fullscreen mode
  • =: Multiplies and assigns the result.
  x = 3   x will be 15 if x was 5
Enter fullscreen mode Exit fullscreen mode
  • /=: Divides and assigns the result.
  x /= 3   x will be 1.6667 if x was 5
Enter fullscreen mode Exit fullscreen mode
  • %=: Takes modulus and assigns the result.

python
  x %= 3   x will be 2 if

https://www.youtube.com/watch?v=Zs8fxcqKro4
Enter fullscreen mode Exit fullscreen mode

Top comments (0)