



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




After performing this lab, students shall be able to understand Python data structures which include:
- Kinds of modes There are three basic types of modes in which files can be opened in Python. mode meaning r open for reading (default) r+ open for both reading and writing (file pointer is at the beginning of the file) w open for writing (truncate the file if it exists) w+ open for both reading and writing (truncate the file if it exists)
mode file = open('book.txt','a') file.write("This will add this line") file.close()
- Python Iterators An iterator is an object that contains a countable number of values. It is an object that can be iterated upon, meaning that you can traverse through all the values. Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods iter() and next(). Every time you ask an iterator for the next item, it calls its __next__method. If there is another value available, the iterator returns it. If not, it raises a StopIteration exception. More information about iterators can be found here. This behavior (only returning the next element when asked to) has two main advantages: - Iterators need less space in memory. They remember the last value and a rule to get to the next value instead of memorizing every single element of a (potentially very long) sequence. - Iterators don’t check how long the sequence they produce might get. For instance, they don’t need to know how many lines a file has or how many files are in a folder to iterate through them. (One important note: don’t confuse iterators with iterables. Iterables are objects that can create iterators by using their iter method) - Building Custom Iterators iter( ) and the next( ) Building an iterator from scratch is easy in Python. We just have to implement the methods. iter( ) The method returns the iterator object itself. If required, some initialization can be performed. next( ) The method must return the next item in the sequence. Return an iterator from a tuple, and print each value:
mytuple = ("apple", "banana", "cherry") myit = iter(mytuple) print(next(myit)) print(next(myit)) print(next(myit)) To iterate the characters of a string: mystr = "banana" for x in mystr: print(x) The (^) for loop actually creates an iterator object and executes the next() method for each loop.
- Exercise (50 marks)
Write a Python program to filter a list of integers using Lambda. Original list of integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Even numbers from the said list: [2, 4, 6, 8, 10] Odd numbers from the said list: [1, 3, 5, 7, 9]
Design a code which reads text from the file “Alphabets.txt” and stores its data in reverse order in another file. For this you may upload the given text file on Google Collab’s session and define the path as: file_path= ‘/Alphabets.txt’ The same convention can be followed for defining path of the resultant file (reversed text file).