Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Operators

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Operators

      What are Operators in Python?

      Python and other programming languages provide different type of operators which are symbols (sometimes called keywords) and used to perform a certain most commonly required operations on one or more operands. Operators allow different operations like addition, subtraction, multiplication, division etc.

      Types of Operators in Python

      Python language supports the following types of operators
      • Arithmetic Operators
      • Comparison (Relational) Operators
      • Assignment Operators
      • Logical Operators
      • Bitwise Operators
      • Membership Operators
      • Identity Operators
      Let us have a look at all the operators one by one.

      Python Arithmetic Operators

      Arithmetic operators are used to perform basic mathematical operations. Assume variable a holds 10 and variable b holds 20, then
      [ Python Arithmetic Operators ]
      Operator
      Name
      Example
      +
      Addition
      a + b = 30
      -
      Subtraction
      a – b = -10
      *
      Multiplication
      a * b = 200
      /
      Division
      b / a = 2
      %
      Modulus
      b % a = 0
      **
      Exponent
      a**b =10**20
      //
      Floor Division
      9//2 = 4

      Python Comparison Operators

      These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators.
      Assume variable a holds 10 and variable b holds 20, then
      [ Python Comparison Operators ]
      Operator
      Name
      Example
      ==
      Equal
      (a == b) is not true.
      !=
      Not equal
      (a != b) is true.
      >
      Greater than
      (a > b) is not true.
      <
      Less than
      (a < b) is true.
      >=
      Greater than or equal to
      (a >= b) is not true.
      <=
      Less than or equal to
      (a <= b) is true.

      Python Assignment Operators

      Assignment operators are used to assign values to variables. Following is a table which shows all Python assignment operators.
      [ Python Assignment Operators ]
      Operator
      Example
      Same As
      =
      a = 10
      a = 10
      +=
      a += 30
      a = a + 30
      -=
      a -= 15
      a = a - 15
      *=
      a *= 10
      a = a * 10
      /=
      a /= 5
      a = a / 5
      %=
      a %= 5
      a = a % 5
      **=
      a **= 4
      a = a ** 4
      //=
      a //= 5
      a = a // 5
      &=
      a &= 5
      a = a & 5
      |=
      a |= 5
      a = a | 5
      ^=
      a ^= 5
      a = a ^ 5
      >>=
      a >>= 5
      a = a >> 5
      <<=
      a <<= 5
      a = a << 5

      Python Bitwise Operators

      Bitwise operator works on bits and performs bit by bit operation. These operators are used to compare binary numbers.
      There are following Bitwise operators supported by Python language
      [ Python Bitwise Operators ]
      Operator
      Name
      Example
      &
      AND
      a & b
      |
      OR
      a | b
      ^
      XOR
      a ^ b
      ~
      NOT
      ~a
      <<
      Zero fill left shift
      a << 3
      >>
      Signed right shift
      a >> 3

      Python Logical Operators

      Python logical operators are used to combile two or more conditions and check the final result. There are following logical operators supported by Python language. Assume variable a holds 10 and variable b holds 20 then
      [ Python Logical Operators ]
      Operator
      Name
      Example
      and
      AND
      a and b
      or
      OR
      a or b
      not
      NOT
      not(a)

      Python Membership Operators

      Python's membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below −
      [ Python Membership Operators ]
      Operator
      Description
      Example
      in
      Returns True if it finds a variable in the specified sequence, false otherwise.
      a in b
      not in
      returns True if it does not finds a variable in the specified sequence and false otherwise.
      a not in b

      Python Identity Operators

      Identity operators compare the memory locations of two objects. There are two Identity operators explained below
      [ Python Identity Operators ]
      Operator
      Description
      Example
      is
      Returns True if both variables are the same object and false otherwise.
      a is b
      is not
      Returns True if both variables are not the same object and false otherwise.
      a is not b

      Python Operators Precedence

      The following table lists all operators from highest precedence to lowest.
      [ Show Example ]
      Sr.No.
      Operator & Description
      1
      **
      Exponentiation (raise to the power)
      2
      ~ + -
      Complement, unary plus and minus (method names for the last two are +@ and -@)
      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
      11
      is is not
      Identity operators
      12
      in not in
      Membership operators
      13
      not or and
      Logical operators