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

Python Programs Solutions

The document provides solutions to various Python programming tasks, including adding list elements, calculating statistical measures, displaying charts, reading CSV files, and image processing. It also includes user input examples for swapping numbers and performing arithmetic operations. Each solution is accompanied by relevant code snippets demonstrating the implementation.

Uploaded by

up395302
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)
2 views3 pages

Python Programs Solutions

The document provides solutions to various Python programming tasks, including adding list elements, calculating statistical measures, displaying charts, reading CSV files, and image processing. It also includes user input examples for swapping numbers and performing arithmetic operations. Each solution is accompanied by relevant code snippets demonstrating the implementation.

Uploaded by

up395302
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/ 3

Python Programs Solutions

1. Add the elements of the two lists


list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = [a + b for a, b in zip(list1, list2)]
print("Result:", result)

2. Calculate mean, median and mode using Numpy


import numpy as np
from scipy import stats

data = [1, 2, 3, 4, 5, 5, 6]
mean = np.mean(data)
median = np.median(data)
mode = stats.mode(data)

print("Mean:", mean)
print("Median:", median)
print("Mode:", mode.mode[0])

3. Display line chart from (2,5) to (9,10)


import matplotlib.pyplot as plt

x = [2, 9]
y = [5, 10]
plt.plot(x, y)
plt.title("Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

4. Display scatter chart for given points


import matplotlib.pyplot as plt

x = [2, 9, 8, 5, 6]
y = [5, 10, 3, 7, 18]
plt.scatter(x, y)
plt.title("Scatter Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
5. Read CSV file and display 10 rows
import pandas as pd

df = pd.read_csv('yourfile.csv')
print(df.head(10))

6. Read CSV file and display its information


import pandas as pd

df = pd.read_csv('yourfile.csv')
print(df.info())

7. Read an image and display using Python


import cv2
from matplotlib import pyplot as plt

image = cv2.imread('yourimage.jpg')
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Image Display")
plt.axis('off')
plt.show()

8. Read an image and identify its shape using Python


import cv2

image = cv2.imread('yourimage.jpg')
print("Image shape:", image.shape)

9. Input two numbers and swap them


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Before swapping: a =", a, ", b =", b)
a, b = b, a
print("After swapping: a =", a, ", b =", b)

10. Read two numbers and arithmetic operator and display the computed result
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")

if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
if num2 != 0:
result = num1 / num2
else:
result = "Cannot divide by zero"
else:
result = "Invalid operator"

print("Result:", result)

You might also like