# Python Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the ‘+’ symbol in the expression ‘1 + 2’ would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within Python that are explained below.

## Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out within a Python program.

Operator | Description |
---|---|

+ | Adds two values together. |

– | Subtracts one value from another. |

* | Multiplies two values together. |

/ | Divides one value by another. |

% | Calculates the remainder after one value is divided by another. |

** | Performs exponential (power) calculation, where the left value is raised to the power of the right value. |

// | Floor division, divides one value by another and removes anything after the decimal point, except where one of the values is negative, the result is rounded up. |

The following examples show each of these operators in action.

# Variables to use in expressions a = 2 b = 3 c = 0 c = a + b # c will equal 5. c = b - a # c will equal 1. c = a * b # c will equal 6. c = b / a # c will equal 1.5. c = b % a # c will equal 1. c = a ** b # c will equal 8. c = b // a # c will equal 1.

## Assignment Operators

Assignment operators in Python are used to assign values to a variable, the most basic of which is ‘=’. Here, the ‘=’ symbol means ‘gets set to’ rather than ‘equals’, so in the expression ‘x = y’, ‘x’ gets set to ‘y’. The arithmetic operator symbols can be combined with ‘=’ to form the assignment operators.

Operator | Description |
---|---|

= | Assigns the value of the right side operand to the left side operand. |

+= | Adds the value from the right side operand to the left side operand and assigns the value to the left side operand. |

-= | Subtracts the value of the right side operand from the left side operand and assigns the value to the left side operand. |

*= | Multiplies the value from the right side operand with the left side operand and assigns the value to the left side operand. |

/= | Divides the left side operand by the value of the right side operand and assigns the value to the left side operand. |

%= | Takes the resulting modulus of the operands on both sides and assigns it to the left hand operand. |

**= | Takes the resulting exponential (power) calculation of the operands on both sides and assigns it to the left hand operand. |

//= | Takes the resulting floor division of the operands on both sides and assigns it to the left hand operand. |

The examples below show these assignment operators in action.

a = 10 # a gets set to 10. b = 2 b += 5 # b gets set to 7. c = 10 c -= 4 # c gets set to 6. d = 2 d *= 2 # d gets set to 4. e = 20 e /= 5 # e gets set to 4.0. f = 5 f %= 2 # f gets set to 1. g = 3 g **= 4 # g gets set to 81. h = 4 h //= 3 # h gets set to 1.

## Relational Operators

Relational operators compare two values and are used, for example, in ‘if’ statements, which are described in the next section on Decision Making. The result of the comparison is either True or False.

Operator | Description |
---|---|

== | Evaluates to True if the two values are equal. |

!= | Evaluates to True if the two values are not equal. |

<> | Evaluates to True if the two values are not equal. Similar to the above. |

> | Evaluates to True if the first value is greater than the second. |

< | Evaluates to True if the first value is less than the second. |

>= | Evaluates to True if the first value is greater than or equal to the second. |

<= | Evaluates to True if the first value is less than or equal to the second. |

## Logical Operators

Logical operators are used where you want to test more than one condition at the same time, for example, in an ‘if’ statement, which is discussed in the next section on Decision Making.

Operator | Description |
---|---|

AND | Logical AND operator where operands on both sides must be True to evaluate to True. |

OR | Logical OR operator where only one of the two operands must be True to evaluate to True. |

NOT | Logical NOT operator that can be used in conjunction with ‘AND’ or ‘OR’ to reverse the resulting operand. In the case of ‘AND’, if both conditions evaluate to True, then with the use of the ‘NOT’ operator the result would be False. |

## Membership Operators

Membership operators are used to test for the presence of a value in another value or set of values, for example, in an ‘if’ statement, which is discussed in the next section on Decision Making.

Operator | Description |
---|---|

in | Evaluates to True if a value is present in another value or set of values. |

not in | Evaluates to True if a value is not present in another value or set of values. |