0% found this document useful (0 votes)
2 views

2 Python Comments

The document explains the purpose and usage of comments in Python programming, highlighting that comments are ignored by the interpreter and can enhance code readability. It covers single-line and multi-line comments, providing examples, and emphasizes the importance of comments for debugging and collaboration. Additionally, it notes keyboard shortcuts for adding comments in text editors.

Uploaded by

adrianjudebl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2 Python Comments

The document explains the purpose and usage of comments in Python programming, highlighting that comments are ignored by the interpreter and can enhance code readability. It covers single-line and multi-line comments, providing examples, and emphasizes the importance of comments for debugging and collaboration. Additionally, it notes keyboard shortcuts for adding comments in text editors.

Uploaded by

adrianjudebl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

LEARNING

OBJECTIVES
• STUDENTS WILL BE ABLE TO LEARN ABOUT PYTHON COMMENTS
• LEARN HOW TO USE PYTHON COMMENTS
PYTHON COMMENTS

• Comments are hints that we add to our


code to make it easier to understand.
• When executing code, Python's interpreter
ignores comments.
• In Python, a comment starts with a #.
FOR EXAMPLE, WE HAVE A PROGRAM TO PRINT A TEXT ENTERED BY THE USER.

name = input("Enter your name:")


print(name)
• To make this program more readable, we can add comments like:

# Program to take the user's name

name = input('Enter your name')


print(name)
Here, the line starting with # is a comment. The Python compiler ignores everything after the
# symbol.
ANOTHER EXAMPLE,

# printing a number
print(25)
Output: 25
Here, # printing a number is a comment. This part is completely
ignored by the computer.
COMMENTS EXAMPLE

• Let's take a look at another example.

# Printing two numbers at once


print(6, 8)
# Output: 6 8
Here, the program contains two comments:

# Printing two numbers at once


# Output: 6 8
THE DIFFERENT TYPES
OF COMMENTS IN
PYTHON.
SINGLE-LINE COMMENT

We use the hash(#) symbol to


On the example, we have used three
write a single-line comment. single-line comments:
For example,
# declare a variable
# declare a variable # print name
# John
name = 'John'

# print name
print(name) # John
NOTE:
• Remember the keyboard shortcut to
apply comments. In most text editors,
it's Ctrl + / if you are on Windows
& Cmd + / if you are on a Mac.
MULTILINE COMMENTS

• Python doesn't have dedicated multi-line comment syntax like some other programming
languages like C++ and Java.

• However, we can achieve the same effect by using the hash (#) symbol at the beginning
of each line.

Let's look at an example.


# print(1)
# print(2)
# print(3)
WE CAN ALSO USE MULTILINE STRINGS AS
COMMENTS LIKE:
'''This program takes an input from the user and prints it'''

name = input('Enter your name: ')


print(name)

Oral Recitation:

What is the OUTPUT= ???


PREVENT EXECUTING CODE USING COMMENTS
• Comments are valuable when debugging code.
• If we encounter an error while running a program,
ACTIVITY 1:
instead of removing code segments, we can
comment them out to prevent execution.
OUTPUT:
For example,
number1 = 10 ???

number2 = 15

sum = number1 + number2

print('The sum is:', sum)


print('The product is:', product)
HERE, THE CODE THROWS AN ERROR BECAUSE WE HAVE NOT DEFINED A
PRODUCT VARIABLE.

We can comment out the code that's causing the error.


For example,
number1 = 10
number2 = 15

sum = number1 + number2

print('The sum is:', sum)


# print('The product is:', product)
SO, WHY USE COMMENTS IF COMPUTERS IGNORE THEM?

We should use comments for the following reasons:

• Comments make our code readable for future reference.


• Comments are used for debugging purposes.
• We can use comments for code collaboration as it helps
peer developers to understand our code.

You might also like