Page 266 - CTS - CSA TP - Volume 2
P. 266
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 127 : Document code segments using
comments and documentation strings
Objectives
At the end of this exercise you shall be able to
• develop python program to document code segments using comments and documentation strings.
Requirements
Tools/Materials
• PC/Laptop with Windows OS
• Latest Version of Python
Procedure
TASK 1: Single-line comment
Code:
# This is a single-line comment
variable = int(input(“Enter the Value: “)) # Inline comment for variable assignment
# The next line prints the variable
print(variable)
Explanation:
1 The first line is a single-line comment. Comments are ignored by the Python interpreter and are used to
provide explanations or notes within the code.
2 The second line prompts the user to enter a value. The input() function takes user input as a string, and int()
is used to convert it to an integer. The result is assigned to the variable.
3 The inline comment after the assignment explains that it is a comment related to the variable assignment.
4 The last line prints the value of the variable using the print() function.
Output:
TASK 2: Multi-line Comments
Code:
“””
This is a multi-line comment.
It can span multiple lines.
Use triple double-quotes or single-quotes.
“””
variable = input(“Enter a String: “)
print(variable)
251