0% found this document useful (0 votes)
15 views8 pages

Add N Complex Numbers in Python

The document outlines three laboratory exercises focused on programming in Python. LAB 9 involves creating a program to add N complex numbers, while LAB 10 focuses on reading a CSV file and generating summary statistics for a selected column. Each section includes an aim, theory, algorithm, and sample code for implementation.

Uploaded by

dishitha0610
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)
15 views8 pages

Add N Complex Numbers in Python

The document outlines three laboratory exercises focused on programming in Python. LAB 9 involves creating a program to add N complex numbers, while LAB 10 focuses on reading a CSV file and generating summary statistics for a selected column. Each section includes an aim, theory, algorithm, and sample code for implementation.

Uploaded by

dishitha0610
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

LAB 9 — ADDITION OF N COMPLEX NUMBERS

Aim :

To develop a program to read N (N ≥ 2) complex numbers and compute the addition of N


complex numbers.

---

Theory

A complex number is of the form a + bj, where:

a → real part

b → imaginary part

Python provides built-in support for complex numbers using the complex() function.

---

Algorithm

1. Start

2. Read the value of N

3. Repeat N times:

Read real part

Read imaginary part

Convert into complex number

Add to total

4. Display the final sum

5. End

---

Code
# Program to add N complex numbers

N = int(input("Enter how many complex numbers: "))

nums = []

for i in range(N):

real = float(input(f"Enter real part of number {i+1}: "))

imag = float(input(f"Enter imaginary part of number {i+1}: "))

[Link](complex(real, imag))

total = sum(nums)

print("Sum of complex numbers:", total)

---

Sample Output

Enter how many complex numbers: 3

Enter real part of number 1: 2

Enter imaginary part of number 1: 3

Enter real part of number 2: 1

Enter imaginary part of number 2: 4

Enter real part of number 3: 3

Enter imaginary part of number 3: 2

Sum of complex numbers: (6+9j)


---

=============================

TEXT ANALYSIS TOOL (WORD FREQUENCY, LONGEST WORD, SENTENCE COUNT)

=============================

Aim

To create a tool that analyses a given paragraph and displays:

Word frequency

Longest word

Number of sentences

---

Theory

Text analysis uses Python libraries such as:

re for pattern matching

Counter for counting word occurrences

---

Code
import re

from collections import Counter

text = input("Enter a paragraph: ")

# Convert to lowercase and extract words

words = [Link](r"\w+", [Link]())

freq = Counter(words)

# Find longest word

longest = max(words, key=len)

# Count sentences

sentences = [Link](r"[.!?]+", text)

sentence_count = len([s for s in sentences if [Link]()])

print("Word Frequency:", freq)

print("Longest Word:", longest)

print("Number of Sentences:", sentence_count)

---

=============================

LAB 10 — DATA SUMMARY GENERATOR USING CSV


=============================

Aim

To read a CSV file, store it in dictionary format, and allow summary queries like max, min,
average for a selected column.

---

Theory

CSV (Comma Separated Values) is used for storing tabular data.

[Link]() helps read each row as a dictionary.

We can then perform numerical computations on individual columns.

---

Algorithm

1. Start

2. Ask user for CSV filename

3. Read data using [Link]()

4. Ask user for column name to analyze

5. Extract numeric values from that column

6. Compute max, min, and average

7. Display summary

8. End

---

Code
import csv

def read_csv_to_dict(filename):

with open(filename, 'r') as file:

reader = [Link](file)

return list(reader)

def summarize(data, column):

values = [float(row[column]) for row in data if row[column]]

return {

'max': max(values),

'min': min(values),

'average': sum(values) / len(values)

filename = input("Enter CSV filename: ")

data = read_csv_to_dict(filename)

col = input("Enter column to summarize: ")

summary = summarize(data, col)

print("Summary:", summary)

---
How to Create a CSV File

Sample CSV File Content (Use this for testing)

Save it as [Link]

Day,Temperature,Humidity

1,32,70

2,30,65

3,35,72

4,33,68

5,31,71

Or for COVID dataset ([Link]):

Day,Cases,Recoveries,Deaths

1,120,80,2

2,140,100,1

3,160,110,3

4,155,130,2

5,170,140,4

---

Expected Output Example


Enter CSV filename: [Link]

Enter column to summarize: Temperature

Summary: {'max': 35.0, 'min': 30.0, 'average': 32.2}

You might also like