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 |
|
|
|
Subtraction |
|
|
|
Multiplication |
|
|
|
Division |
|
|
|
Floor Division |
|
|
|
Modulo |
|
|
Boolean Operators#
These operators return True
or False
(i.e. boolean data type).
Operator |
Description |
Example. |
---|---|---|
== |
Returns |
|
!= |
Returns |
|
< |
Returns |
|
> |
Returns |
|
<= |
Returns |
|
>= |
Returns |
|
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 |
|
|
|
Not Equal To |
|
|
|
Greater Than |
|
|
|
Greater Than or Equal To |
|
|
|
Less Than |
|
|
|
Less Than or Equal To |
|
|
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 |
---|---|---|---|
|
Logical AND |
|
|
|
Logical OR |
|
|
|
Logical NOT |
|
|
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 |
---|---|---|---|
|
Identity |
|
|
|
Non-Identity |
|
|
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 |
---|---|---|---|
|
Membership |
|
|
|
Non-Membership |
|
|
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 |
|
|
|
Addition Assignment |
|
|
|
Subtraction Assignment |
|
|
|
Multiplication Assignment |
|
|
|
Division Assignment |
|
|
|
Floor Division Assignment |
|
|
|
Modulo Assignment |
|
|
|
Bitwise AND Assignment |
|
|
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 |
|
|
|
Bitwise OR |
|
|
|
Bitwise XOR |
|
|
|
Bitwise NOT |
|
|
|
Bitwise Left Shift |
|
|
|
Bitwise Right Shift |
|
|
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
orFalse
.- 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
orFalse
.- logical operator#
An operator that combines two or more boolean expressions and evaluates to either
True
orFalse
.- modulo#
An operator that performs integer division and returns the remainder of the result.
Exercise#
What is the result of the following expression?
Expression |
Result |
|
---|---|---|
i. |
|
|
ii. |
|
|
iii. |
|
|
iv. |
|
|
v. |
|
|
vi. |
|
|
vii. |
|