python
python
o (i) join() method: The join() method in Python is used to concatenate elements of a
list or tuple into a single string, with a specified delimiter between each element.
For example:
python
result = ','.join(my_list)
o (ii) split() method: The split() method splits a string into a list where each word is a
list item. By default, it splits by whitespace, but you can specify a delimiter.
Example:
python
my_string = "apple,banana,cherry"
result = my_string.split(',')
2. Illustrate the benefits of compressing file? Also explain reading of a zip file with an
example.
o Benefits of compressing files: Compression reduces file size which saves storage
space and speeds up file transfer over networks. It also can make data more
manageable and easier to archive.
o Reading a zip file: You can use Python's zipfile module to read zip files. Here's an
example:
python
import zipfile
zip_ref.extractall('extracted_folder')
3. Explain the concept of File Handling. Also explain the reading & writing process with
suitable example.
o File Handling: File handling in Python involves opening, reading, writing, and
closing files.
o Reading & Writing Example:
python
# Writing to a file
file.write('Hello, World!')
content = file.read()
o Assertions: Assertions in Python are used to check if a condition is true, and if not,
it raises an AssertionError. They are useful for debugging:
python
return a / b
divide(10, 0)
5. With the code snippet, give the di erence between shutil.copy() and shutil.copytree()
method.
python
import shutil
shutil.copy('source.txt', 'destination.txt')
# Copying a directory
shutil.copytree('source_folder', 'destination_folder')
o (i) Operator Overloading: This allows you to define how Python operators work with
your own classes. Example:
python
class Vector:
self.x = x
self.y = y
v1 = Vector(2, 3)
v2 = Vector(4, 5)
v3 = v1 + v2
o (ii) Type based Dispatch: This involves di erent behavior based on the type of
object. Python does this automatically with polymorphism. Example:
python
return a + b
7. Write a function named DivExp which takes TWO parameters a, b and returns a value c
(=a/b). Write suitable assert 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.
python
if b > 0:
return a / b
try:
result = DivExp(a, b)
print(f"Result: {result}")
except AssertionError as e:
except ValueError as e:
except ZeroDivisionError:
8. Explain the methods init and str, with suitable example to each.
python
class Car:
self.make = make
self.model = model
python
class Car:
self.make = make
self.model = model
def __str__(self):
o Adding bullets in Wiki-Markup typically involves using asterisks *. Here's how you
might simulate this in Python:
python
def wiki_bullet(text):
lines = text.split('\n')
return '\n'.join(bulleted_lines)
print(wiki_bullet(text))
This function adds a bullet (*) to the beginning of each line in the given text.
10. With a suitable code snippet, explain Assertions and the contents of an assert
statement.
python
def check_positive(x):
try:
result = check_positive(-1)
except AssertionError as e:
11. Interpret for Opening of a file with open() functions, reading the contents of the file
with read() & writing to files with write().
python
file.write('This is a test.')
content = file.read()
file.write('\nThis is appended.')
12. Interpret on how you copy files & folders using shutil module.
13. Develop a program to Back Up a given Folder (Folder in a current working directory) into
a ZIP File by using relevant modules and suitable methods.
python
import os
import zipfile
zipf.write(file_path)
backup_to_zip('my_folder', 'backup.zip')
o i. Defining a class:
python
class Dog:
self.name = name
def bark(self):
python
my_dog = Dog('Rex')
python
15. Write a function named DivExp which takes TWO parameters a, b and returns a value c
(=a/b). Write suitable assert 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.
16. Describe Polymorphism functions to find histogram to count the number of times each
letter appears in a word or in sentence.
class Word:
self.text = text
def histogram(self):
count = {}
if char.isalpha():
count[char] = count.get(char, 0) + 1
return count
class Sentence(Word):
super().__init__(text)
word = Word("hello")