Operators in Python

Coding with Python

🕑 This lesson will take about 15 minutes

Operators are the symbols which are used to manipulate the value of operands. Take for example, the expression 6 + 4 = 10. Here, 6 and 4 are called operands and the '+' sign is called the operator. There are a variety of operators in the Python programming language. We have already looked at some of the arithmetic operators used for mathematical operators and the conditional operators used for comparing values. Let's review some of these operators and check out some more operators in the Python language and how they can be used.

Conditional operators

Conditional operators are used to compare the values on either sidesof them and determine the relation among them. They are also called relational operators or comparison operators.

With the operators below, assume we are working with a variable called a which holds the value 6 and a variable called b which holds the value 4.

  • == means “is equal to” - if the values provied are equal, then the condition evaluates to True. For example, a == b does not evaluate to True.

  • != means “is not equal to” - if the values provided are not equal, then the condition evaluates to True. For example, a != b evaluates to True.

  • > means “greater than” - if the value on the left side of the > operator is greater than the value on the right side of the > operator, then the condition evaluates to True. For example, a > b evaluates to True.

  • < means “less than” - if the value on the left side of the < operator is less than the value on the right side of the < operator, then the condition evaluates to True. For example, a < b evaluates to False.

  • >= means “greater than or equal to” - if the value on the left side of the >= operator is greater than or equal to the value on the right side of the >= operator, then the condition evaluates to True.
    For example:

    • a >= b evaluates to True

    • 7 >= 7 evaluates to True

    • 8 >= 7 evaluates to True

    • 5 >= 7 evaluates to False

  • <= means “less than or equal to” - if the value on the left side of the <= operator is less than or equal to the value on the right side of the <= operator, then the condition evaluates to True.
    For example:

    • a <= b evaluates to False

    • 9 <= 9 evaluates to True

    • 8 <= 9 evaluates to True

    • 7 <= 5 evaluates to False

Example using the > operator:

Assignment operators

Assignment operators are used to assign a value to a variable. However, they can also perform arithmetic and assign the result to a variable, for example, taking a variable's value, increasing its value by 2, and then replacing the variable’s existing value with the new value.

With the operators below, assume we are working with a variable called a which holds the value 6 and a variable called b which holds the value 4.

  • = means “equals”. This is the standard assignment operator that is used to assign a value to a variable. For example, b = 4 (where b is the variable and 4 is the value being assigned to it).

  • += is used to “add and assign”. It adds the value from the operand on the right side of the += operator to the value in the variable on the left side of the += operator and stores the result in that variable. For example, a += b is the same as writing a = a + b

  • -= is used to “subtract and assign”. It subtracts an amount from a variable and assigns the result to the variable. For example, a -= b is the same as writing a = a - b

  • *= is used to “multiply and assign”. It multiplies the right operand with the left operand and assigns the result to the left operand. For example, a *= b is the same as writing a = a * b.

  • /= is used to “divide and assign”. It divides the left operand with the right operand and assigns the result to the left operand. For example, a/= b is the same as writing a = a / b.

  • %= means “modulo and assign”. It divides the left operand with the right operand and assigns the remainder to the left operand. For example, a %= b is the same as writing a = a % b

  • **= means “exponential and assign - performs exponential calculation (power) and assigns the value to the left operand. For example, a *= b is the same as writing a = a ** b

  • //= means “floor division and assign” - it performs floor division and assigns the value to the left operand. For example, a //= b is the equivalent of a = a // b

Example

Here is a code sample using the += operator:

Logical operators

Logical operators are used to test more than one condition at once.

With the operators below, assume we are working with a variable called a which holds the value 6, a variable called b which holds the value 4, and another variable called c which also holds the value 4.

  • and - If all operands are True, then the condition will evaluate to True.
    For example:

    • a == 6 and b == 6 will return False.

    • b == 4 and c == 4 will return True.

  • or - if at least one of the operands are True, then the condition will evaluate to True.
    For example:

    • a == 6 or b == 6 will return True

    • a == 6 or b == 4 will return True

    • a == 3 or b == 5 will return False

  • not - not can be used with is not and can also be used with not in.
    For example:

    • a is 6 and b is not 6 will return True

    • a is 6 and b is not 4 will return False

    • 6 is in my_list will return True if a list called my_list contains the value 6

Example

Here is an example using the and operator:

Membership operators

Membership operators are used to test if something belongs to something else, for example, if a value belongs in a list.

With the examples below, assume we are working with a variable called a which holds the value 6, a variable called b which holds the value 9, and a list called mylist which contains the values [1,2,3,4,5,6,7].

  • in - this will evaluate to True if it finds a value that exists in the specified sequence, and False if it does not exist.
    For example:

    • a in mylist will evaluate to True

    • b in mylist will evaluate to False

  • not in - this will evaluate to True if it does not find a value in the specified sequence, and False if it does.
    For example:

    • a not in mylist will evaluate to False

    • b not in mylist will evaluate to True

Example

Here is an example using the in operator:

Identity operators

Identity operators are used to test if two variables point to the same object.

With the examples below, assume we are working with a variable called a which holds the value 6, a variable called b which also holds the value 6, and another variable called c which holds the value 3.

  • is - will evaluate to True if the variables on either side of the operator point to the same object, and False if they don't.
    For example:

    • a is b will return True

    • a is c will return False

  • is not - will evaluate to True if the variables on either side of the operator don’t point to the same object, and False if they do.
    For example:

    • a is not b will return False

    • a is not c will return True

Example

Here is an example using is not: