0% found this document useful (0 votes)
56 views

Python IA 3 Assignment Answers v1

Here are the key differences between shutil.copy() and shutil.copytree() with examples: shutil.copy(): - Copies a single file from source to destination - Destination can be a file or directory Example: import shutil shutil.copy('file1.txt', 'backup') shutil.copytree(): - Copies an entire directory tree from source to destination - Destination must be a new directory - Handles subdirectories, files, symlinks etc. Example: import shutil shutil.copytree('documents', 'backup/documents') To rename files, shutil provides the move() function: import shutil shutil.move('old_file

Uploaded by

shezu0204
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Python IA 3 Assignment Answers v1

Here are the key differences between shutil.copy() and shutil.copytree() with examples: shutil.copy(): - Copies a single file from source to destination - Destination can be a file or directory Example: import shutil shutil.copy('file1.txt', 'backup') shutil.copytree(): - Copies an entire directory tree from source to destination - Destination must be a new directory - Handles subdirectories, files, symlinks etc. Example: import shutil shutil.copytree('documents', 'backup/documents') To rename files, shutil provides the move() function: import shutil shutil.move('old_file

Uploaded by

shezu0204
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1)List out the benefits of compressing file? Also explain reading of a zip file with example.

Benefits of compressing file:

i) Compressing a file reduces its size

ii) Which is useful when transferring it over the internet.

iii) since a ZIP file can also contain multiple files and subfolders, it’s a handy way to package several files into one.

iv) Hence keeps the files organised

v) Zip files can also be password protected for better security.

Reading ZIP Files:

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:

(IN EXAM JUST WRITE ASSERTION ERROR AND STATEMENT)

II) Program

Example Output:

// These 2 lines can be anything


// but should be equal

(IN EXAM JUST WRITE ASSERTION ERROR AND STATEMENT)

4)Write an assert statement that always triggers an assertion, Error.


Ans:

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:

ii) Moving files and folders:


Calling shutil.move(source, destination) will move the file or folder at the path source to the path destination and will
return a string of the absolute path of the new location. The destination path can also specify a filename which will
be used as the new name of the moved file

iii) Permantely deleting files and folders.


You can delete a single file or a single empty folder with functions in the os module, whereas to delete a folder and
all of its contents, you use the shutil module.

Example functions:
7)Briefly explain the printing objects with an example:

Printing objects:

Consider the following example:

To call this function, you have to pass a Time object as an argument:

To make print_time a method, all we have to do is move the function definition inside the class definition.

Now there are two ways to call print_time.

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,

Disabling logging entirely:


Logging can be disabled completely by using logging.disable(logging.CRITICAL) method.
Logging.disable() disables all log messages at that level and below since critical is highest level using critical as
argument stops all logging.
10)Illustrate the scenario with a suitable example i. if an existing file is opened in write mode ii. difference between
the read () and readlines () methods

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.

Existing file before writing

\\this can be anyline

Program

File after writing:

ii) read() vs readlines()

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:

Accessing members of class


Members of class can be accessed using dot operator. It is similar to how methods are accessed.
Example
16) Zip File objects have a close () method just like File objects’ close() method. demonstrate Zip File method is
equivalent to File objects’ open () method.

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: Zipfile equivalent:

17) Develop init method with a python program example.

The init method:


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:

The parameters are optional, so if you call Time with no arguments, you get the default values.

If you provide one argument, it overrides hour:

If you provide two arguments, they override hour and minute.

And if you provide three arguments, they override all three default values.
18) Explain _ _ init_ _() and _ _str_ _() methods with examples.

The init method:


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:

The parameters are optional, so if you call Time with no arguments, you get the default values.

If you provide one argument, it overrides hour:

If you provide two arguments, they override hour and minute.

And if you provide three arguments, they override all three default values.

The str method


__str__ is a special method, like __init__, that is supposed to return a string representation of an object

Example:

When you print an object, Python invokes the str method:

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:

Renaming using shutil:


We can use shutil.move(source, destination), and keep source as existing file name and destination to name that we
want to rename, with this we can rename the file using shutil module

Example:

This renames spam.txt to spam2.txt


20) Write a function named DivExp which takes TWO parameters a, b and returns a value c (c=a/b). Write
suitable assertion for a>0 in function DivExp and raise an exception for when b=0. Develop a suitable
program which reads two values from the console and calls a function DivExp.

Output:

21) Explain Operator overloading with example.


Changing the behavior of an operator so that it works with programmer-defined types is called operator overloading.
For every operator in Python there is a corresponding special method, like __add__.

Example:

Here even though example1 and 2 are objects, using “+” operator invokes __add__ function.

You might also like