Mathematical Operators#

Important

This page is under construction. All or part of the content may be incomplete or incorrect.

Arithmetic Operators#

The following table lists the arithmetic operators in Python. The first four operators are the basic arithmetic operators. The last two operators are the floor division and modulo operators, respectively.

Operator

Description

Example

Result

+

Addition

1 + 2

3

-

Subtraction

1 - 2

-1

*

Multiplication

1 * 2

2

/

Division

1 / 2

0.5

//

Floor Division

1 // 2

0

%

Modulo

1 % 2

1

Boolean Operators#

These operators return True or False (i.e. boolean data type).

Operator

Description

Example.

==

Returns True if left hand operand is equal to right hand operand. Otherwise, returns False

a+b == b+a

!=

Returns True if left hand operand is NOT equal to right hand operand. Otherwise, returns False.

a*b != b/a

<

Returns True if left hand operand is less than right hand operand. Otherwise, returns False.

b/a < b

>

Returns True if left hand operand is greater than right hand operand. Otherwise, returns False.

a*b > b

<=

Returns True if left hand operand is less than or equal to right hand operand. Otherwise, returns False.

a-b <= a

>=

Returns True if left hand operand is greater than or equal to right hand operand. Otherwise, returns False.

a*b >= b*a

a = 2
b = 10
a+b == b+a
True
a+b != a*b
True
a**2 == a*2
True
a+b < a+b
False
a+b <= a+b
True

Comparison Operators#

The following table lists the comparison operators in Python. These operators compare two operands and evaluate to either True or False.

Operator

Description

Example

Result

==

Equal To

1 == 2

False

!=

Not Equal To

1 != 2

True

>

Greater Than

1 > 2

False

>=

Greater Than or Equal To

1 >= 2

False

<

Less Than

1 < 2

True

<=

Less Than or Equal To

1 <= 2

True

Logical Operators#

The following table lists the logical operators in Python. These operators combine two or more boolean expressions and evaluate to either True or False.

Operator

Description

Example

Result

and

Logical AND

True and False

False

or

Logical OR

True or False

True

not

Logical NOT

not True

False

Identity Operators#

The following table lists the identity operators in Python. These operators compare the memory addresses of two operands and evaluate to either True or False.

Operator

Description

Example

Result

is

Identity

1 is 1

True

is not

Non-Identity

1 is not 1

False

Membership Operators#

The following table lists the membership operators in Python. These operators evaluate to either True or False depending on whether a value is a member of a sequence.

Operator

Description

Example

Result

in

Membership

1 in [1, 2, 3]

True

not in

Non-Membership

1 not in [1, 2, 3]

False

Assignment Operators#

We have already seen the assignment operator = in the previous sections.

The assignment operator has many variations that combine assignment with another operator (primarily arithmetic operators).

The following table lists the assignment operators in Python. These operators assign a value to a variable.

Operator

Description

Example

Result

=

Assignment

x = 1

1

+=

Addition Assignment

x += 1

2

-=

Subtraction Assignment

x -= 1

1

*=

Multiplication Assignment

x *= 2

2

/=

Division Assignment

x /= 2

1

//=

Floor Division Assignment

x //= 2

0

%=

Modulo Assignment

x %= 2

1

&=

Bitwise AND Assignment

x &= 0b101

0b101

Bitwise Operators#

The following table lists the bitwise operators in Python. These operators combine two or more bit patterns and evaluate to a new bit pattern.

Operator

Description

Example

Result

&

Bitwise AND

0b101 & 0b110

0b100

|

Bitwise OR

0b101 | 0b110

0b111

^

Bitwise XOR

0b101 ^ 0b110

0b011

~

Bitwise NOT

~0b101

0b010

<<

Bitwise Left Shift

0b101 << 1

0b1010

>>

Bitwise Right Shift

0b101 >> 1

0b10



Glossary#

arithmetic operator#

An operator that performs a mathematical operation on two operands.

assignment operator#

An operator that assigns a value to a variable.

bitwise operator#

An operator that performs a bitwise operation on two or more bit patterns.

boolean operator#

An operator that returns a boolean value.

comparison operator#

An operator that compares two operands and evaluates to either True or False.

floor division#

An operator that performs integer division and returns the floor of the result.

identity operator#

An operator that compares the memory addresses of two operands and evaluates to either True or False.

logical operator#

An operator that combines two or more boolean expressions and evaluates to either True or False.

modulo#

An operator that performs integer division and returns the remainder of the result.



Exercise#

  1. What is the result of the following expression?

Expression

Result

i.

1 + 2 * 3

ii.

(1 + 2) * 3

iii.

1 + 2 * 3 - 4 / 5

iv.

1 + 2 * (3 - 4) / 5

v.

1 + 2 * 3 - 4 / 5 ** 6

vi.

1 + 2 * 3 - 4 / 5 ** 6 % 7

vii.

1 + 2 * 3 - 4 / 5 ** 6 % 7 // 8