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

07 PGM

The document describes a program to add complex numbers. It defines a Complex class to represent complex numbers with real and imaginary parts. It also defines an add_complex_numbers function that takes a list of Complex objects and returns their sum. The program takes in N complex numbers from the user, stores them in a list, and calls add_complex_numbers to print the result of adding all the numbers together.

Uploaded by

ambika400
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

07 PGM

The document describes a program to add complex numbers. It defines a Complex class to represent complex numbers with real and imaginary parts. It also defines an add_complex_numbers function that takes a list of Complex objects and returns their sum. The program takes in N complex numbers from the user, stores them in a list, and calls add_complex_numbers to print the result of adding all the numbers together.

Uploaded by

ambika400
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

7)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.

import os
import sys
import pathlib
import zipfile

dirName = input("Enter Directory name that you want to backup : ")

if not os.path.isdir(dirName):
print("Directory", dirName, "doesn't exists")
sys.exit(0)

curDirectory = pathlib.Path(dirName)

with zipfile.ZipFile("myZip.zip", mode="w") as archive:


for file_path in curDirectory.rglob("*"):
archive.write(file_path, arcname=file_path.relative_to(curDirectory))

if os.path.isfile("myZip.zip"):
print("Archive", "myZip.zip", "created successfully")
else:
print("Error in creating zip archive")
8. 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.

Program:
def DivExp(a, b):
assert a > 0, "Error: 'a' should be positive."
if b == 0:
raise Exception("Error: Division by zero.")
c=a/b
return c

# Reading two values from the console


a = float(input("Enter value for 'a': "))
b = float(input("Enter value for 'b': "))

# Calling the function DivExp


try:
result = DivExp(a, b)
print("Result:", result)
except Exception as e:
print(e)

OUTPUT
1) Enter value for 'a': 3
Enter value for 'b': 2
Result: 1.5

2) Enter value for 'a': -2


Enter value for 'b': 4
Error: 'a' should be positive.
9. 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.

class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag

def __add__(self, other):


return Complex(self.real + other.real, self.imag + other.imag)

def __str__(self):
return '{} + {}i'.format(self.real, self.imag)

def add_complex_numbers(complex_numbers):
result = Complex(0, 0)
for number in complex_numbers:
result += number
return result

# read n from user


n = int(input("Enter the number of complex numbers: "))

# read n complex numbers


complex_numbers = []
for i in range(n):
real = float(input("Enter real part of complex number {}: ".format(i + 1)))
imag = float(input("Enter imaginary part of complex number {}: ".format(i + 1)))
complex_numbers.append(Complex(real, imag))
# compute the addition of n complex numbers
result = add_complex_numbers(complex_numbers)

print("Result of addition: ", result)

output:
Enter the number of complex numbers: 2
Enter real part of complex number 1: 4
Enter imaginary part of complex number 1: 2
Enter real part of complex number 2: 5
Enter imaginary part of complex number 2: 3
Result of addition: 9.0 + 5.0i

You might also like