Chapter 14 Applied Python Exercise 3

14.1 Goal – Print some specified number of lines from the beginning of an input file: random_snippet.vcf

Write Python code that works towards recreating the Bash tool head, displaying a specified number of lines from the beginning of a file.

14.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

14.3 Coding Blueprint

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

Previous chapter’s pseudocode:

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

In this chapter, we no longer want to print the line if and only if it’s the first line of the file. We now want to print the line if it’s any of the first several lines of the file, where the actual number of lines is specified or set or defined, much like how we define the input file name. Edit the pseudocode, adding one line, and editing another line to reflect this change in the goal.


ANSWER:

First, we need to SET the input file
Next, we need to SET the desired number of displayed lines
Then, FOR every line in the open file
   IF a desired line (by its numerical position)
     PRINT the line
   END IF
END FOR


14.4 Building the code

Create a new Python script

Much like how we added one line and edited one line of the previous chapter’s pseudocode, for the actual code, you will only need to add one line of code, and edit one line of code.

14.4.1 SET the input file

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


ANSWER:
filename = "random_snippet.vcf"

14.4.2 SET the desired number of lines

To SET the desired number of beginning lines to display, write a variable assignment expression, using some integer value as the desired number. Add this new line of code within your Python script.


ANSWER:
n_lines = 10

14.4.3 FOR every line in the open file

To loop through every line in the open file, use the for statement structure from the second Applied Python Exercise chapter. Write this line of code within your Python script.


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

14.4.4 IF a desired line (by its numerical position)

Next, edit the body of the for loop, specifically the conditional expression, such that it asks if the line is one of the desired beginning lines. Consult the notes on indexing in Python to see what numerical comparison operator (e.g., <=, <, ==) is necessary. Add this edited line of code within the for loop body within your Python script.


ANSWER:
  if i < n_lines:

14.5 Complete Intended Goal Code


ANSWER:
filename = "random_snippet.vcf" #SET the input filename
n_lines = 10 #SET the desired number of lines
for i, line in enumerate(open(filename)): #FOR every line in the open file
  if i < n_lines: #IF a desired line by its numerical position
    print(line.strip('\r\n')) #PRINT the line