_bca 3rd python lab (1)
_bca 3rd python lab (1)
e =[1, 2, 3, 4]
print("Type of e:", type(e))
print("Value of e:", e)
#Program 2:
#create list and perform the following methods
#1.insert() 2.remove() 3.append 4,len() 5.pop() 6.clear()
#create a list
my_list =[1, 2, 3, 4, 5]
print("Original List: ", my_list)
#1. Add items 2. len() 3. check for item in tuple 4. Access items
# Create a tuple
my_tuple = (1, 2, 3, 4, 5)
print("Original Tuple: ", my_tuple)
# Create a dictionary
student = {"name": "Mary", "age": 20, "grade": "A"}
# Menu-driven program
while True:
# Display the menu
print("\nARITHMETIC OPERATIONS")
print("1. TO PERFORM ADDITION")
print("2. TO PERFORM SUBTRACTION")
print("3. TO PERFORM MULTIPLICATION")
print("4. TO PERFORM DIVISION")
print("5. EXIT")
choice = int(input("Enter your choice: "))
if choice==5:
print("existing the program")
break
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
ARITHMETIC OPERATIONS
1. TO PERFORM ADDITION
2. TO PERFORM SUBTRACTION
3. TO PERFORM MULTIPLICATION
4. TO PERFORM DIVISION
5. EXIT
Enter your choice: 2
Enter first number: 12
Enter second number: 13
Result: -1.0
ARITHMETIC OPERATIONS
1. TO PERFORM ADDITION
2. TO PERFORM SUBTRACTION
3. TO PERFORM MULTIPLICATION
4. TO PERFORM DIVISION
5. EXIT
Enter your choice: 3
Enter first number: 12
Enter second number: 14
Result: 168.0
ARITHMETIC OPERATIONS
1. TO PERFORM ADDITION
2. TO PERFORM SUBTRACTION
3. TO PERFORM MULTIPLICATION
4. TO PERFORM DIVISION
5. EXIT
Enter your choice: 4
Enter first number: 12
Enter second number: 13
Result: 0.9230769230769231
ARITHMETIC OPERATIONS
1. TO PERFORM ADDITION
2. TO PERFORM SUBTRACTION
3. TO PERFORM MULTIPLICATION
4. TO PERFORM DIVISION
5. EXIT
Enter your choice: 5
existing the program
#Input a number
Enter a number: -7
-7 is a negative number
def is_even(num):
return num % 2 == 0
#Use filter() to filter only even numbers
even_numbers = list(filter(is_even, numbers))
#Program 8:Write a program to print date, time for today and now
import datetime
import datetime
today =datetime.datetime.now().date()
def count_characters(string):
# Create an empty dictionary
char_count = {}
def count_characters(filename):
# Open the file in read mode
with open(filename, "r") as file:
# Read the contents of the file into a string
contents = file.read()
import numpy as np
#rogram 14 :Write a python code to read a csv file using pandas module
and print the first and five lines of a file.
import pandas as pd
First 5 Rows:
Index Height(Inches)" "Weight(Pounds)"
0 1 65.78 112.99
1 2 71.52 136.49
2 3 69.40 153.03
3 4 68.22 142.34
4 5 67.79 144.30
Last 5 Rows:
Index Height(Inches)" "Weight(Pounds)"
195 196 65.80 120.84
196 197 66.11 115.78
197 198 68.24 128.30
198 199 68.02 127.47
199 200 71.39 127.88
import math
#Program 16: Use the following data (load it as CSV file) for this
exercise. Read thi or NumPy or using in-built matplotlib function.
#Important Note: It's Important to plot data that makes sense and is
relevant to the analysis w are trying to perform,
#in the code, we are plotting the sales trend by having the x-azis an
"Months and y-axis as "Total Units" This makes
#sense as it gives us the trend of units sold over the months However,
marking profits in the y-axis and labeling it as
#"Sold Units" does not make sense. To plot the profit trend, we need
to have the saxis as "Months" and y-axis as "Total Profit
#This gives us the trend of profits earned over the months. So, it's
important to plot relevant data in the ass and
#y-axis for the analysis we are trying to perform. We have mindified
the program title to display the total units of all months
#in y-axis.
import pandas as pd
import matplotlib.pyplot as plt
# Add title
plt.title("Sales Trend of All Products")
#Program 16 (a)
#Get total profit of all months and show line plot with the following
Style properties Generated line plot must include
#following Style properties:
# 1.line style dootted and line -color should be blue
#2.show legend at the lower right location
#X label name = Months
#Important Note: This program is to show the profits trend over the
months. To plot the profit , we need to have the x-axis as
#"Months" and y-axis as lower profit
import pandas as pd
import matplotlib.pyplot as plt
# Add title
plt.title("Sales Trend of All Products")
df = pd.read_csv('Documents/sale.csv')
# Plot the units sold data for each product
plt.plot(df["months"], df['pen'], color='blue', marker='o',
label="Pen")
plt.plot(df["months"], df['book'], color='red', marker='o',
label="Book")
plt.plot(df["months"], df['marker'], color='green', marker='o',
label="Marker")
plt.plot(df["months"], df['chair'], color='yellow', marker='o',
label="Chair")
plt.plot(df["months"], df['table'], color='purple', marker='o',
label="Table")
plt.plot(df["months"], df['pen stand'], color='orange', marker='o',
label="Pen Stand")
#Program 16 (c) Read chair and tablet sales data and show it using The
bar chart .
# the bar chart should display the number of units sold per month for
each product add a seperate bar
#for each product in the same chart
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('Documents/sale.csv')
# Extract the data for Chair and Table products
data = df[["months", "chair", "table"]]
# Add title
plt.title("Sales Trend of Chair and Table")
#Program 16 (d) Read all product sales data and show it using the
stack plot.
import pandas as pd
import matplotlib.pyplot as plt
# Add title
plt.title("Sales Trend of All Products")