arrow
arrow
arrow
Consider the following Python code and answer the question given below: with open("NOTES.TXT", "r") as F: LINE = F.readlines() __________ # S
Question

Consider the following Python code and answer the question given below:
with open("NOTES.TXT", "r") as F:
LINE = F.readlines()
__________ # Statement 1
Which of the following Python statements will find and display the number of lines present in the file "NOTES.TXT"?

A.

print(len(LINE))

B.

print(LINE.count())

C.

print(LINE.count('\n'))

D.

print(LINE.len())

Correct option is A


The code uses the readlines() method to read all lines from the file "NOTES.TXT" and store them in the list LINE. To find the number of lines in the file, you can use the len() function, which returns the number of elements in the list LINE.
Since readlines() reads each line from the file as an element in the list (including the newline character \n at the end of each line), the length of the list will represent the number of lines in the file.
Thus, the correct statement to display the number of lines is:
print(len(LINE))
Important Key Points:
1. readlines(): This method reads the entire file, splitting it into lines and returning them as a list of strings.
2. len(): This function returns the number of items (lines in this case) in the list, giving the total number of lines in the file.
3. List Representation: Each element in the list corresponds to one line in the file, so the length of the list is the number of lines.
Knowledge Booster:
· Option (b): LINE.count() is incorrect because the count() method requires an argument (an element to count), and this is not how the number of lines is calculated.
· Option (c): LINE.count('\n') is incorrect because count('\n') would count the occurrences of newline characters within a single string or a list of strings. While it's related to line breaks, it's not the correct way to count the number of lines directly in the list.
· Option (d): LINE.len() is incorrect because there is no len() method associated with lists in Python. The correct function is the built-in len() function.

Free Tests

Free
Must Attempt

UPTET Paper 1: PYP Held on 23rd Jan 2022 (Shift 1)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Social Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Maths & Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
test-prime-package

Access ‘EMRS PGT’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
354k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow