Quick Enquiry Form

×

    EnquiryEnquire Now

    Quick Enquiry

      Operators in Python

      Blog

      Operators in Python

      Operators in Python

      Python is one of the top programming languages for the future. Operators are generally used in Python programming to manipulate variables and values. These are common symbols that are employed in arithmetic and logic operations. We shall examine various Python operator types in this article. 

      Python Operators

      Operators are symbols or unique keywords in Python that are used to manipulate variables and values. In Python, some typical operator types are as follows:

      Arithmetic Operators

      Arithmetic operators are utilized for performing operations in mathematics, such as division, multiplication, and subtraction. Instances include +, -, /, % (modulo), and * (exponentiation).

      a = 10

      b = 3

      print(“Addition:”, a + b)

      print(“Subtraction:”, a – b)

      print(“Multiplication:”, a * b)

      print(“Division:”, a / b)

      print(“Modulo:”, a % b)

      print(“Exponentiation:”, a ** b)

      OperatorDescriptionSyntax
      +To add two operandsx + y
      To subtract two operandsx – y
      *To multiply two operandsx * y
      /To divide the first operand by the second (float).x / y
      //To divide the first operand by the second (floor).x // y
      %Once the first operand is divided by the second, to return the remainderx % y
      **To return raised to power secondx ** y
      Division Operators

      In the Python programming language, you can divide two numbers and get the quotient back; that is, you can divide the first number on the left by the second number on the right and get the quotient. 

      Two categories of division operators exist:

      • Float Division
      • Floor Division
      Float Division

      Regardless of whether two numbers are integers or floats, the quotient that this operator returns is a float value. As an illustration: 

      Example: The code divides data and publishes the outcome. It shows that divisions with integers and divisions with floating points both produce accurate results. For example, “10/2” yields “5.0,” whereas “-10/2” yields “-5.0.”

      print(5/5) 

      print(10/2) 

      print(-10/2) 

      print(20.0/2) 

      Output

      1.0

      5.0

      -5.0

      10.0

      Integer Division (Floor Division)

      The given input determines the quotient that this operator returns. It returns output in floats if any of the numbers are floats. Because the output will be floored if any integer is negative, it is also known as floor division. As an illustration:

      Example: This code uses the ‘//’ operator to show how to divide an integer (the floor). It yields the following outcomes: The terms “10//3” and “-5//2” correspond to “3,” “5.0//2” and “2.0,” and “-5.0//2” and “-3.0,” respectively. The greatest integer that is less than or equal to the division result is returned by integer division.

      print(10//3) 

      print (-5//2) 

      print (5.0//2) 

      print (-5.0//2)

      Output

      3

      -3

      2.0

      -3.0

      Python’s Arithmetic Operator Sequence

      Python’s arithmetic operators are arranged as follows in order of precedence:

      P: Parenthesis

      E: Exponentiation.

      M: Multiplication (the order of precedence for multiplication and division is the same)

      D: Division

      A: Addition (the precedence of addition and subtraction is the same)

      S – Subtraction

      The modulus operator facilitates the extraction of a number’s last digit or digits. As an illustration:

      x% 10 -> Produces the last number

      x%100 -> Produces the last two digits 

      Arithmetic Operators Including Modulo, Power, Addition, Subtraction, and Multiplication

      Here is an illustration of how several Python arithmetic operators operate:

      Example: The code uses the values “a” and “b” to carry out simple arithmetic operations. It computes the fraction (‘%’), multiplies (‘*’), adds (‘+’), subtracts (‘-‘), and raises a to the power of (‘b (**)’). These operations’ outcomes are printed.

      a = 9

      b = 4

      add = a + b   

      sub = a – b 

      mul = a * b 

      mod = a % b 

      p = a ** b 

      print(add) 

      print(sub) 

      print(mul) 

      print(mod) 

      print(p) 

      Output:

      13

      5

      36

      1

      6561

      Learn what a goto statement is in Python.

      Comparison Operators in Python

      Operators for comparison are utilized for value comparison. Instances include: == (equal to),!= (not equal to), > (greater than), < (less than), >= (greater than or equal to), and \= (less than or equal to).

      x = 5

      y = 10

      print(“Equal to:”, x == y)

      print(“Not equal to:”, x != y)

      print(“Greater than:”, x > y)

      print(“Less than:”, x < y)

      print(“Greater than or equal to:”, x >= y)

      print(“Less than or equal to:”, x <= y)

      OperatorDescriptionSyntax
      >If the left operand is larger than the right, then the statement is true.x > y
      <If the left operand is less than the right, then the statement is true.x < y
      ==True if both operands are equal.x == y
      !=True if the operands are not identical, but not equal tox != y
      >=Greater than or comparable to If the left operand is larger than or equal to the right, then it is true.x >= y
      <=Less than or comparable to If the left operand is equal to or less than the right, then it is true.x <= y

      == is a comparison operator and = is an assignment operator. 

      Python’s Comparison Operator Hierarchy

      The precedence of comparison operators in Python is lower than that of arithmetic operators. Within comparison operators, every operator has the same order of precedence. Let’s look at an illustration of a Python comparison operator.

      Example: The code uses a variety of comparison operators to compare the values of “a” and “b,” printing the results. It determines whether “a” is less than, equal to, bigger than, equal to, not equal to, and less than or equal to “b.” 

      a = 13

      b = 33

      print(a > b) 

      print(a < b) 

      print(a == b) 

      print(a != b) 

      print(a >= b) 

      print(a <= b) 

      Output

      False

      True

      False

      True

      False

      True

      Explore method overloading in Python.

      Logical Operators

      Used to combine conditional statements. Examples: and (logical AND), or (logical OR), not (logical NOT).

      p = True

      q = False

      print(“p and q:”, p and q)

      print(“p or q:”, p or q)

      print(“not p:”, not p)

      OperatorDescriptionSyntax
      and Logical AND: Valid when both operands are true.x and y
      orLogical OR: True if any of the operands is truex or y
      notLogical NOT: If the operand is false, then truenot x
      Precedence of Logical Operators in Python

      In Python, logical operators are arranged as follows:

      Examples of logical operators in Python, or Logical not Logical and Logical

      The Python code that follows demonstrates how to use logical operators:

      Example: The code uses boolean values to carry out logical operations. It uses the operators “not” to negate the value of “a” and “and” to check if “b” and “a” are both true (or “and”). The outcomes are printed appropriately.

      a = True

      b = False

      print(a and b) 

      print(a or b) 

      print(not a) 

      Output

      False

      True

      False

      Suggested Article: Object Methods in Python.

      Bitwise Operators

      Used to perform bitwise operations on integers. Examples: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift).

      a = 5  # 0101 in binary

      b = 3  # 0011 in binary

      print(“Bitwise AND:”, a & b)   # Output: 1 (0001 in binary)

      print(“Bitwise OR:”, a | b)    # Output: 7 (0111 in binary)

      print(“Bitwise XOR:”, a ^ b)   # Output: 6 (0110 in binary)

      print(“Bitwise NOT for a:”, ~a) # Output: -6 (inverts bits and adds one)

      print(“Bitwise left shift:”, a << 1) # Output: 10 (1010 in binary, shifting a to the left by one)

      print(“Bitwise right shift:”, a >> 1) # Output: 2 (0010 in binary, shifting a to the right by one)

      OperatorDescriptionSyntax
      &Bitwise ANDx & y
      |Bitwise ORx | y
      ~Bitwise NOT~ x 
      ^Bitwise XORx ^ y
      >>Bitwise right sidex >>
      <<Bitwise left sidex << y
      Bitwise Operator Priorities in Python

      In Python, bitwise operators are arranged as follows:

      Bitwise NOT Intriguing Python Bitwise Operators: Shift Bitwise AND Bitwise XOR Bitwise OR Bitwise Operators Here’s an example of how bitwise operators in Python operate:

      Example: Using the values “a” and “b,” the code illustrates several bitwise operations. It executes the following bitwise operations: left shift (\\), right shift (>>), OR (|), NOT (~), XOR (^), and AND (&). The output is printed. These procedures modify the binary representations of the integers.

      a = 10

      b = 4

      print(a & b) 

      print(a | b) 

      print(~a) 

      print(a ^ b) 

      print(a >> 2) 

      print(a << 2) 

      Output

      0

      14

      -11

      14

      2

      40

      Recommended Read: Jump Statements in Python.

      Assignment Operators in Python

      It is used to assign values to variables. Examples: = (assign), += (add and assign), -= (subtract and assign), = (multiply and assign), /= (divide and assign), %= (modulo and assign), *= (exponentiate and assign).

      x = 5

      x += 3

      print(x)  # Output: 8

      y = 10

      y -= 2

      print(y)  # Output: 8

      z = 3

      z *= 2

      print(z)  # Output: 6

      Similarly, /=, %=, **= can be used as operators.

      OperatorDescriptionSyntax
      =Give the operand on the left side of the expression the value of the expression’s right side.x = y + z
      +=Add AND: After adding the right-side operand to the left-side operand, assign it to the left operand.a += ba = a+b
      -=Subtract AND: Assign to the left operand after subtracting the right operand from the left operand.a -=ba = a-b
      *=Multiply AND: Assign the result of multiplying the right operand by the left operand to the left operand.a*=b     a=a*b
      /=Divide AND: Assign to the left operand after dividing the right operand in half.a/=b     a=a/b
      %=Modulus AND: Assigns the result to the left operand after calculating modulus using the left and right operands.a%=ba=a%b
      //=Divide (floor) AND: Assign the value (floor) to the left operand after dividing the left operand by the right operand.a//=ba=a//b
      **=Exponent AND: Using operands, determine the value of the exponent (raise power) and assign it to the left operand.a**=ba=a**b
      &=Assigns a value to the left operand and performs bitwise AND on operands.a&=ba=a&b
      |=It assigns a value to the left operand after performing bitwise OR on operands.a|=ba=a|b
      ^=It assigns a value to the left operand and performs bitwise xOR on operands.a^=ba=a^b
      >>=It assigns value to the left operand and performs a bitwise right shift on operands.a>>=ba=a>>b
      <<=It executes operands that are bitwise left-shifted, and the left operand is assigned a value.a <<= ba= a << b
      Assignment Operators in Python

      Let’s look at an example of a Python assignment operator.

      For instance, the code begins with the values 10 for both “a” and “b.” After that, it operates on ‘b’ in the following ways: addition, subtraction, multiplication, and a left shift. Each operation’s results are presented, illustrating how these processes affect the value of “b.”

      a = 10

      b = a

      print(b)

      b += a

      print(b)

      b -= a

      print(b)

      b *= a

      print(b)

      b <<= a

      print(b)

      Output

      10

      20

      10

      100

      102400

      Suggested Blog: Top 10 Software Courses.

      Identity Operators

      Examples: is (returns ‘True’ if both variables point to the same object), is not (returns ‘True’ if both variables point to different objects).

      x = [1, 2, 3]

      y = [1, 2, 3]

      print(“x is y:”, x is y)

      print(“x is not y:”, x is not y)

      isIf the operands are identical, it returns true
      is notIf the operands are not identical, It returns ‘is not’
      Identity Operators in Python

      Let’s look at a Python example of an identity operator.

      Example: The code compares variables in Python using identity operators. It determines whether or not “a” and “b” are the same object (which they are not as their values differ) and whether or not “a” and “c” are the same object (which they are since “c” was given “a’s” value).

      a = 10 b = 20 c = a 

      print(a is not b) 

      print(a is c)

      Output

      True

      True

      Article Suggestion: Python Interview Questions and Answers.

      Membership Operators

      It is used to test if a sequence is present in an object. Examples: in (element is present in a sequence), not in (element is not in a sequence).

      my_list = [1, 2, 3, 4, 5]

      print(“Is 3 in my_list?”, 3 in my_list)

      print(“Is 6 not in my_list?”, 6 not in my_list)

      inIf the value is found in the sequence, it is true
      in notIf the value is not found in the list, it sends ‘is not’
      Examples of Membership Operators in Python

      The Python code that follows demonstrates how to use membership operators:

      As an illustration, the code verifies if the values “x” and “y” are present in the list. It indicates if every value is on the list or not. The printed messages indicate that ‘y’ is present and ‘x’ is not on the list. These tests are carried out by the code using the “in” and “not in” operators.

      x = 24

      y = 20

      list = [10, 20, 30, 40, 50]   

      if (x not in list): 

          print(“x is NOT present in given list”) 

      else: 

          print(“x is present in given list”) 

      if (y in list): 

          print(“y is present in the given list”) 

      else: 

          print(“y is NOT present in given list”) 

      Output

      x is NOT present in the given list

      y is present in the given list.

      Blog Recommendation: Building Web Applications with Django.

      Conclusion

      These are some of Python’s main types of operators, each serving a different purpose. Operators in Python are unique symbols, symbol combinations, or keywords that represent different calculation types. Combining operators and objects allows you to create expressions that carry out the calculation yourself. In other words, operators are the fundamental units of expressions that you can use to work with data. Learn more about operators in Python in our Python training in Chennai at SLA Institute and accelerate your career in a promising career.

      For Online & Offline Training

      Have Queries? Ask our Experts

      +91 88707 67784 Available 24x7 for your queries

      Quick Enquiry Form

        1
        Softlogic-Academy