1.3-Comments, Identifiers and Keywords
1.3-Comments, Identifiers and Keywords
❖ The lines that begins with # symbol are considered as comments and ignored by the
python interpreter.
❖ For multiline comments three single quotes(‘’’) or three double quotes (“””) can be
used
Example 1
print("Sri Eshwar") #This is a comment
Example 2
#This is a comment
#written in
#more than just one line
print("Sri Eshwar")
Example 3
"""
This is a comment
written in
more than just one line
"""
print("Sri Eshwar")
Example 4
‘’’
This is a comment
written in
more than just one line
‘’’
print("Sri Eshwar")
Input and Output Functions
❖ A program needs to interact with the user to accomplish the desired task; this can be
achieved using Input-Output functions.
❖ The input () function helps to enter data at run time by the user and the output function
print () is used to display the result of the program on the screen after execution. ❖ In
python print () function is used to display the result on the screen.
The syntax for print () is as follows:
print(“String to be displayed as output”)
print(variable)
o Example
>>> print("Welcome to Python Programming")
Output
Welcome to Python Programming
Script
>>> x=5
>>> y=6
>>> z=x+y
>>> print(z)
Output
11
Script
>>> print("The sum=",z)
Output
The sum= 11
Script
>>> print("The sum of",x,"and",y,"is",z)
Output
The sum of 5 and 6 is 11
Output Formatting
o Sometimes we would like to format out output to make it look attractive.
o This can be done by using the str.format() method
Example Output
>>> x=10;y=20 The values of x is 10 and
>>> print('The values of x is {} and y is {}'.format(x,y)) y is 20
∙ Example
Note: Python accepts string as default type. Conversion is required for type.
The eval function takes a string and returns it in the type it is expected. The
following example illustrates this concept
Example
>>> X=eval('123')
>>> X
123
>>> type(X)
<class 'int'>
Indentation
❖ Python uses whitespace such as spaces and tabs to define program blocks whereas other languages
like C, C++, java use curly braces { } to indicate blocks of codes for class, functions or body of the
loops and block of selection command.
❖ The number of whitespaces (spaces and tabs) in the indentation is not fixed, but all statements
Tokens
❖ A token is the smallest unit of the program.
o Identifiers/Variables
o Keywords
o Operators
o Delimiters
o Literals
❖ Variables are noting but just parts of computer’s memory where information is stored.
Rules
o An identifier must start with an alphabet (A..Z or a..z) or underscore ( _ ).
o Identifiers may contain digits (0 .. 9), but cannot start with a digit.
o Python identifiers are case sensitive i.e. uppercase and lowercase letters are distinct
o Identifiers must not be a python keyword.
o Python does not allow punctuation character such as %,$, @ etc., within identifiers.
⮚ Example of valid identifiers
o Sum, total_marks, regno, num1
⮚ Example of invalid identifiers
o 12Name, name$, total-mark, continue
⮚ Assigning a value to a variable
o In Python, the equal sign (=) is used as the assignment operator
o Syntax : Variable=expression
Keywords
⮚ Keywords are the reserved words in Python.
⮚ Keywords are case sensitive.
⮚ We cannot use a keyword as variable name, function name or any other identifier.
and assert break class continue def del elif else Except