Python Operators :
Arithmatic Operators
Arithmetic operators are used with numeric values to perform mathematical operations.
x = 15.5
y = 2
Operator | Name | Syntax | Example |
---|---|---|---|
+ | Addition | x + y | 17.7 |
- | Subtraction | x - y | 13.5 |
* | Multiplication | x * y | 31.0 |
/ | Division | x / y | 7.75 |
% | Modulus or Remainder | x % y | 1.5 |
** | Exponentiation | x ** y | 240.25 |
// | Floor division | x // y | 7.0 |
Assignment Operators
Assignment operators are used to assign values to variables:
Operator | Syntax | Result value |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x+5 |
-= | x -= 5 | x = x-5 ..Same As for other arithmatic Operator(*, /, ** ...) |
Comparison Operators
Comparison operators are used to compare two values.
Operator | Name | Example | Return type |
---|---|---|---|
== | Equal | x == 5 | boolean |
!= | Not Equal | x != 5 | boolean |
> | Greater than | x > 5 | boolean |
< | Less than | x < 5 | boolean |
>= | Greater than or Equal to | x >= 5 | boolean |
<= | Less than or Equal to | x <= 5 | boolean |
Logical Operators
Logical operators are used to combine conditional statements
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | x > 5 and y < 10 |
or | Returns True if one of statements is true | x > 5 or y < 10 |
not | Reverse the result, returns False if the result is true | not(x > 5 and y < 10) |
Identity Operators
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Operator | Description | Example |
---|---|---|
is | Returns True if both variables are the same object | x is y |
is not | Returns True if both variables are not the same object | x is not y |
Membership Operators
Membership operators are used to test if a sequence is presented in an object
Operator | Description | Example |
---|---|---|
in | Returns True if a sequence with the specified value is present in the object. | x in y |
not in | Returns True if a sequence with the specified value is not present in the object. | x not in y |