0% found this document useful (0 votes)
3 views48 pages

Python PDF

This document serves as a practical record for the B.Sc. Data Science Interdisciplinary Elective course on Python for Analytics at M.O.P. Vaishnav College for Women for the academic year 2022-2023. It includes a bonafide certificate, an index of various programming exercises, and detailed examples of Python programs demonstrating arithmetic operations, data structures, and data analysis techniques using libraries like NumPy and Pandas. The document outlines the practical applications of Python in analytics through multiple coding examples and their expected outputs.
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)
3 views48 pages

Python PDF

This document serves as a practical record for the B.Sc. Data Science Interdisciplinary Elective course on Python for Analytics at M.O.P. Vaishnav College for Women for the academic year 2022-2023. It includes a bonafide certificate, an index of various programming exercises, and detailed examples of Python programs demonstrating arithmetic operations, data structures, and data analysis techniques using libraries like NumPy and Pandas. The document outlines the practical applications of Python in analytics through multiple coding examples and their expected outputs.
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

M.O.P.

VAISHNAV COLLEGE FOR WOMEN


(AUTONOMOUS)

CHENNAI-600 034

[Link]. DATA SCIENCE


INTER DISCIPLINARY ELECTIVE (IDE)

PYTHON FOR ANALYTICS


CODE: 21UELE302E
(SEMESTER – IV)

2022 – 2023

REGISTER NUMBER:

CLASS & YEAR:


BONAFIDE CERTIFICATE

[Link]. DATA SCIENCE - INTER DISCIPLINARY ELECTIVE

This is to certify that __________________________ bearing Register

Number __________________ of _________________________, M.O.P.

VAISHNAV COLLEGE FOR WOMEN (AUTONOMOUS) has done

her practical record work on IDE – Python For Analytics during the

year 2022- 2023.

Date: 26.04.2023 Faculty in-Charge

External Examiner:_________________________

Internal Examiner: _________________________


INDEX

Page
[Link] Date Topic
No.
Program to demonstrate arithmetic operations and basic
1 20/12/22
IO statements.
Program to demonstrate the working of id() and type()
2 20/12/22
functions using different datatypes.
Program to find the largest of three numbers using
3 21/12/22
Nested if.
4 21/12/22 Program to find the grade of a student using If..Elif
Program to find the factorial of a given number using
5 27/12/22
For loop.
Python function that prints prime numbers in between 1
6 27/12/22
and 25
Python function that prints odd and even numbers for a
7 28/12/22
specified range
Program to demonstrate functions and modules using
8 28/12/22
import statement.
9 3/1/23 Program code to demonstrate List and related functions
Program code to demonstrate Tuple and related
10 3/1/23
functions
11 4/1/23 Program code to demonstrate Set and related functions
Program code to demonstrate Dictionary and related
12 4/1/23
functions
13 10/1/23 Program to demonstrate String Functions
14 11/1/23 Program to demonstrate Date Function
15 11/1/23 Program to demonstrate Time Function
Program to implement Numpy operations
(a)​ Creating one and two dimensional arrays
16 24/1/23 (b)​Shape and Reshape
(c)​ Concatenation and Split arrays
(d)​Searching, Sorting and Filter

17 25/1/23 Matrix Addition and Subtraction


18 31/1/23 Creating Pandas Series and Data Frame
19 1/2/23 Dataset Slicing, Iterations and Sorting
20 14/2/23 Reading CSV and JSON file
21 15/2/23 Group by and Aggregate Functions
22 28/2/23 Dataset analysis using descriptive Statistics
23 1/3/23 Data cleaning by removing null values and imputation.
Matplot Graphs
(a)​ Line Graphs – single, multiple
(b)​Barplot – stacked, grouped
24 7/3/23 (c)​ Histogram
(d)​Boxplot
(e)​ Scatter plot
(f)​ Pie chart

25 14/3/23 Analysing Iris Dataset


26 14/3/23 Analysing Batsman_Score Dataset
27 14/3/23 Analysing mtcars Dataset
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 1 Program to demonstrate arithmetic operations and basic


IO statements.
20/12/2025

AIM: To implement arithmetic operations and basic input output


statements.

PROGRAM:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link] CS

Ex: No: 2 Program to demonstrate the working of id() and type()


functions using different datatypes.
20/12/2025

AIM: To implement working of id() and type() functions

PROGRAM:
a = 10
b = 3.5
c = "Hello"
d = [1, 2, 3]
print(id(a), type(a))
print(id(b), type(b))
print(id(c), type(c))
print(id(d), type(d))

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 3 Write a Python program to find the largest of three


numbers using nested if statements.
20/12/2022

AIM: write and execute a Python program to determine the largest


among three given numbers using nested if.

PROGRAM:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b:
if a > c:
print("Largest number is:", a)
else:
print("Largest number is:", c)
else:
if b > c:
print("Largest number is:", b)
else:
print("Largest number is:", c
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

OUTPUT:
2413721034096
2413721034094
II BCOM
BCOM A
A&&FFSSIIB
IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 4 Write a Python program to find the grade of a student


using if..elif.
20/12/2022

AIM: To write and execute a Python program to display the grade of a


student based on the marks obtained using if..elif.

PROGRAM:
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 50:
print("Grade: C")
else:
print("Grade: Fail")

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 5 Program to print the factorial of a given number (get input
from the user and use for loop)

20/12/2022

AIM: To implement the factorial of a given number

PROGRAM:
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial = factorial * i
print("Factorial of", num, "is", factorial)

OUTPUT:
2413721034096
2413721034094
2413721042072
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link] CS
Ex: No: 6 Write a Python function that prints all prime numbers
between 1 and 25.

20/12/2022

AIM: To write and execute a Python function to display prime


numbers between 1 and 25.

PROGRAM:
def print_primes():
for num in range(2, 26):
for i in range(2, num):
if num % i == 0:
break
else:
print(num)
print_primes()

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link] CS
Ex: No: 7 Write a Python function that prints odd and even numbers
for a specified range.

20/12/2022

AIM: To write and execute a Python function to display odd and even
numbers within a given range.

PROGRAM:
def odd_even(start, end):
for i in range(start, end + 1):
if i % 2 == 0:
print(i, "is Even")
else:
print(i, "is Odd")
odd_even(1, 10)

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link] CS
Ex: No: 8 Program to demonstrate functions and modules using
import statement.

20/12/2022

AIM: To write and execute a Python program to demonstrate the use


of functions from a module using the import statement.

PROGRAM:
import math
print([Link](25))
print([Link](5))
print([Link](2, 3))

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link] CS
Ex: No: 9 Program code to demonstrate List and related functions

20/12/2022

AIM: To write and execute a Python program to demonstrate the use


of List and its related functions in Python.

PROGRAM:
lst = [10, 20, 30, 40]
[Link](50)
print(lst)
[Link](2, 25)
print(lst)
[Link](20)
print(lst)

[Link]()
print(lst)

[Link]()
print(lst)
OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 10 Program code to demonstrate Tuple and related


Functions

20/12/2022

AIM: To write and execute a Python program to demonstrate the use


of Tuple and its related functions in Python.

PROGRAM:
t = (10, 20, 30, 20, 40)
print(t)
print([Link](20))
print([Link](30))
print(len(t))
print(max(t))
print(min(t))

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 11 Write a Python program to demonstrate Set and related


functions.

20/12/2022

AIM: To write and execute a Python program to demonstrate the use


of Set and its related functions in Python.

PROGRAM:
s = {10, 20, 30, 40}
print(s)
[Link](50)
print(s)
[Link](20)
print(s)
[Link]([60, 70])
print(s)
print(len(s))
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link] CS
OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 12 Write a Python program to demonstrate Dictionary and


related functions.

20/12/2022

AIM: To write and execute a Python program to demonstrate the use


of Dictionary and its related functions in Python.

PROGRAM:
d = {"Name": "Ravi", "Age": 20, "Course": "Python"}
print(d)
print([Link]())
print([Link]())
print([Link]())
[Link]({"Age": 21})
print(d)
[Link]("Course")
print(d)

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link] CS
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link] CS
Ex: No: 13 Write a Python program to demonstrate String functions.

20/12/2022

AIM: To write and execute a Python program to demonstrate


commonly used String functions in Python.

PROGRAM:
s = "Python Programming"
print([Link]())
print([Link]())
print([Link]("Python", "Java"))
print([Link]())
print(len(s))

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 14 Write a Python program to demonstrate Date functions.

20/12/2022

AIM: To write and execute a Python program to demonstrate Date


functions in Python.

PROGRAM:
from datetime import date
d = [Link]()
print(d)
print([Link])
print([Link])
print([Link])

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 15 Write a Python program to demonstrate Time functions.

20/12/2022

AIM: To write and execute a Python program to demonstrate Time


functions in Python.

PROGRAM:
from datetime import datetime
t = [Link]().time()
print(t)
print([Link])
print([Link])
print([Link])

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

EX: NO: 16 Write a Python program to implement NumPy operations:


a) Creating one and two dimensional arrays
b) Shape and Reshape
c) Concatenation and Split arrays
d) Searching, Sorting and Filtering
AIM:
To write a Python program to perform basic NumPy array operations.
PROGRAM:
import numpy as np
# (a) Create arrays
a = [Link]([1,2,3,4])
b = [Link]([[5,6],[7,8]])
print("1D Array:",a)
print("2D Array:",b)
# (b) Shape and Reshape
print("Shape:",[Link])
r = [Link](2,2)
print("Reshaped Array:",r)
# (c) Concatenate and Split
c = [Link]((a,[9,10]))
print("Concatenated:",c)
s = np.array_split(c,2)
print("Split:",s)
# (d) Searching, Sorting, Filtering
print("Index of 3:",[Link](a==3))
print("Sorted:",[Link](a))
print("Filtered (>2):",a[a>2])

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 17 Write a Python program to perform matrix addition and subtraction using
NumPy.
AIM:
To write a Python program to perform addition and subtraction of two matrices.
PROGRAM:
import numpy as np
# first matrix
A = [Link]([[1,2], [3,4]])
# second matrix
B = [Link]([[5,6], [7,8]])
# matrix addition
add = A + B
print("Matrix Addition:")
print(add)
# matrix subtraction
sub = A - B
print("Matrix Subtraction:")
print(sub)
OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 18 Write a Python program to create a Pandas Series and Data Frame.
AIM:
To write a Python program to create a Series and Data Frame using Pandas.
PROGRAM:
import pandas as pd
# creating a series
a = [Link]([10,20,30,40])
print("Pandas Series:")
print(a)
# creating a data frame
data = {
"Name":["Ram","Sam","Ravi"],
"Marks":[85,90,78]
}
df = [Link](data)
print("Pandas DataFrame:")
print(df)

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 19 Write a Python program to perform dataset slicing, iteration and sorting
using Pandas.
AIM:
To write a Python program to perform slicing, iteration and sorting operations on a dataset.
PROGRAM:
import pandas as pd
# creating dataset
data = {
"Name":["Ram","Sam","Ravi","Anu"],
"Marks":[85,90,78,88]
}
df = [Link](data)
print("Dataset:")
print(df)
# slicing
print("Sliced Dataset:")
print(df[0:2])
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

# iteration
print("Iterating Names:")
for x in df["Name"]:
print(x)
# sorting
print("Sorted Dataset:")
print(df.sort_values("Marks"))

OUTPUT:
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 20 Write a Python program to read CSV and JSON files using Pandas.
AIM
To write a Python program to read data from CSV and JSON files using Pandas.
PROGRAM:
import pandas as pd
df = pd.read_csv("D:\\dataset\\[Link]")
print(df)
OUTPUT:

Ex: No: 21 Group by and Aggregate Functions


AIM :
To write a Python program using Pandas to demonstrate Group By and Aggregate Functions.
PROGRAM :
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

import pandas as pd

# Create simple data


data = {
"Department": ["Sales", "Sales", "HR", "HR", "IT"],
"Salary": [30000, 35000, 25000, 27000, 40000]
}

# Create DataFrame
df = [Link](data)

print("Original Data:")
print(df)

# Group by Department and find total salary


result = [Link]("Department").sum()

print("\nTotal Salary by Department:")


print(result)
OUTPUT :
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 22 Dataset analysis using descriptive Statistics


AIM :
To write a Python program using Pandas to perform dataset analysis using descriptive statistics.
PROGRAM :
import pandas as pd
# Create simple dataset
data = {
"Name": ["A", "B", "C", "D", "E"],
"Marks": [78, 85, 90, 66, 88]
}
# Create DataFrame
df = [Link](data)
print("Dataset:")
print(df)
# Descriptive statistics
print("\nDescriptive Statistics:")
print([Link]())
OUTPUT :
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 23 Data cleaning by removing null values and imputation


AIM :
To write a Python program to perform data cleaning by removing null values and filling missing
values (imputation) using Pandas.
PROGRAM :
import pandas as pd
data = {
"Name": ["A", "B", "C", "D"],
"Marks": [80, None, 75, None]
}
df = [Link](data)
print("Original Data:")
print(df)
# Remove rows with null values
clean_data = [Link]()
print("\nAfter Removing Null Values:")
print(clean_data)
# Fill null values with mean
df["Marks"] = df["Marks"].fillna(df["Marks"].mean())
print("\nAfter Imputation (Filling Missing Values):")
print(df)
OUTPUT :
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 24 : Matplot Graphs


(a) Line Graph – single, multiple
(b) Barplot – stacked, grouped
(c) Histogram
(d) Boxplot
(e) Scatter plot
(f) Pie chart
AIM :
To write a Python program using Matplotlib to demonstrate different types of graphs.
PROGRAM :
import [Link] as plt
import numpy as np

# Data
x = [1,2,3,4]
y1 = [10,20,30,40]
y2 = [15,25,35,45]

# (a) Line Graph - Single


[Link](x, y1)
[Link]("Single Line Graph")
[Link]()

# (a) Line Graph - Multiple


[Link](x, y1)
[Link](x, y2)
[Link]("Multiple Line Graph")
[Link]()
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

# (b) Bar Plot - Grouped


[Link](x, y1)
[Link](x, y2)
[Link]("Grouped Bar Plot")
[Link]()

# (b) Bar Plot - Stacked


[Link](x, y1)
[Link](x, y2, bottom=y1)
[Link]("Stacked Bar Plot")
[Link]()

# (c) Histogram
data = [5,7,8,7,2,3,4,6,9,10]
[Link](data)
[Link]("Histogram")
[Link]()

# (d) Box Plot


[Link](data)
[Link]("Box Plot")
[Link]()

# (e) Scatter Plot


[Link](x, y1)
[Link]("Scatter Plot")
[Link]()

# (f) Pie Chart


labels = ["A","B","C","D"]
sizes = [20,30,25,25]
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link](sizes, labels=labels)
[Link]("Pie Chart")
[Link]()
OUTPUT :
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link]
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link]
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link]
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

[Link]

Ex: No: 25 : Analysing Iris Dataset


AIM :
To write a Python program to analyze the Iris dataset using Pandas and Matplotlib.
PROGRAM :
import pandas as pd
import [Link] as plt
# Skip the first row so correct headers are used
df = pd.read_csv("[Link]", header=1)
print("First 5 rows")
print([Link]())
print("\nStatistical Summary")
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

print([Link]())
print("\nSpecies Count")
print(df["Species"].value_counts())
[Link](df["SepalLengthCm"], df["PetalLengthCm"])
[Link]("Sepal Length vs Petal Length")
[Link]("Sepal Length")
[Link]("Petal Length")
[Link]()
OUTPUT :

Ex: No: 26 Analysing Batsman_Score Dataset


AIM :
To write a Python program to analyse the Batsman_Score dataset using Pandas.
PROGRAM :
import pandas as pd
import [Link] as plt
# Read dataset
df = pd.read_csv("Batsman_Score.csv")
print("First 5 rows")
print([Link]())
print("\nDataset Information")
print([Link]())
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

print("\nStatistical Summary")
print([Link]())
# Runs frequency
print("\nRuns Scored Distribution")
print(df["RunsScored"].value_counts())
# Histogram of runs
[Link](df["RunsScored"])
[Link]("Distribution of Runs Scored")
[Link]("Runs")
[Link]("Frequency")
[Link]()
OUTPUT :
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

Ex: No: 27 : Analysing mtcars Dataset


AIM :
To write a Python program to analyse the mtcars dataset using Pandas.
PROGRAM :
import pandas as pd
df = pd.read_csv("[Link]")
print([Link]())
print("\nDataset Info")
print([Link]())
print("\nStatistics")
print([Link]())
print("\nAverage MPG")
print(df["mpg"].mean())
print("\nTop Horsepower Cars")
print(df[["model","hp"]].sort_values(by="hp",ascending=False).head())
OUTPUT :
2413721034096
2413721034094
II BCOM A & F S IIB
GAAYATHRI KS
DIVYADHARSHINI S.

You might also like