Thursday, May 2, 2024

BCA PART B 11. Create DataFramefrom Excel sheet using Pandas and Perform Operations on DataFrames

 


import pandas as pd

# making data frame from csv file
df = pd.read_excel("nba.xlsx")

# printing the first 10 rows of the dataframe
df[:10]

# Applying aggregation across all the columns
# sum and min will be found for each
# numeric type column in df dataframe

df.aggregate(['sum', 'min'])

BCA PART B 10. Create Array using NumPy and Perform Operations on Array

 


import numpy as np

arr = np.arange(1,11)
print(arr)

# returns the sum of the numbers
print(arr + arr)
# returns the diff between the numbers
print(arr - arr)
# returns the multiplication of the numbers
print(arr * arr )
# the code will continue to run but shows an error
print(arr / arr )

BCA PART B 9. Drawing Histogram and Pie chart using Matplotlib

 


import matplotlib.pyplot as plt
import numpy as np
y = np.random.normal(170, 10, 250)
plt.hist(y)
plt.show()

x = [64.73,18.43,3.37,3.36,10.11]
labels = ['Chrome', 'Safari', 'Edge', 'Firefox', 'Others']
plt.pie(x, labels=labels,autopct='%.1f%%')
plt.show()

BCA PART B 8. Demonstrate usage of advance regular expression

 


import matplotlib.pyplot as plt
country = ['A', 'B', 'C', 'D', 'E']
gdp_per_capita = [45000, 42000, 52000, 49000, 47000]
plt.plot(country, gdp_per_capita)
plt.title('Per Capita GDP')
plt.xlabel('Name ')
plt.ylabel('GDP')
plt.show()
plt.bar(country, gdp_per_capita)
plt.title('Per Capita GDP')
plt.xlabel('Name ')
plt.ylabel('GDP')
plt.show()

BCA PART B 7. Demonstrate Exceptions in Python

 a=int(input('Enter value of a: '))

b=int(input('Enter value of b: '))

try:
print('a/b=',a/b)
except ZeroDivisionError:
print("Sorry, Zero in the denominator.")

BCA PART B 6. Create a GUI using Tkinter module

 


from tkinter import *

# create root window
root = Tk()

# root window title and dimension
root.title("Welcome to VVFGC")
# Set geometry(widthxheight)
root.geometry('350x200')

#adding a label to the root window
lbl = Label(root, text = "Are you a Student?")
lbl.grid()

# Execute Tkinter
root.mainloop()

BCA PART B 5. Create SQLite Database and Perform Operations on Tables

 


import sqlite3

# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('VVFGC.db')

# cursor object
cursor_obj = connection_obj.cursor()

# Drop the table if already exists.
cursor_obj.execute("DROP TABLE IF EXISTS BCA")

# Creating table
table = """ CREATE TABLE BCA (
Email VARCHAR(255) NOT NULL,
First_Name CHAR(25) NOT NULL,
Last_Name CHAR(25),
Score INT
); """

cursor_obj.execute(table)

print("Table is Ready")

# Close the connection
connection_obj.close()

BCA PART B 4. Demonstrate use of Dictionary

 


n = 10

# declare dictionary
numbers = {}

# run loop from 1 to n
for i in range(1, n+1):
numbers[i] = i * i

# print dictionary
print(numbers)

BCA PART B 3. Demonstrate use of List

 


list = [10, 20, 30, "VVFGC", "Mysuru"]

# printing list
print("List elements are: ", list)

# adding elements
list.append (40)
list.append (50)
list.append ("Bengaluru")

# printing list after adding elements
print("List elements: ", list)

# removing elements
list.pop () ;
# printing list
print("List elements: ", list)
# removing elements
list.pop () ;
# printing list
print("List elements: ", list)

BCA PART B 2. Demonstrate usage of advance regular expression

 


import re
t="A fat cat doesn't eat oat but a rat eats bats."
mo = re.findall("[force]at", t)
print(mo)

BCA PART B 1. Demonstrate usage of basic regular expression

 


import re
print("Usage of square bracket : ")
string = "The quick brown fox jumps over the lazy dog"
pattern = "[a-m]"
result = re.findall(pattern, string)
print(result)

pattern = r"^The"
print("\n\n Search the pattern "+pattern+" at the end of the string ")
match = re.search(pattern, string)
if match:
print("Match found!")
else:
print("Match not found.")