Python Arithmetic Operators

In Python, arithmetic operators are used to perform mathematical operations on numerical values. Here are the primary arithmetic operators in Python:

**Addition (**: Adds two operands.`+`

)**Subtraction (**: Subtracts the second operand from the first.`-`

)**Multiplication (**: Multiplies two operands.`*`

)**Division (**: Divides the first operand by the second. Returns a float.`/`

)**Floor Division (**: Divides the first operand by the second and rounds down to the nearest whole number.`//`

)**Modulus (**: Returns the remainder of the division of the first operand by the second.`%`

)**Exponentiation (**: Raises the first operand to the power of the second operand.`**`

)

Here’s an example demonstrating the use of arithmetic operators in Python:

```
x = 10
y = 3
# Addition
print(x + y) # Output: 13
# Subtraction
print(x - y) # Output: 7
# Multiplication
print(x * y) # Output: 30
# Division
print(x / y) # Output: 3.3333333333333335
# Floor Division
print(x // y) # Output: 3
```

Python Assignment Operators

Operator | Exam | Explanation | Remarks |
---|---|---|---|

= | x = 5 | Assigns the value 5 to the variable x. | Simple assignment |

+= | x += 3 | Adds 3 to the current value of x and assigns the result to x. | Short for x = x + 3 |

-= | x -= 2 | Subtracts 2 from the current value of x and assigns the result to x. | Short for x = x – 2 |

*= | x *= 4 | Multiplies the current value of x by 4 and assigns the result to x. | Short for x = x * 4 |

/= | x /= 2 | Divides the current value of x by 2 and assigns the result to x. | Short for x = x / 2. |

%= | x %= 3 | Computes the remainder of x divided by 3 and assigns the result to x. | Short for x = x % 3 |

//= | x //= 2 | Performs floor division of x by 2 and assigns the result to x. | Short for x = x // 2 |

**= | x **= 3 | Raises x to the power of 3 and assigns the result to x. | Short for x = x ** 3 |

&= | x &= 5 | Performs a bitwise AND operation between x and 5 and assigns the result to x. | Short for x = x & 5 |

|= | x |= 3 | Performs a bitwise OR operation between x and 3 and assigns the result to x. | Short for x = x | 3 |

^= | x ^= 2 | Performs a bitwise XOR operation between x and 2 and assigns the result to x. | Short for x = x ^ 2 |

>>= | x >>= 1 | Performs a right shift of x by 1 bit and assigns the result to x. | Short for x = x >> 1 |

<<= | x <<= 2 | Performs a left shift of x by 2 bits and assigns the result to x. | Short for x = x << 2 |

Python comparison operator

Certainly! Here are Python comparison operators in a tabular format:

Symbol | Operator | Example |
---|---|---|

== | Equal to | x == y (True if x is equal to y) |

!= | Not equal to | x != y (True if x is not equal to y) |

> | Greater than | x > y (True if x is greater than y) |

< | Less than | x < y (True if x is less than y) |

>= | Greater than or equal to | x >= y (True if x is greater than or equal to y) |

<= | Less than or equal to | x <= y (True if x is less than or equal to y) |