Question: Use programming concepts only from the chapter Decision structure, Repetition structure, Function, Files and Exceptions, Lists / Tuples and Strings. Q# 4 : ( 7

Use programming concepts only from the chapter Decision structure, Repetition structure, Function, Files and Exceptions, Lists/Tuples and Strings. Q#4: (7 points)Write a Python program that:
Uses a given filename, sample.txt, to read from.
Asks the user how many lines they want to read from this file.
Reads the specified number of lines from sample.txt (or all available lines if the file has fewer than the requested number).
Stores the lines in a list called lines_list
Performs the following operations on lines_list.
Create a list comprehension to store the length of each line in a list called line_lengths.Create a list comprehension to store only the lines that contain more than 20 characters in a list called long_lines.Calculate the average length of readed lines on lines_list and store it in a variable called average_length.
Prints the following:
The original lines_list.The line_lengths list.The long_lines list.The average_length of readed lines.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!