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
      Enums

      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.

      Enums

      The term 'enumeration' refers to the process of assigning fixed constant values to a set of strings, so that each string can be identified by the value bound to it. Python's standard library offers the enum module. The Enum class included in enum module is used as the parent class to define enumeration of a set of identifiers − conventionally written in upper case.

      Example 1

      from enum import Enum
      
      class subjects(Enum):
      ENGLISH = 1
      MATHS = 2
      SCIENCE = 3
      SANSKRIT = 4
      In the above code, "subjects" is the enumeration. It has different enumeration members, e.g., subjects.MATHS. Each member is assigned a value.
      Each member is ab object of the enumeration class subjects, and has name and value attributes.
      obj = subjects.MATHS
      print (type(obj), obj.value)
      It results in following output
      <enum 'subjects'> 2

      Example 2

      Value bound to the enum member needn't always be an integer, it can be a string as well. See the following example
      from enum import Enum
      
      class subjects(Enum):
      ENGLISH = "E"
      MATHS = "M"
      GEOGRAPHY = "G"
      SANSKRIT = "S"
      obj = subjects.SANSKRIT
      print (type(obj), obj.name, obj.value)
      It will produce the following output
      <enum 'subjects'> SANSKRIT S

      Example 3

      You can iterate through the enum members in the order of their appearance in the definition, with the help of a for loop
      for sub in subjects:
      print (sub.name, sub.value)
      It will produce the following output
      ENGLISH E
      MATHS M
      GEOGRAPHY G
      SANSKRIT S
      The enum member can be accessed with the unique value assigned to it, or by its name attribute. Hence, subjects("E") as well as subjects["ENGLISH"] returns subjects.ENGLISH member.

      Example 4

      An enum class cannot have same member appearing twice, however, more than one members may be assigned same value. To ensure that each member has a unique value bound to it, use the @unique decorator.
      from enum import Enum, unique
      
      @unique
      class subjects(Enum):
      ENGLISH = 1
      MATHS = 2
      GEOGRAPHY = 3
      SANSKRIT = 2
      This will raise an exception as follows −
      @unique
      ^^^^^^
      raise ValueError('duplicate values found in %r: %s' %
      ValueError: duplicate values found in <enum 'subjects'>: SANSKRIT -> MATHS
      
      The Enum class is a callable class, hence you can use the following alternative method of defining enumeration
      from enum import Enum
      subjects = Enum("subjects", "ENGLISH MATHS SCIENCE SANSKRIT")
      The Enum constructor uses two arguments here. First one is the name of enumeration. Second argument is a string consisting of enumeration member symbolic names, separated by a whitespace.

      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.