# Addition
= 10
x = 5
y
= x + y
summation print(summation)
15
Juma Shafara
November 1, 2023
September 16, 2024
Python Operators
Operators are symbols that perform operations on operands. Operands can be variables, strings, numbers, booleans etc
Before we continue, we have a humble request, to be among the first to hear about future updates of the course materials, simply enter your email below, follow us on (formally Twitter), or subscribe to our YouTube channel.
Arithemators are symbols that perform mathematical operations on operands
Arithmetic Operator | Description |
---|---|
+ |
Addition |
- |
Subraction |
/ |
Division |
* |
Multiplication |
** |
Exponentiates |
% |
Remainder |
// |
Floor Division |
The addition operator returns the sum of its numerical operands
It can also be used to concatenate or join strings together
The subtraction operator returns the difference of its numerical operands
The multiplication operator returns the product of its numerical operands
The division operator returns the quotient of its numerical operands.
The exponentiation operator raises the left operand to the power of the right operand
The remainder operator, also knows as the modulus operator returns the remainder after dividing the left operand by the right operand
The floor division rounds down the quotient of its numerical operands to the nearest whole number.
Operator sequence describes the order of performed operations in an arithmetic expression.
Assignment operators are used to assign values to variables.
Name | Operation | Same As |
---|---|---|
Assignment | x = y |
x = y |
Addition Ass | x += y |
x = x + y |
Subtraction Ass | x -= y |
x = x - y |
Mult Ass | x *= y |
x = x * y |
Division Ass | x /= y |
x = x / y |
Expo Ass | x **= y |
x = x ** y |
Remainder Ass | x %= y |
x = x % y |
Floor Div Ass | x //= y |
x = x // y |
The assignment operator assigns a value to a variable
The addition assignment operator adds the left and right operands and assigns the sum to the left operand (the variable)
The subtraction assignment operators deducts the right operand and assigns the difference to the left operand (the variable)
A comparison operator compares its operands and returns a Boolean value based on whether the comparison is True of False
Name | Operation |
---|---|
Equality | == |
Inequality | != |
Greater than | > |
Less than | < |
Greater or equal | >= |
Less or equal | <= |
The equality operator compares two values and returns True
if the operands are equal, otherwise returns False
The inequality operator compares two values and returns True
if the operands are Not equal, otherwise returns False
The greater than operator returns True
if the left operand is greater than the right operand, otherwise returns False
.
The less than operator returns True
if the left operand is less than the right operand, otherwise returns False
The greater than or equal to operator returns True
if the left operand is greater than or equal to the right operand, otherwise returns False
The less than or equal to operator returns True
if the left operand is less than or equal to the right operand, otherwise returns False
In this example, we use the operators to calculate the body mass index of a person and check if they’re overweight, normal or underweight
Identity operators are used to compare two values to determine if they point to the same object
Operator | Name |
---|---|
is |
The is operator |
is not |
The is not operator |
is
operatorThis operator returns True
if both operands point to the same object.
is not
operatorThis operator return True
if both operands do Not point to the same object.
Logical operators are commonly used with Booleans. In Python, there are 3 logical operators
Operator | Description |
---|---|
and |
Logical and operator |
or |
Logical or |
not |
Logical not |
and
The logical and
operator returns True
if both operands are True
or
The logical or
operator returns True
if one of the operands is True
not
The logical not
operator returns True
if the operand is False
, otherwise returns False
if the operand is True
Membership operators are used to check if a sequence is present in an object like a string, list etc
Operator | Name |
---|---|
in |
The in operator |
not in |
The not in operator |
in
operatorThe in
operator returns True
if a sequence or value is present in an object
not in
operatorThe not in
operator returns True
if a sequence or value is NOT present in an object