Comments & other Matters

Input, Process, Output Comments Indenting Code Summary Exercise Assignment Part 4

Learning Outcomes

On completion of this section you will know

·         Input, Process and Output: the universal divisions of all programmes

·         The need for comments in programming

·         The purpose of  indentation in Python

Download pdf version

Input, Process & Output

Go to top

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:

·         Lines 2 – 5 get information from the user – specifically values for employee’s name,  hours worked, hourly rate and superannuation code

·         Lines 6 – 25 use numeric values of the data received by the user in order to calculate values for various aspects of a payroll application – specifically the gross pay, tax, superannuation contribution and net pay

·         Lines 26 – 29 display on the console the values that were calculated in lines 6 – 25 above

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:

·         Reading the contents of text boxes in a GUI application

·         Opening and reading contents of a text file

·         Using a select query to read the contents of a database table

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, rate and superannuation code to calculate values for the gross, tax, superannuation payment 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:

·         write the values to a text file

·         display them in labels in a GUI application

·         use an insert or update query to store them in a database

·         post them somewhere on the internet

Listing 1

1

#C4Payroll Complex Decisions.py

2

strName=input("Enter employee's full name:  ")

3

floatHours=float(input("Enter value for hours:  "))

4

floatRate=float(input("Enter value for rate:  "))

5

intSuperCode=int(input("Enter value for Superannuation code:  "))

6

floatGross=floatHours * floatRate

7

if floatGross<500:

8

    floatTax=floatGross * 0.25

9

else:

10

    floatTax=125+(floatGross - 500) * 0.33

11

if intSuperCode == 0:

12

    floatSuperAmt = 0.0

13

elif intSuperCode == 1:

14

    floatSuperAmt = floatGross * 0.05

15

elif intSuperCode == 2:

16

    floatSuperAmt = floatGross * 0.1

17

elif intSuperCode == 3:

18

    floatSuperAmt = floatGross * 0.15

19

elif intSuperCode == 4:

20

    floatSuperAmt = floatGross * 0.2

21

else:

22

    floatSuperAmt = 0.0

23

    print("Faulty value for super code")

24

    print("Superannuation is calculated as zero")

25

floatNet=floatGross-floatTax-floatSuperAmt

26

print("Gross is " + str(floatGross))

27

print("Tax is " + str(floatTax))

28

print("Superannuation contribution is:  " + str(floatSuperAmt))

29

print("Net is " + str(floatNet))

 

Comments

Go to top

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 8 are comments indicating the start and end of the Input section while line pairs 10, 31 and 33, 39 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.

1

#C5Payroll Complex Decisions Commented.py

2

 

3

#Start of INPUT Section ******************

4

strName=input("Enter employee's full name:  ")

5

floatHours=float(input("Enter value for hours:  "))

6

floatRate=float(input("Enter value for rate:  "))

7

intSuperCode=int(input("Enter value for Superannuation code:  "))

8

#End of INPUT Section ******************

9

 

10

#Start of PROCESSING Section ******************

11

floatGross=floatHours * floatRate

12

if floatGross<500:

13

    floatTax=floatGross * 0.25

14

else:

15

    floatTax=125+(floatGross - 500) * 0.33

16

if intSuperCode==0:

17

    floatSuperAmt=0

18

elif intSuperCode==1:

19

    floatSuperAmt=floatGross*0.05

20

elif intSuperCode==2:

21

    floatSuperAmt=floatGross*0.1

22

elif intSuperCode==3:

23

    floatSuperAmt=floatGross*0.15

24

elif intSuperCode==4:

25

    floatSuperAmt=floatGross * 0.2

26

else:

27

    floatSuperAmt=0

28

    print("Faulty value for super code")

29

    print("Superannuation is calculated as zero")

30

floatNet=floatGross-floatTax-floatSuperAmt

31

#End of PROCESSING Section ******************

32

 

33

#Start of OUTPUT Section ******************

34

print("Gross is " + str(floatGross))

35

print("Tax is " + str(floatTax))

36

print("Superannuation contribution is:  " + str(floatSuperAmt))

37

print("Net is " + str(floatNet))

38

#End of OUTPUT Section ******************

39

#End of Programme

Listing 2

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.

Indenting Code in Python

Go to top

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 4 – 7 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..

Now, if we examine the code in lines 11 – 30, we see a very different arrangement. Let us start with the line pair 12 and 13.  Line 12 is flush against the left margin, which means that it is of equal importance to line 11. Line 13, however, is indented which indicates that it is subservient to line 12.  This subservience is indicated in two ways: firstly by the indentation which we have just mentioned and secondly by the colon at the end of line 12.  Python reads this colon as meaning indented lines are coming next.

What we have said above about the line pairs 12 and 13 also applies to the line pairs 14 and 15, 16 and 17 etc as far as the pair 24 and 25.  Lines 26 – 29 present a different scenario.  Line 26 is flush against the left margin and ends in a colon while lines 27 – 29 are indented.  This means that lines 27 – 29 are subservient to line 26 and they will be executed only if programme control reaches 26.

Line30 is not indented and is therefore independent on all of the lines above it.  For this reason it will always be executed.

Lines 34 – 37 are flush against the left margin, again indicating that they are top level commands who will always be executed.

Regarding indentation don’t be tempted to use the tab key to indent your code as some versions of Python use the tab for other purposes.  The safest way to indent is to use spaces.  Four spaces gives a good level of indentation but you can use more or less spaces as you wish. Whichever number of spaces you use when indenting your first line, you must the same number of spaces to indent all other lines.

One more word about indentation.  The discussion above only applies to lines of programme code. It does not apply to comment lines.  You can indent those using whatever number of tabs or spaces you wish. It will make no difference to the programme since those lines are ignored anyway.

 

Summary

Go to top

Here we looked at three aspects of Python coding:

·         the Input, Process and Output divisions of a programme

·         comment a programme

·         how Python uses indentation

Exercises

Go to top

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?

Assignment Part 4

Go to top

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