1. Write a python program to read the first two lines of a file: a.txt as provided in the following output.
The output is:
a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises.
Expected Answer:
Welcome to
www.freelearningpoints.com
Sample Answer:
def read_file(file_name, n_lines): from itertools import islice with open(file_name) as f: for line in islice(f, n_lines): print(line) read_file('a.txt', 2)
More Exercises:
Python String ExercisesMore Numpy Exercises:
Numpy String ExercisesMore Pandas Exercises:
Pandas Series ExercisesMore Tutorials:
Python Installation - Linux (Ubuntu)