Single-Line Comments
We'll cover the following
Single-line comments
Single-line comments can be added by simply putting the hash symbol before the comment.
Let's take a look at an example that displays a message on the screen. This code uses comments to describe the functionality.
message = "Let's learn Python Comments today"# Displaying the message to the user on the consoleprint("Message for the user = " + message)
Multiple single-line comments
In Python, we can add as many comments as we want to describe the working of the code.
For instance, this piece of code calculates the amount of work to be done in a day work_per_day
using information such as total_work
, my_progress_so_far
, days_spent
, and all_days
. It consists of multiple single-line comments, and the format for each comment remains the same.
# Given values for the problemtotal_work = 100my_progress_so_far = 27days_spent = 20all_days = 60# Calculating remaining work and daysremaining_work = total_work - my_progress_so_fardays_remaining = all_days - days_spent# Calculating work per daywork_per_day = remaining_work / days_remaining# Printing the resultprint(f'You need to do approximately {work_per_day:.3f} units of work per day to complete the task.')
Note: The
.3f
in the{work_per_day:.3f}
statement allows us to specify the number of decimal places to print. In this case,work_per_day
is printed up to three decimal places.