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
      Set 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.

      Set Operators

      In the Set Theory of Mathematics, the union, intersection, difference and symmetric difference operations are defined. Python implements them with following operators.

      Union Operator (|)

      The union of two sets is a set containing all elements that are in A or in B or both. For example,
      {1,2}∪{2,3}={1,2,3}
      The following diagram illustrates the union of two sets.
      
      Python uses the "|" symbol as a union operator. The following example uses the "|" operator and returns the union of two sets.

      Example

      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1 | s2
      print ("Union of s1 and s2: ", s3)
      It will produce the following output
      Union of s1 and s2: {1, 2, 3, 4, 5, 6, 7, 8}

      Intersection Operator (&)

      The intersection of two sets AA and BB, denoted by A∩B, consists of all elements that are both in A and B. For example,
      {1,2}∩{2,3}={2}
      The following diagram illustrates intersection of two sets.
      
      Python uses the "&" symbol as an intersection operator. Following example uses & operator and returns intersection of two sets.
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1 & s2
      print ("Intersection of s1 and s2: ", s3)
      It will produce the following output
      Intersection of s1 and s2: {4, 5}

      Difference Operator (-)

      The difference (subtraction) is defined as follows. The set A−B consists of elements that are in A but not in B. For example,
      If A={1,2,3} and B={3,5}, then A−B={1,2}
      The following diagram illustrates difference of two sets
      
      Python uses the "-" symbol as a difference operator.

      Example

      The following example uses the "-" operator and returns difference of two sets.
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1 - s2
      print ("Difference of s1 - s2: ", s3)
      s3 = s2 - s1
      print ("Difference of s2 - s1: ", s3)
      It will produce the following output
      Difference of s1 - s2: {1, 2, 3}
      Difference of s2 - s1: {8, 6, 7}
      Note that "s1-s2" is not the same as "s2-s1".

      Symmetric Difference Operator

      The symmetric difference of A and B is denoted by "A Δ B" and is defined by
      A Δ B = (A − B) ⋃ (B − A)
      If A = {1, 2, 3, 4, 5, 6, 7, 8} and B = {1, 3, 5, 6, 7, 8, 9}, then A Δ B = {2, 4, 9}.
      The following diagram illustrates the symmetric difference between two sets
      
      Python uses the "^" symbol as a symbolic difference operator.

      Example

      The following example uses the "^" operator and returns symbolic difference of two sets.
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1 - s2
      print ("Difference of s1 - s2: ", s3)
      s3 = s2 - s1
      print ("Difference of s2 - s1: ", s3)
      s3 = s1 ^ s2
      print ("Symmetric Difference in s1 and s2: ", s3)
      It will produce the following output 
      Difference of s1 - s2: {1, 2, 3}
      Difference of s2 - s1: {8, 6, 7}
      Symmetric Difference in s1 and s2: {1, 2, 3, 6, 7, 8}

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.