Docsity

• Python file handling • Python iterators, Assignments of Artificial Intelligence

Partial preview of the text

Download • Python file handling • Python iterators and more Assignments Artificial Intelligence in PDF only on Docsity!

National University of Computer and Emerging

Sciences

Lab Manual 05 CL461-Artificial

Intelligence Lab

Course Instructor Dr Mubashir baig

Lab Instructor (s) Nimra

Saima Akhtar

Section H

Semester Spring 2022

Department of Computer Science

FAST-NU, Lahore, Pakistan

Objectives

After performing this lab, students shall be able to understand Python data structures which include:

  • Python file handling
  • Python iterators - Task Distribution Total Time 170 Minutes Python File Handling 20 Minutes Python Iterators 10 Minutes Exercise 90 Minutes - Python File Handling Python allows users to handle files by supporting to read and write files, along with many other file handling options. More details can be learnt HYPERLINK "https://towardsdatascience.com/knowing-these-you-can-cover-99-of-file-operations-in-python- 84725d82c2df"here HYPERLINK "https://towardsdatascience.com/knowing-these-you-can- cover-99-of-file-operations-in-python-84725d82c2df" - Open & Close a file When you want to read or write a file, the first thing to do is to open the file. Python has a builtin function open that opens the file and returns a file object. To return a file object we use open() function along with two arguments, that accepts file name and the mode, whether to read or write. The syntax is given below:

open(filename, mode)

- 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)

Python code to illustrate append()

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)

1.1 Even/Odd using Lambda function

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]

1.2 Reading text from a file and storing it in reversed order

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

1.3 Create an iterator that returns numbers, starting with 1, and each sequence

will increase by one (returning 1,2,3,4,5 etc.):