Identifiers =========== Identifiers are short pieces of text that are **used** as: * variables names * function names * function argument keywords * class names * module names Unlike string literals they are *not* enclosed in quotation marks. .. interactive_code_block:: :caption: Identifiers are like string literals pieces of text but can be easily distinguished from each other # A variable is an identifier that acts as a placeholder for a value a_variable_name = 'is not enclosed in quotation marks like string literals' print(a_variable_name) The variable name to the left of the equal sign ``=`` is not enclosed in quotation marks. To get a valid, working identifier a small set of **rules** has to be followed: * It must not be one of the `keywords `__ * It may contain * lowercase and uppercase *letters* ``a`` .. ``z`` and ``A`` .. ``Z`` * *digits* ``0`` .. ``9`` except as the first character * *underscores* ``_`` Actually Python 3 allows some `additional characters `__, so letters like ä, ö, ü, ß *could* also be used. .. interactive_code_block:: :caption: Valid identifiers (used as a variable names) # Identifiers with letters only are hard to read numberofdaysperweek = 7 # Identifiers with uppercase letters only even harder to read NUMBEROFMONTHSPERYEAR = 12 # Mixing upper- and lowercase letters (= camel case because of the 'humps') improves readablity a bit firstDayOfTheWeek = 'Monday' # Seprating words with underscores improves readablity much more (and uses only slightly more space) second_day_of_the_week = 'Tuesday' # Numbers can also been used (but not as first character!) python_3 = 'an easy to learn programming language' # Underscores can also be used as the first character _do_overscores_exist = False # Keywords can only be used as identifiers if the case is changed Finally = 'the sun is shining! :^)' print('If You read this, the code above contained no errors.') .. interactive_code_block:: :caption: Invalid identifiers # Identifiers must not start with a number 2nd_day_of_the_week = 'Tuesday' # Predefined names like keywords or built-in functions must not be used as identifier continue = 'don\\'t stop' # Special characters within identifiers are not allowed us$ = 'a currency' # Spaces within identifiers are also not allowed the final frontier = 'space' print('If You read this, the code above contained no errors.') Each of the variables in the statements above throw an error (and aborts running the program). |br| ► Try to correct all of them, one statment after the other, and watch the error messages.