Comments, Identifiers
and Keywords
Unit 1 Lesson 2
Understanding Python Comments
Comments
In a computer program, a comment is used to mark a section
of code as non-executable.
Comments can be used to explain Python code, so one can
understand the code better.
Comments can be used to prevent execution when testing
code.
Understanding Python Comments
In Python, there are two types of comments:
Single-line comments: These start with a # symbol, and
everything after it on the same line is treated as a
comment.
Docstring comment: These are enclosed in triple
quotes ((''') or (""")). We'll learn more about these later.
Understanding identifiers in python
Identifiers:
An identifier is a name used to identify a variable, function,
class, module, or object.
Identifier helps in differentiating one entity from the other.
Python is a case-sensitive programming language.
Meaning, Age and age are two different identifiers in
Python.
Understanding identifiers in python
Rules for writing identifiers:
Identifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an
underscore ( _ ).
Examples: myClass, var_1, print_this_to_screen, _number etc..
An identifier can start with an alphabet or an underscore (_), but not with a digit.
1_variable is invalid, but variable_1 is perfectly fine.
Keywords cannot be used as identifiers. (Keywords are reserved words in Python which have a special meaning,
Keywords in Python will be explained later.)
Examples: def, and, not, for, while, if, else and so on.
Special symbols like !, @, #, $, % etc. are not allowed in identifiers. Only one special symbol underscore (_) is
allowed.
company#name, $name, email@id are invalid Python identifiers.
Identifiers can be of any length.
Understanding identifiers in python
Naming conventions for Python identifiers :
• Class names start with an uppercase letter. All other identifiers
start with a lowercase letter.
• Starting an identifier with a single leading underscore (_)
indicates that the identifier is private.
• Starting an identifier with two leading underscores (__) indicates
that the identifier is strongly private.
• If an identifier begins with two leading underscores (__) and also
ends with two trailing underscores (__), then it indicates that the
identifier is a language-defined special name. For example,
Docstrings become __doc__ - a special attribute of an object.
Understanding python keywords
• Every programming language usually has a set of words known
as keywords.
• These are reserved words with special meaning and purpose.
They are used only for the intended purpose.
Note: We cannot use a keyword as a variable name, function
name, or as any other identifier name.
• Python also has its own set of reserved words called keywords.
• The interpreter uses the keywords to recognize the structure of the program.
Understanding python keywords
• Python 2 has 32 keywords while Python
3.5 has 33 keywords. An extra keyword
called nonlocal was added in Python 3.5.
• Note: Python 3.5 introduced async and await as context-
dependent keywords, and their usage as identifiers was
deprecated. In Python 3.7 async and await were added
as keywords, resulting in a total of 35 keywords in Python
3.7.