0% found this document useful (0 votes)
2 views4 pages

7 th lab programm

The document outlines a Python script that creates an Excel workbook using openpyxl, populates it with student data (names, ages, fees), and performs operations like appending and deleting rows. It also includes a class definition for complex numbers, allowing for the addition of multiple complex numbers input by the user. The script demonstrates data manipulation and aggregation functions, such as calculating the sum, average, minimum, and maximum of fees.

Uploaded by

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

7 th lab programm

The document outlines a Python script that creates an Excel workbook using openpyxl, populates it with student data (names, ages, fees), and performs operations like appending and deleting rows. It also includes a class definition for complex numbers, allowing for the addition of multiple complex numbers input by the user. The script demonstrates data manipulation and aggregation functions, such as calculating the sum, average, minimum, and maximum of fees.

Uploaded by

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

import openpyxl

# Create a new workbook and get the active sheet

workbook = openpyxl.Workbook()

sheet = workbook.active

# Set the sheet title

sheet.title = 'ISE and Robotics'

# Set the headers

sheet['A1'] = "Name"

sheet['B1'] = "Age"

sheet['C1'] = "Fees"

# Data to be written

names = ["AKASH S", "AMOGH C", "AMRUTHA P", "B A SYED AFTAB", "CHANDANA L",

"CHANDU PRAKASH REDDY V S", "CHETHAN S", "DEEPTHI R", "G M THARUN", "GOWTHAM K
M"]

ages = [18, 20, 21, 23, 24, 19, 22, 18, 25, 26]

fees = [5000, 6000, 7000, 8000, 9000, 10000, 4000, 2000, 3000, 1000]

# Write data to the sheet

for i in range(len(names)):

sheet.cell(row=i + 2, column=1).value = names[i] # Write names

sheet.cell(row=i + 2, column=2).value = ages[i] # Write ages

sheet.cell(row=i + 2, column=3).value = fees[i] # Write fees

# Save the workbook

workbook.save("ISE_and_Robotics.xlsx")

import openpyxl

import pandas as pd

# Load the workbook and sheet

workbook = openpyxl.load_workbook('ISE_and_Robotics.xlsx')

sheet = workbook.active

# Collect data from the sheet into a DataFrame

data = []

for row in sheet.iter_rows(values_only=True):


data.append(row)

# Convert data to pandas DataFrame

df = pd.DataFrame(data[1:], columns=data[0]) # Use first row as column headers

# Display the first 5 rows

print(df.head(5))

________________________________________________________________________________

# Appending a new row

# New row data

new_row = ["NEW NAME", 27, 12000]

# Append the new row

sheet.append(new_row)

# Save the workbook

workbook.save('ISE_and_Robotics.xlsx')

print("New row added successfully!")

# Delete row

# Delete the 3rd row

sheet.delete_rows(3)

# Save the workbook

workbook.save('ISE_and_Robotics.xlsx')

print("Row 3 deleted successfully!")

# To perform aggregate functions

# Collect data from the 'Fees' column (assuming it's the 3rd column)

fees = []

for row in sheet.iter_rows(min_row=2, min_col=3, max_col=3, values_only=True):

fees.append(row[0])

# Aggregate Functions

total_sum = sum(fees)

average = sum(fees) / len(fees)


count = len(fees)

min_value = min(fees)

max_value = max(fees)

# Print Results

print(f"Sum of Fees: {total_sum}")

print(f"Average Fee: {average}")

print(f"Count of Fees: {count}")

print(f"Minimum Fee: {min_value}")

print(f"Maximum Fee: {max_value}")

9.Complex Class Demo 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. Objective:

• 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. By two objects.

class Complex:

def __init__(self, real=0, imag=0):

self.real = real

self.imag = imag

def read(self):

self.real = int(input("Enter real part: "))

self.imag = int(input("Enter imaginary part: "))

def show(self):

print(f"({self.real}) + i({self.imag})")

def add(self, other):

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

def main():

c1, c2 = Complex(3, 5), Complex(6, 4)


print("Complex Number 1: ", end=""), c1.show()

print("Complex Number 2: ", end=""), c2.show()

print("Sum of two Complex Numbers: ", end=""), c1.add(c2).show()

n = int(input("\nEnter the number of complex numbers (N >= 2): "))

comp_list = [Complex() for _ in range(n)]

for i, c in enumerate(comp_list, 1):

print(f"Enter details for Complex Number {i}:")

c.read()

print("\nEntered Complex Numbers:")

for c in comp_list:

c.show()

sum_obj = Complex()

for c in comp_list:

sum_obj = sum_obj.add(c)

print("\nSum of N Complex Numbers: ", end=""), sum_obj.show()

if __name__ == "__main__":

main()

You might also like