Archangel Macsika Sikademy Image
Archangel Macsika

Complete Guide to Using the Various Python Operators

Tutorial 9 of 10 | 11 minutes read
Complete Guide to Using the Various Python Operators - Sikademy

Operators in Python

Operators in programming are symbolic constructs used to perform different operations on variables and values.

These operations can be arithmetic, comparison, assignment, logical, etc.

For example:



x = 1
y = 4
z = x + y

In the example above, x and y are variables assigned 1 and 4 respectively using the = operator. Afterward, we use the + operator to add together x and y and assign the outcome to z.

Types of Operators in Python

In Python programming language, operators are grouped into different categories based on their functions.

  • Arithmetic operators

  • Bitwise operators

  • Assignment operators

  • Comparison operators

  • Logical operators

  • Special operators

    • Identity operators

    • Membership operators

    Arithmetic Operators in Python

    Arithmetic operators are used for basic mathematical operations on numeric values or variables bearing numeric values.

    OperatorNameExampleDescriptionCode Example
    +Additionx + yAdds x and y.
    x, y = 5, 2
    print(x + y)
    
    Output: 7
    -Subtractionx - ySubtracts y from x.
    
    x, y = 5, 2
    print(x - y)
    
    Output: 3
    *Multiplicationx * yMultiply x and y.
    
    x, y = 5, 2
    print(x * y)
    
    Output: 10
    **Exponentx ** yRaises x to the power of y.
    
    x, y = 5, 2
    print(x ** y)
    
    Output: 25
    /Divisionx / yDivide x by y and returns the result.
    
    x, y = 5, 2
    print(x / y)
    
    Output: 2.5
    %Modulusx % yDivide x by y and returns the remainder.
    
    x, y = 5, 2
    print(x % y)
    
    Output: 1
    //Floor Divisionx // yDivides x by y and rounds the result down to the nearest whole number. If one of the operands (x or y) is negative, the result is rounded towards negative infinity.
    
    x, y = 5, 2
    print(x / y)
    
    x, y = 5, -2
    print(x / y)
    
    Output:
    2
    -3
    

    Bitwise Operators in Python

    These operators are used on numbers for comparison.

    During the comparison, they treat numbers as strings of binary digits (bits), written in twos-complement binary, rather than a single numeric value.

    To solve the examples in the table below, let x = 7 (0000 0111 in binary form) and y = 2 (0000 0010 in binary form).

    OperatorNameExampleDescriptionCode Example
    &Bitwise ANDx & yPerforms a "bitwise and". Sets each bit to 1 if the bit of both x and y is 1, otherwise, it's 0.
    x, y = 7, 2
    print(x & y)
    
    Output: 2
    |Bitwise ORx | yPerforms a "bitwise or". Sets each bit to 1 if either or both bit of x and y is 1, otherwise, it's 0.
    x, y = 7, 2
    print(x | y)
    
    Output: 7
    ^Bitwise XORx ^ yPerforms a "bitwise exclusive or". Sets each bit to 1 if x is 0 and y is 1, or x is 1 and y is 0, otherwise, if both are the same, it's 0.
    x, y = 7, 2
    print(x ^ y)
    
    Output: 5
    ~Bitwise NOT~xPerforms a "bitwise not". Inverts the bit of x by switching each 1 for a 0 and each 0 for a 1.
    x = 2
    print(~x)
    
    Output: -3
    >>Bitwise Right Shiftx >> yPerforms a "bitwise right shift". Shifts the bits of x to the right by the number of bits specified in y.
    x, y = 7, 2
    print(x >> y)
    
    Output: 1
    <<Bitwise Left Shiftx << yPerforms a "bitwise left shift". Shifts the bits of x to the right by the number of bits specified in y, and new bits on the right-hand-side are zeros.
    x, y = 7, 2
    print(x << y)
    
    Output: 28

    Assignment Operators in Python

    To assign values to variables in python programming, you make use of assignment operators.

    OperatorNameExampleDescriptionCode Example
    =Assignx = 7Assigns the value, 7, to the variable x.
    x = 7
    print(x)
    
    Output: 7
    +=Add and Assignx += 2Same as x = x + 2. Adds 2 to the variable x and assigns it back to the variable x.
    
    x = 7
    x += 2
    print(x)
    
    Output: 9
    -=Subtract and Assignx -= 2Same as x = x - 2. Subtracts 2 from the variable x and assigns it back to the variable x.
    
    x = 7
    x -= 2
    print(x)
    
    Output: 5
    *=Multiply and Assignx *= 2Same as x = x * 2. Multiplies the variable x by 2 and assigns it back to the variable x.
    
    x = 7
    x *= 2
    print(x)
    
    Output: 14
    **=Exponent and Assignx **= 2Same as x = x ** 2. Raises the variable x to the power of 2 and assigns it back to the variable x.
    
    x = 7
    x **= 2
    print(x)
    
    Output: 49
    /=Divide and Assignx /= 2Same as x = x / 2. Divides the variable x by 2 and assigns it back to the variable x.
    
    x = 7
    x /= 2
    print(x)
    
    Output: 3.5
    %=Modulus and Assignx %= 2Same as x = x % 2. Divides the variable x by 2, returns the remainder and assigns it back to the variable x.
    
    x = 7
    x %= 2
    print(x)
    
    Output: 1
    //=Floor Division and Assignx //= 2Same as x = x // 2. Performs floor division on the variable x and 2, and assigns it back to the variable x.
    
    x = 7
    x //= 2
    print(x)
    
    Output: 3
    &=Bitwise AND and Assignx &= 2Same as x = x & 2. Performs Bitwise AND to the variable x and 2, and assigns it back to the variable x.
    
    x = 7
    x &= 2
    print(x)
    
    Output: 2
    |=Bitwise OR and Assignx |= 2Same as x = x | 2. Performs Bitwise OR to the variable x and 2, and assigns it back to the variable x.
    
    x = 7
    x |= 2
    print(x)
    
    Output: 7
    ^=Bitwise Exclusive OR and Assignx ^= 2Same as x = x ^ 2. Performs Bitwise Exclusive OR to the variable x and 2, and assigns it back to the variable x.
    
    x = 7
    x ^= 2
    print(x)
    
    Output: 5
    <<=Binary Left Shift and Assignx <<= 2Same as x = x << 2. Performs Bitwise Binary Left Shift to the variable x and 2, and assigns it back to the variable x.
    
    x = 7
    x <<= 2
    print(x)
    
    Output: 28
    >>=Binary Right Shift and Assignx >>= 2Same as x = x >> 2. Performs Bitwise Binary Right Shift to the variable x and 2, and assigns it back to the variable x.
    
    x = 7
    x >>= 2
    print(x)
    
    Output: 1

    Comparison Operators in Python

    Comparison operators are used to compare two or more values, statements, variables etc.

    OperatorNameExampleDescriptionCode Example
    ==Equalx == yChecks if value of x equals y.
    x, y = 5, 2
    print(x == y)
    
    Output: False
    !=Not Equalx != yChecks if value of x is not equal to y.
    x, y = 5, 2
    print(x != y)
    
    Output: True
    >Greater Thanx > yChecks if value of x is greater than y.
    x, y = 5, 2
    print(x > y)
    
    Output: True
    <Less Thanx < yChecks if value of x is less than y.
    x, y = 5, 2
    print(x < y)
    
    Output: False
    >=Greater Thanx >= yChecks if value of x is greater than or equal to y.
    x, y = 5, 2
    print(x >= y)
    
    Output: True
    <=Less Thanx <= yChecks if value of x is less than or equal to y.
    x, y = 5, 2
    print(x <= y)
    
    Output: False

    Logical Operators in Python

    These operators are used to combine two or more statements. They can be used to compare statements to make a decision based on an outcome.

    Logical operators are the and, or, and not operators.

    OperatorNameExampleDescriptionCode Example
    andLogical ANDx == a and x == bReturns true if x has same value as a and b. Returns false if x's value differs from a, b, or both.
    x, a, b = 5, 2, 5
    print(x == a and x == b)
    
    Output: False
    orLogical ORx == a or x == bReturns true if x has same value as a or b or both. Returns false if x's value differs from a and b.
    x, a, b = 5, 2, 5
    print(x == a and x == b)
    
    Output: True
    notLogical NOTnot(x == a)Checks if x has same value as a, and returns the opposite of the result. That is, it returns true if false and returns false if true.
    x, a = 5, 2
    print(not(x == a))
    
    Output: True

    Special Operators in Python

    There some special operators used particularly in Python programming language that is worth mentioning.

    These operators are the identity operator and the membership operator./p>

    Identity Operators in Python

    Identity operators are used to compare two values or variables by checking if they are located on the same part of the memory.

    Two variables or values that are similar does not mean that they are identical.

    Unlike the comparison operators which check if two values are similar using their form, the identity operator checks if they are identical using their memory location.

    Identity operators are the is and is not operators.

    OperatorNameExampleDescriptionCode Example
    isIs same objectx is yReturns true if x has the same memory location as y. Otherwise, it returns false.
    x, y = 5, 2
    print(x is y)
    x, y = 5, 5
    print(x is y)
    
    Output:
    False
    True
    
    is notIs Not same objectx is not yReturns true if x is not located in the same place in memory as y. Otherwise, it returns false.
    x, y = 5, 2
    print(x is not y)
    x, y = 5, 5
    print(x is not y)
    
    Output:
    True
    False
    

    Membership Operators in Python

    Membership operators are used to check if a value or variable is found in a sequence such as string, list, tuple, set, and dictionary.

    In the case of a dictionary, Python allows us to only test the key and not the value.

    Identity operators are the in and not in operators.

    OperatorNameExampleDescriptionCode Example
    inIn objectx in yReturns true if x can be found in y. Otherwise, it returns false.
    x = 5
    y = [0,1,2,3,4,5,6,7,8,9]
    print(x in y)
    
    Output: True
    not inNot In objectx not in yReturns true if x can not be found in y. Otherwise, it returns false.
    x = 5
    y = [0,1,2,3,4,5,6,7,8,9]
    print(x not in y)
    
    Output: False

    What is the Precedence of Operators in Python?

    Precedence of operators is the sequence in which operators will be processed and evaluated when used in a mathematical expression.

    It is similar to the "BODMAS" and "PEMDAS" used in mathematics.

    The operators are evaluated from the highest to the lowest. The operator(s) with the highest precedence evaluated first followed by those with the lowest.

    For example, x = 2 + 3 * 2; here, x is assigned 8, not 10 because the operator * has higher precedence than +, so it first multiplies 3 * 2 and then is added to 2.

    The table below shows the precedence of operators from highest to lowest. In python programming, some operators are ranked the same.

    RankOperatorName
    1**Exponent
    2~ + -Complement, unary plus and minus (method names for the last two are [email protected] and [email protected])
    3* / % //Multiply, divide, modulo and floor division
    4+ -Addition and Subtraction
    5>> <<Right and left bitwise shift
    6&Bitwise 'AND'
    7^|Bitwise exclusive 'OR' and regular 'OR'
    8<= < > >=Comparison operators
    9<> == !=Equality operators
    10= %= /= //= -= += *= **=Assignment operators
    11is is notComparison operators
    12in not inMembership operators
    13not or andLogical operators

    Wrap Off

    Operators in programming are used to perform different operations on variables and values. These operations can be arithmetic, comparison, assignment, logical, etc.

    In Python programming language, operators are grouped into different categories: Arithmetic operators, Bitwise operators, Assignment operators, Comparison operators, Logical operators, and Special operators like Identity operators and Membership operators.

    If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.

    Tutorial Exercise

    if x = 5 and y = 7. Use comparison operators and identity operators to test if they are similar and identical.


    Enjoy this Tutorial? Please don't forget to share.