Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

4/25/24, 10:28 PM Notebook.

ipynb - Colab

keyboard_arrow_down Comments
Comments in Python are non-executable lines of text that provide explanations or annotations within your code. They are used to enhance code
readability, provide context, and make your code easier to understand for yourself and others who may read or maintain your code.

Motivation

Welcome to your journey of learning about comments in programming! Let's explore the key reasons why learning about comments is valuable:

Code Documentation: Comments are like notes that you can write in your code to help explain what it does. They make it easier for others
to understand your code and for you to remember what you did when you look at it later.

Code Readability and Organization: Comments make your code easier to read and follow. They act like signposts that guide you through
your code and help you understand how it works.

Debugging and Troubleshooting: Comments can help you find and fix problems in your code. You can use them to mark or explain parts
of the code that are causing issues, and it makes it easier for you or others to figure out what went wrong.

keyboard_arrow_down Single-line comments


Single-line comments begin with the # symbol and extend until the end of the line. They are used to comment on a specific line or provide short
explanations. Anything after the # symbol on the same line is considered a comment and is not executed. For example:

# This is a single-line comment


x = 5 # Assigning a value to x

In the above example, the first line is a single-line comment that provides a general comment. The second line includes a comment at the end
to explain the purpose of the code.

How to use comments?

Comments are essential for improving code understanding, facilitating collaboration, and ensuring code maintainability. Here are some tips for
using comments effectively:

Provide context: Use comments to explain the purpose, intent, or rationale behind the code. Describe the why, not just the how.
Clarify complex logic: Use comments to explain intricate or non-obvious logic, making it easier for others (and yourself) to follow the
code's flow
Update comments during code maintenance: Keep comments up to date when modifying code. If you change the functionality, update
the comments accordingly to reflect the new behavior.
Avoid redundant comments: Avoid commenting obvious or self-explanatory code. Focus on commenting where clarity is genuinely
needed.
Maintain consistency: Follow a consistent commenting style throughout your codebase to enhance readability and coherence

keyboard_arrow_down Comments for Variables


Comments can be particularly useful when documenting variables in your code. They provide additional information about the purpose, usage,
or characteristics of variables, making it easier for you and others to understand the code. Here are some ways to effectively use comments for
variables:

Variable declarations
Add comments next to variable declarations to explain their purpose or initial values. This helps in understanding the role of variables in the
code. For example:

# Number of students in the class


student_count = 25

# User's first name


first_name = "John"

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/0. Comments/Noteb… 1/2


4/25/24, 10:28 PM Notebook.ipynb - Colab

In the above example, comments are used to provide information about the variables student_count and first_name , clarifying their
significance.

keyboard_arrow_down Special variable conditions


If a variable has special conditions or restrictions, use comments to highlight them. This helps ensure that the variable is used correctly. For
example:

# Discount percentage (should be between 0 and 100)


discount_percentage = 20

# Maximum number of attempts for login (limited to 3)


max_login_attempts = 3

In this case, comments are used to indicate the range or limitations of the variables discount_percentage and max_login_attempts .

keyboard_arrow_down Variable modifications


If a variable's value is modified in a particular context or under specific conditions, explain it with comments. This provides clarity on why the
modification is being made. For example:

# Increment the counter by 1 for each iteration


counter += 1

# Update the total score by adding the current score


total_score += current_score

Here, comments are used to describe the purpose of modifying the variables counter and total_score . Don't worry about not being familiar
with arithmetic operations yet, we will cover them in detail in a latter lesson.

Remember to keep your comments concise, clear, and focused on providing valuable information about the variables. Use comments as a tool
to enhance the understanding of your code and make it easier for yourself and others to work with the variables effectively.

Key takeaways

Comments are non-executable lines of text that provide explanations or annotations within your code. They improve code understanding
and maintainability.
Use comments to document variables, providing information about their purpose, restrictions, modifications, or usage. Clear and concise
comments make your code more understandable for yourself and others.

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/0. Comments/Noteb… 2/2

You might also like