Press enter to skip the top menu

Python Intermediate

Comments and other matters

NCEA Compatibility

Module: as91883 Develop a computer program

Requirements: Documenting the program with comments

Go to top

Learning Outcomes

On completion of this section you will know

Go to top

Input, Process and Output

If we go through the code in Listing 1 below with the words of the above heading on our minds we may notice the following:

This three-part division is not peculiar to our programme alone or to the way it was designed. Just about all programmes fall into these three divisions.

The division where we get data from the user is known as the Input section because the programme accepts raw input data from the user. In our case, the user keyed in the raw data at the keyboard in response to prompts from the user. As we move further into programming we will see other ways of getting raw input data:

The division where the programme uses the raw data in order to produce further values is known as the Processing section of the programme. This occurs once all of the raw data has been received and is usually invisible to users. In our case the processing section uses the raw input data, i.e. the values for hours and rate to calculate values for the gross, tax and net pay. By the end of the processing part all of those values are stored in variables inside the computer’s memory and invisible to the users – hence the necessity of the next section.

The Output section takes the values calculated in the processing section and makes them visible to the users. In our case it simply prints the values of the gross, tax, superannuation contribution and net on to the console.

Again, as we progress further with our programming, other methods of output will become familiar to us:

Listing 1
                   
                        #C3Payroll Programme Decisions.py
                        strName=input("Enter employee's full name:  ")
                        floatHours=float(input("Enter value for hours:  "))
                        floatRate=float(input("Enter value for rate:  "))
                        floatGross=floatHours * floatRate
                        if floatGross<500:
                            floatTax=floatGross * 0.25
                        else:
                            floatTax=125+(floatGross - 500) * 0.33
                        floatNet=floatGross-floatTax
                        print("Gross is " + str(floatGross))
                        print("Tax is " + str(floatTax))
                        print("Net is " + str(floatNet))
                    
                
Go to top

Comments

Now that we know how to work out the different sections of our programme and where each section begins and ends, it would be useful to make them more easily discernable to a newcomer who is not familiar with the programme layout. The way to do this is through comments.

We have been using comments since we started this course although the comments have been very limited. The first line of all of our programmes started with the ‘#’ followed by the name of the file our programme was stored in.

If a line starts with the ‘#’ symbol, it is a signal to Python to ignore this line because it is not a programming command but an explanation or a piece of information for the person reading the programme.

In the earlier part of this text we analysed the programme in order to determine which groups of lines comprised the Input, Process and Output. Here we shall use comments in order to indicate the beginning and end of each of those sections. This means that it will be easier for a stranger reading the programme to determine where each section of the programme begins and ends.

Listing 2 below shows a commented version of the programme. Lines 3 and 7 are comments indicating the start and end of the Input section while line pairs 9, 16 and 18, 23 indicate the beginning and end of the Input, Process and Output sections.

Also notice that each comment line, apart from line 1, ends in a group of ‘*’ characters. This is in order to make the comment line more visible to a viewer. You can replace those ‘*’ characters with any other character or group of characters you wish, if you feel that it will make your comment more visible or you can leave them out. Remember that they will be ignored by the programme so it is entirely up to you what you put into them.

Listing 2
                    
                        #C3Payroll Programme Decisions.py
                        
                        #Start of INPUT Section ******************
                        strName=input("Enter employee's full name:  ") 
                        floatHours=float(input("Enter value for hours:  "))
                        floatRate=float(input("Enter value for rate:  "))
                        #End of INPUT Section ******************
                        
                        #Start of PROCESSING Section ******************
                        floatGross=floatHours * floatRate
                        if floatGross < 500:
                            floatTax=floatGross * 0.25
                        else:
                            floatTax=125+(floatGross - 500) * 0.33
                        floatNet=floatGross-floatTax
                        #End of PROCESSING Section ******************
                        
                        #Start of OUTPUT Section ******************
                        print("Gross is " + str(floatGross))
                        print("Tax is " + str(floatTax))
                        print("Net is " + str(floatNet))
                        #End of OUTPUT Section ******************
                        #End of Programme
                    
                    

Although we have used only one-line comments in our example here, a comment can take up as many lines as you wish. However each line of the comment must begin with the ‘#’ symbol.

Go to top

Indenting Code in Python

Among the major programming languages, Python is unique in that it uses indentation to delineate different levels within the programme, in the same way we use indentation to distinguish different levels within normal text.

Examining the code in Listing 2 above we notice that lines 3 to 10 are not indented but placed against the left margin. This indicates that they are top level commands, all of equal importance. For this reason the Python interpreter will execute each one of them in turn.

Line 12, however, is indented one tab space. This indicates that it is the body of the if construct at line 11. This line will only get executed of the condition at line 11 is True, i.e. the value stored in the variable floatGross is less than 500, otherwise it is skipped.

Line 13 is again not indented since since the else is the alternative to the if at line 11. This indicates that if the condition at line 11 is false, i.e. if the value of floatGross has a value of 500 or more then line 12 is skipped and control passes to line 13, which then passes control to line 14. Again line 14 is indented since it will only execute if the condition at line 11 is false.

The rest of the code, i.e. line 15 and lines 19 to 21 are not indented, indicating that they will always execute.

Go to top

Summary

Here we looked at three aspects of Python coding:

Go to top

Revision

Multi choice

Fill in the blanks

Go to top

Exercises

  1. What are the three divisions of most programmes?
  2. Explain what happens in each of those three divisions.
  3. What are comments used for?
  4. You cannot use numeric characters in comments. True or false?
  5. You cannot use tabs or indentation with comments. True or false?
  6. What two indications does Python give us that one line is subservient to another?
  7. How do you ensure that a group of lines are subservient to another line?
  8. It is a good idea to use the tab key in order to indent lines of code. True or false?

Go to top

Assignment

Modify the code you created for Assignment Part 3 so that the Input, Process and Output sections of it are commented in a fashion similar to Listing 2 above

Go to top