Python Operators Demystified
Tags: python, operators, webdev, react
Published: 2024-07-11 08:07:04

[Python operators](https://www.youtube.com/watch?v=Zs8fxcqKro4) 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.
  ```python
  x = 5 + 3   x will be 8
  ```
- `-` (Subtraction): Subtracts the second operand from the first.
  ```python
  x = 5 - 3   x will be 2
  ```
- `` (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.
  ```python
  x = 2  3   x will be 8
  ```
- `//` (Floor Division): Divides the first operand by the second and returns the largest integer less than or equal to the result.
  ```python
  x = 5 // 2   x will be 2
  ```

 2. Comparison Operators
Comparison operators compare two values and return a boolean value (`True` or `False`).

- `==` (Equal): Returns `True` if both operands are equal.
  ```python
  x = (5 == 3)   x will be False
  ```
- `!=` (Not Equal): Returns `True` if operands are not equal.
  ```python
  x = (5 != 3)   x will be True
  ```
- `>` (Greater Than): Returns `True` if the left operand is greater than the right.
  ```python
  x = (5 > 3)   x will be True
  ```
- `<` (Less Than): Returns `True` if the left operand is less than the right.
  ```python
  x = (5 < 3)   x will be False
  ```
- `>=` (Greater Than or Equal To): Returns `True` if the left operand is greater than or equal to the right.
  ```python
  x = (5 >= 3)   x will be True
  ```
- `<=` (Less Than or Equal To): Returns `True` if the left operand is less than or equal to the right.
  ```python
  x = (5 <= 3)   x will be False
  ```

 3. Logical Operators
Logical operators are used to combine conditional statements.

- `and`: Returns `True` if both statements are true.
  ```python
  x = (5 > 3 and 5 < 10)   x will be True
  ```
- `or`: Returns `True` if one of the statements is true.
  ```python
  x = (5 > 3 or 5 < 3)   x will be True
  ```
- `not`: Reverses the result, returns `False` if the result is true.
  ```python
  x = not(5 > 3)   x will be False
  ```

 4. Assignment Operators
Assignment operators are used to assign values to variables.

- `=`: Assigns a value to a variable.
  ```python
  x = 5
  ```
- `+=`: Adds and assigns the result.
  ```python
  x += 3   x will be 8 if x was 5
  ```
- `-=`: Subtracts and assigns the result.
  ```python
  x -= 3   x will be 2 if x was 5
  ```
- `=`: Multiplies and assigns the result.
  ```python
  x = 3   x will be 15 if x was 5
  ```
- `/=`: Divides and assigns the result.
  ```python
  x /= 3   x will be 1.6667 if x was 5
  ```
- `%=`: Takes modulus and assigns the result.
  ```python
  x %= 3   x will be 2 if

https://www.youtube.com/watch?v=Zs8fxcqKro4