Python IA 3 Assignment Answers v1
Python IA 3 Assignment Answers v1
iii) since a ZIP file can also contain multiple files and subfolders, it’s a handy way to package several files into one.
To read the contents of a ZIP file, first you must create a ZipFile object. To create a ZipFile object, call the
zipfile.ZipFile() function, passing it a string of the .ZIP file’s filename. Using this object we can read the contents of
zipfile.
Example:
2) Outline a function named rect_in_circle that takes a Circle and a Rectangle and returns True if the Rectangle lies
entirely in or on the boundary of the circle.
Example Output
3) An assert statement that triggers an Assertion Error if the variable spam is an integer less than 10. Write an assert
statement that triggers an Assertion Error if the variables eggs and bacon contain strings that are the same as each
other, even if their cases are different (that is, 'hello' and 'hello' are considered the same, and 'goodbye' and
'Goodbye' are also considered the same)
ANS:
i)Program
Example output:
II) Program
Example Output:
Output
Explainantion:
The assertion error is raised based on Boolean value given after “assert” keyword. That is it raises a
assertion error if condition evaluates to False. By assigning False, we can make it always raise a assertion
error.
5) Develop a program to backing Up a given Folder (Folder in a current working directory) into a ZIP File by using
relevant modules and suitable methods.
Program:
Output:
6) Explain the following file operations in python with suitable example: i) Copying files and folders ii)
Moving files and folders iii) Permantely deleting files and folders.
i)Copying files and folders:
The shutil module provides functions for copying files, as well as entire folders. Calling shutil.copy(source,
destination) will copy the file at the path source to the folder at the path destination. ) If destination is a filename, it
will be used as the new name of the copied file. This function returns a string or Path object of the copied file.
Example:
Example functions:
7)Briefly explain the printing objects with an example:
Printing objects:
To make print_time a method, all we have to do is move the function definition inside the class definition.
i)
ii)
In this use of dot notation, print_time is the name of the method, and start is the object the method is invoked on,
which is called the subject.
By convention, the first parameter of a method is called self, so it would be more common to write print_time like
this:
The syntax for a function call, print_time(start) says something like “Hey print_time! Here’s an object for you to
print.”. While start.print_time() says “Hey start! Please print yourself.”
8) Illustrate pure function with an example using python programming.
Pure functions:
The functions which doesn’t modify the values or attributes of the passed objects or variables are called
pure functions, these functions also doesn’t take input, or display output, it only returns a value.
Example
The function creates a new Time object, initializes its attributes, and returns a reference to the new object. This is
called a pure function because it does not modify any of the objects passed to it as arguments and it has no effect,
like displaying a value or getting user input, other than returning a value.
9) What are the five logging levels? What line of code can you add to disable all logging messages in your program?
Logging levels provide a way to categorize your log messages by importance. There are five logging levels,
i) Write mode will overwrite the existing file and start from scratch, just like when you overwrite a variable’s value
with a new value.
Example.
Program
read() reads the entire text file and stores it in format of string. While readlines() reads all lines separately and stores
each line as an value in the list it returns.
Example:
Sample.txt file:
Program:
Output:
11)Develop a program to print 5 most frequently appearing words in a text file.
Program:
Example Output:
5
12) Define a function which takes TWO objects representing complex numbers and returns new complex number
with a addition of two complex numbers. Define a suitable class ‘Complex’ to represent the complex number.
Develop a program to read N (N >=2) complex numbers and to compute the addition of N complex numbers.
Output
13) Develop a program that uses class Student which prompts the user to enter marks in three subjects and
calculates total marks, percentage and displays the score card details.
Output:
14) Explain Polymorphism in python with example.
Polymorphism:
Functions that work with several types are called polymorphic. Polymorphism can facilitate code reuse. For
example, the built-in function sum, which adds the elements of a sequence, works as long as the elements of the
sequence support addition.
Example:
Histogram used to count numbers can also count no of occurrences of letter in word.
This function also works for lists, tuples, and even dictionaries, if the elements of “s” are hashable, so they can be
used as keys in d.
15)Illustrate with an example the creation, initiation of a class and accessing members of class
Creation of class:
A class can be created using keyword “class”, this class is a programmer defined data type
Example:
Initialization of class:
The init method (short for “initialization”) is a special method that gets invoked when an object is instantiated. Its full
name is __init__ (two underscore characters, followed by init, and then two more underscores).
Example program:
ZipFile objects are conceptually similar to the File objects we saw returned by the open() function they are values
through which the program interacts with the file. To create a ZipFile object, call the zipfile.ZipFile() function, passing
it a string of the .ZIP file’s filename.
Example:
The parameters are optional, so if you call Time with no arguments, you get the default values.
And if you provide three arguments, they override all three default values.
18) Explain _ _ init_ _() and _ _str_ _() methods with examples.
Example:
The parameters are optional, so if you call Time with no arguments, you get the default values.
And if you provide three arguments, they override all three default values.
Example:
This will become very helpful in debugging your code once its written.
19) Demonstrate with suitable code snippet the difference between shutil. copy() and shutil.copytree(), Also function
is used to rename files?
Shutil.copy()
Calling shutil.copy(source, destination) will copy the file at the path source to the folder at the path destination. If
destination is a filename, it will be used as the new name of the copied file.
Example:
Shutil.copytree()
Shutil.copytree() will copy an entire folder and every folder and file contained in it. Calling shutil .copytree(source,
destination) will copy the folder at the path source, along with all of its files and subfolders, to the folder at the path
destination.
Example:
Example:
Output:
Example:
Here even though example1 and 2 are objects, using “+” operator invokes __add__ function.