Python Questions Answers
Python Questions Answers
import os
import zipfile
def backup_to_zip(folder):
folder = os.path.abspath(folder)
number = 1
while True:
if not os.path.exists(zip_filename):
break
number += 1
print(f'Creating {zip_filename}...')
backup_zip.write(foldername)
backup_zip.write(os.path.join(foldername, filename))
print('Backup complete.')
# Example usage:
# backup_to_zip('my_folder')
2. Interface in Python
In Python, an interface is a way to define a contract for what a class should do, without specifying
how it should do it. This is often achieved using abstract base classes (ABCs) from the `abc`
module.
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Circle(Shape):
self.radius = radius
def area(self):
def perimeter(self):
# circle = Circle(5)
# print(circle.area())
# print(circle.perimeter())
3. Explanation of Commands
import shutil
shutil.move('source.txt', 'destination_folder/')
import os
os.unlink('file_to_remove.txt')
import os
os.rmdir('empty_folder')
import shutil
shutil.rmtree('folder_to_remove')
- **extractall()**: Extracts all contents of a ZIP file.
import zipfile
zip_ref.extractall('extracted_folder')
4. Detailed Explanation
- **Debugger**: A tool that allows developers to inspect and control the execution of a program. It
helps in finding and fixing bugs by providing functionalities like breakpoints, step execution, and
variable inspection.
- **Functionality of Debug Control Window**: This window in a debugger allows users to control the
flow of program execution. It typically provides options to start/stop execution, step through code
- **Exceptions**: Errors that occur during the execution of a program. They can be handled using
- **Assertions**: Statements used during debugging to check if a condition is true. If the condition is
5. Code Snippets
# Copy a file
shutil.copy('source.txt', 'destination.txt')
# Copy a directory
shutil.copytree('source_folder', 'destination_folder')
import os
print(f'\tFile: {file_name}')
import zipfile
zipf.write('file_to_add.txt')
self.real = real
self.imag = imag
def __str__(self):
# Example usage:
for _ in range(n)]
- **Classes**: Blueprints for creating objects. They encapsulate data and functions.
class Dog:
self.age = age
def bark(self):
# Example usage:
dog1 = Dog('Buddy', 5)
dog1.bark()
8. Operator Overloading
class Vector:
self.x = x
self.y = y
def __str__(self):
# Example usage:
v1 = Vector(1, 2)
v2 = Vector(3, 4)
v3 = v1 + v2
class Student:
self.name = name
self.marks = []
self.marks.append(mark)
def total_marks(self):
return sum(self.marks)
def percentage(self):
def display(self):
print(f'Percentage: {self.percentage()}%')
# Example usage:
student = Student('Alice')
student.add_marks(90)
student.add_marks(85)
student.add_marks(88)
student.display()
The __init__ method is a constructor in Python classes. It initializes the object's attributes.
class Car:
self.brand = brand
self.model = model
def display_info(self):
# Example usage:
car.display_info()
Python does not have formal interfaces like some other languages, but abstract base classes
(ABCs) can be used to define a set of methods that must be created within any child classes built
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
return "Woof!"
# Example usage:
dog = Dog()
print(dog.sound())