Python Operators
Contents
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Identity Operators
Membership Operators
Operators in Python are used to perform operations on variables
and values. They are divided into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
stable
Arithmetic Operators
Arithmetic operators are used with numeric values to perform
common mathematical operations.
Syntax Description Example
+ Addition 3+4
- Subtraction 1-5
* Multiplication 5*6
/ Division 7/2
% Modulus 5%1
** Exponentiation 8 ** 3
// Floor division 3 // 8
Assignment Operators
Assignment operators are used to assign values to variables.
stable
Syntax Example Same as
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
Comparison Operators
Comparison operators are used to compare two or more variables.
Print to PDF stable
Syntax Description Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Logical Operators
Logical operators are used to combine conditional statements.
Syntax Description Example
and Returns True if both statements x < 5 and x < 10
are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverses the result not(x < 5 and x <
10)
stable
Identity Operators
Identity operators compare the memory locations of two objects.
Syntax Description Example
is Returns True if both variables are the same x is y
object
is not Returns True if both variables are not the x is not y
same object
Membership Operators
Membership operators test if a sequence is present in an object.
Syntax Description Example
in Returns True if a sequence with the x in y
specified value is present in the object
not in Returns True if a sequence with the x not in
specified value is not present in the object y
stable