Chapter 13 Applied Python Exercise 2

13.1 Goal – Print the first line in a file

Write Python code that works towards recreating the Bash tool head, displaying only the first line in a file: random_snippet.vcf

13.2 Learning Objectives

After going through this chapter, students should be able to:

  • State the sub-steps needed to meet the coding goal
  • Use the following datatypes, structures, and fundamentals to meet the coding goal:
    • variable assignment
    • strings
    • opening a file
    • for loop
    • integers
    • logical expression
    • conditional statement
    • printing output

13.3 Coding Blueprint

Let’s start with and edit the pseudocode from the previous chapter to meet the needs of this chapter

Note: you may write/edit pseudocode anywhere you would like. You will not have to turn in any pseudocode for any chapter. Just remember that since pseudocode doesn’t follow the syntax of any one programming language, if you choose to write it in the online python text editor interface or your local python script, you’ll want to make sure when you run the script that the pseudocode is commented out (with a # in front of it) so that Python does not try to treat it as real code which will cause errors.

Previous chapter’s pseudocode:

First, we need to SET the input file
Second, FOR every line in the open file
   PRINT the line
END FOR

In this chapter, we want to only print the line if it’s the first line of the file. Keeping the for loop, edit the pseudocode, inserting a step that asks which line position it is numerically in the file.


ANSWER:

First, we need to SET the input file
Second, FOR every line in the open file
   IF the first line
     PRINT the line
   END IF
END FOR


13.3.1 SET the input file

Much like how we reused the pseudocode of the previous chapter, we’ll be able to reuse much of the code from the previous chapter. Do you think that defining a variable with the input file name is one of those reusable steps?


ANSWER:

Yes, it is


13.3.2 FOR every line in the open file

Let’s again focus on building the for loop structure by considering the 2 components of the loop used in the first Applied Python Exercise chapter.

  1. The for statement

Do you think that the for statement from the previous chapter. is the best pattern for this problem? Consider that the for statement in the previous chapter only provided you access to each line (or item) in the file (or collection of items), but did not provide you any information on which line position numerically it was in the file.


ANSWER:

We should use the third for statement pattern instead which provides both the item and the item’s position or index.


13.3.3 IF the first line and PRINT the line

  1. The body of the for loop

Considering that the body of the for loop is where the pseudocode was added, we also need to add to the body of the for loop. However, does the code that we already have from the first Applied Python Exercise chapter still work well for this chapter?


ANSWER:

Yes, it does.


What can we use to see if the index or position of the line is the first line in the file?


ANSWER:

We can use a conditional and logical expression such that we’ll PRINT the line, but only if it’s the first line in the file.


13.4 Building the code

Create a new Python script

13.4.1 SET the input file

To SET what the input file is, we will use the variable assignment code from the first Python Applied Exercise chapter. Within your Python script, write that variable assignment expression.


ANSWER:
filename = "random_snippet.vcf"

13.4.2 FOR every line in the open file

Consult the notes on the third pattern of the for statement and edit the first Applied Python Exercise chapter’s for statement code to meet the needs of this chapter. Within your python script, write the revised for statement.


ANSWER:
for i, line in enumerate(open(filename)):

13.4.3 IF the first line

Next, edit the body of the for loop from the first Applied Python Exercise chapter such that it asks if the line is the first line. Consult the notes on indexing to see what integer you want the index to be equal to (e.g., 0 or 1). Write the conditional and logical expression code within your Python script within the for loop body.


ANSWER:
  if i == 0:

13.5 Complete Intended Goal Code


ANSWER:
filename = "random_snippet.vcf" #SET the input filename
for i, line in enumerate(open(filename)): #FOR every line in the open file
  if i == 0: #IF the first line
    print(line.strip('\r\n')) #PRINT the line