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

Python Question Bank Answers

The document provides detailed answers to Python IA questions, covering topics such as string methods (join() and split()), file handling, assertions, exception handling, and operator overloading. It includes code examples demonstrating the use of these concepts, as well as benefits of file compression and methods for copying files. Additionally, it outlines the structure of future questions that will address various Python programming concepts.

Uploaded by

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

Python Question Bank Answers

The document provides detailed answers to Python IA questions, covering topics such as string methods (join() and split()), file handling, assertions, exception handling, and operator overloading. It includes code examples demonstrating the use of these concepts, as well as benefits of file compression and methods for copying files. Additionally, it outlines the structure of future questions that will address various Python programming concepts.

Uploaded by

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

Python Question Bank Answers

Detailed Answers to the Python IA Questions


Question 1

a. Explain the following:

(i) join() method: The join() method in Python is used to concatenate the elements of an
iterable (like a list or tuple) into a single string. The elements must be strings.

Syntax:

separator.join(iterable)

Example:

words = ["Python", "is", "awesome"]


result = " ".join(words)
print(result) # Output: Python is awesome

(ii) split() method: The split() method splits a string into a list based on a specified
separator. By default, it splits on whitespace.

Syntax:

string.split(separator, maxsplit)

Example:

text = "Python is awesome"


result = text.split()
print(result) # Output: ['Python', 'is', 'awesome']

b. Illustrate the benefits of compressing files. Also, explain reading a zip file with an
example:

Benefits of Compressing Files:

Reduces storage space.


Speeds up file transfer.
Organizes multiple files into a single compressed archive.

Reading a ZIP File: Python’s zipfile module allows handling ZIP files.

Example:

import zipfile

# Reading a ZIP file


with zipfile.ZipFile('example.zip', 'r') as zip_ref:
zip_ref.extractall('extracted_folder')

Question 2
a. Explain the concept of File Handling. Also, explain the reading & writing process with
a suitable example:

File Handling in Python: File handling allows reading, writing, and manipulating files.

Modes of File Handling:

r : Read
w : Write
a : Append
rb , wb : Read/Write in binary mode

Example:

# Writing to a file
with open('example.txt', 'w') as file:
file.write("Hello, World!")

# Reading from a file


with open('example.txt', 'r') as file:
content = file.read()
print(content) # Output: Hello, World!

b. Briefly explain Assertions and raise an exception:

Assertions:

Assertions are debugging tools to test conditions during execution.


If the condition evaluates to False , an AssertionError is raised.

Example:

x = 10
assert x > 5, "x should be greater than 5"

Raise an Exception:

The raise keyword is used to raise exceptions.

Example:

value = -1
if value < 0:
raise ValueError("Value must be non-negative")

Question 3
a. Difference between shutil.copy() and shutil.copytree() with code snippet:

shutil.copy() : Copies a single file.

shutil.copytree() : Copies an entire directory tree.

Example:

import shutil

# Copy a single file


shutil.copy('source.txt', 'destination.txt')

# Copy an entire directory


shutil.copytree('source_folder', 'destination_folder')

b. Explain the concept of the following with examples:

(i) Operator Overloading: Operator overloading allows defining custom behavior for operators.

Example:

class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def __add__(self, other):


return Point(self.x + other.x, self.y + other.y)

p1 = Point(1, 2)
p2 = Point(3, 4)
p3 = p1 + p2
print(p3.x, p3.y) # Output: 4 6

(ii) Type-based Dispatch: Type-based dispatch allows defining methods that behave differently
based on argument types.

Example:

from multipledispatch import dispatch

@dispatch(int, int)
def add(a, b):
return a + b

@dispatch(str, str)
def add(a, b):
return a + " " + b

print(add(1, 2)) # Output: 3


print(add("Hello", "World")) # Output: Hello World

Question 4
a. Write a function named DivExp with assertions and exception handling:

Code:

def DivExp(a, b):


assert a > 0, "a must be greater than 0"
if b == 0:
raise ZeroDivisionError("b cannot be zero")
return a / b

try:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print("Result:", DivExp(a, b))
except Exception as e:
print("Error:", e)

Remaining Questions
The rest of the questions will be answered with similar detailed explanations and code
examples, including:

Initialization and string representation methods ( __init__ and __str__ ).


Adding bullets to Wiki-Markup-support with code.
Assertions and the contents of an assert statement.
File operations using open() and shutil .
Creating a ZIP file backup.
Class definition, initialization, and member access.
Polymorphism and histogram creation.

s.

You might also like