image
image
image

Comments in Pythons

image

When writing python programs and indeed any programming language, comments are very important. Comments are used to describe what is happening within a program. It becomes easier for another person taking a look at a program to have an idea of what the program does by reading the comments in it. Comments are also useful to a programmer as one can forget the critical details of a program written. The hash (#) symbol is used before writing a comment in Python. The comment extends up to the newline character. The python interpreter normally ignores comments. Comments are meant for programmers to understand the program better.

Example

i.  Start IDLE

ii.  Navigate to the File menu and click New Window

iii.  Type the following:

#This is my first comment

#The program will print Hello World

Print(‘Hello World’)  #This is an inbuilt function to display

iv.  On the file, menu click on Save. Type the name of myProgram5.py

Navigate to Run and click Run Module to run the program

Practice Exercise

This practice exercise integrates most of what we have covered so far.

a.  Write a program to sum two numbers 45, and 12 and include single line comments at each line of code.

b.  Write a program to show the names of two employees where the first employee is “Daisy” and the second employee is “Richard.” Include single comments at each line of code.

c.  Write a program to display the student registration numbers where the student names and their registration are: Yvonne=235, Ian=782, James=1235, Juliet=568.