Python 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

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.

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.

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 are true

x < 5 and x < 10

or

Returns True if one of the statements is true

x < 5 or x < 4

not

Reverses the result

not(x < 5 and x < 10)

Identity Operators#

Identity operators compare the memory locations of two objects.

Syntax

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 test if a sequence is present in an object.

Syntax

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