UNIT-1 Lesson 2 Part 1 Variables, Expression and Statements
UNIT-1 Lesson 2 Part 1 Variables, Expression and Statements
A Python program is usually composed of multiple statements. Each statement is composed of one
or a combination of the following:
1. Comments
2. Whitespace characters
3. Tokens
In a computer program, a comment is used to mark a section of code as non-executable.
Comments are mainly used for two purposes:
a) To mark a section of source code as non-executable, so that the Python interpreter ignores it.
b) To provide remarks or an explanation on the working of the given section of code in plain
English, so that a fellow programmer can read the comments and understand the code.
Types of Comments with examples
In Python, there are two types of comments:
1. Single-line comment : It starts with # (also known as the hash or pound character) and the content following # till the end
of that line is a comment.
2. Docstring comment : Content enclosed between triple quotes, either ''' or """.
Below code example demonstrates the usage of comments:
The main purpose of docstrings in Python is to provide information on what a particular Python object does and not
how it does
According to the Python coding conventions, the docstring should always begin with a capital letter and end with
a period (.)
Example:
"""
This is a comment
written in
more than just one line.
"""
print("Hello, World!")
Note: docstrings inside modules, classes, functions, members, method definitions,
etc. will be done later
Identifier
An identifier is a name used to identify a variable, function, class, module, or object.
Identifier helps in differentiating one entity from the other. For example, name and age which speak of two different aspects are called identifiers.
Python is a case-sensitive programming language. Meaning, Age and age are two different identifiers in Python.
Rules:
1) 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 are valid Python identifiers.
2) An identifier can start with an alphabet or an underscore (_), but not with a digit.
Examples:
1_variable is invalid, but variable_1 is perfectly fine.
3) Keywords cannot be used as identifiers. (Keywords are reserved words in Python which have a special meaning).
Examples:
def, and, not, for, while, if, else etc.
4) Special symbols like !, @, #, $, % etc. are not allowed in identifiers. Only one special symbol underscore (_) is allowed.
Examples:
company#name, $name, email@id are invalid Python identifiers.
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 2 has 32 keywords while Python 3.5 has 33 keywords. An extra keyword called nonlocal was
added in Python 3.5. 2 more keywords were added in Python 3.7 making it to 35
Latest version is also having 35 keywords
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Python provides a way to print the list of keywords in its current version.
import keyword # This statement is used to import keyword module.
print(keyword.kwlist) # kwlist contains all the keywords of Python
More on keywords
To check whether a given word is a python keyword or not, we use a built-in function iskeyword().
This function returns a boolean value, if the given word is keyword then it returns True as output
otherwise returns False.
Let us consider a few examples:
Program - 1:
import keyword # We have to import keyword module
print(keyword.iskeyword('and')) # Here 'and' is a keyword so it prints True as output
Output: True
Program - 2:
import keyword # We have to import keyword module
print(keyword.iskeyword('python')) # Here 'python' is not a keyword so it prints False as output
Output: False
Which of the following options are correct?
1. Python version 3.5 has 33 keywords.
2. true is a valid keyword in Python.
3. The keyword nonlocal does not exist in Python 2.
4. Interpreter raises an error when you try to use keyword as a name of an entity.
5. A programmer can easily modify the keywords.