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

Python Practical

The document outlines several practical Python programming exercises, including checking if a number is even or odd, calculating the factorial of a number, finding the greatest of three numbers, plotting graphs using Matplotlib, calculating statistical measures, and implementing linear regression with Scikit-learn. Each practical includes the aim, code, and results demonstrating successful execution. The exercises cover fundamental programming concepts and data visualization techniques.

Uploaded by

balwanpanchal100
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)
6 views8 pages

Python Practical

The document outlines several practical Python programming exercises, including checking if a number is even or odd, calculating the factorial of a number, finding the greatest of three numbers, plotting graphs using Matplotlib, calculating statistical measures, and implementing linear regression with Scikit-learn. Each practical includes the aim, code, and results demonstrating successful execution. The exercises cover fundamental programming concepts and data visualization techniques.

Uploaded by

balwanpanchal100
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

Practical 1: Even or Odd

Aim:

To write a Python program to check whether a number is even or odd.

Code:
num = int(input("Enter a number: "))

if num % 2 == 0:
print("Even number")
else:
print("Odd number")

OUTPUT :

Result:

The program successfully checks and displays whether the entered number is even or odd.
Practical 2: Factorial of a Number
Aim:
To write a Python program to find the factorial of a given number.

Code:
num = int(input("Enter a number: "))

fact = 1

if num < 0:

print("Factorial not defined for negative numbers")

elif num == 0:

print("Factorial is 1")

else:

for i in range(1, num + 1):

fact = fact * i

print("Factorial of", num, "is:", fact)

OUTPUT :

Result:
The program successfully calculates and displays the factorial of the given number.

Practical 3: Greatest of Three Numbers


Aim:
To write a Python program to find the greatest among three numbers.

Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:


print("Greatest number is:", a)
elif b >= a and b >= c:
print("Greatest number is:", b)
else:
print("Greatest number is:", c)

OUTPUT :

Result:
The program successfully finds and prints the greatest of three numbers.
Practical 4: Plotting using Matplotlib
Aim:
To write a Python program to plot 4 graphs simultaneously using matplotlib.

Code:
import [Link] as plt

# Data
x = [1, 2, 3, 4, 5]
y1 = [10, 20, 25, 30, 40]
y2 = [5, 15, 20, 25, 30]
y3 = [2, 10, 15, 20, 25]
y4 = [8, 18, 22, 28, 35]

# Create figure
[Link](figsize=(10, 8))

# Plot 1 - Line Plot


[Link](2, 2, 1)
[Link](x, y1, marker='o', linestyle='-')
[Link]("Line Plot")
[Link]("X-axis")
[Link]("Values")
[Link](True)
[Link](["Data 1"])

# Plot 2 - Bar Chart


[Link](2, 2, 2)
[Link](x, y2)
[Link]("Bar Chart")
[Link]("X-axis")
[Link]("Values")
[Link](True)

# Plot 3 - Scatter Plot


[Link](2, 2, 3)
[Link](x, y3)
[Link]("Scatter Plot")
[Link]("X-axis")
[Link]("Values")
[Link](True)
# Plot 4 - Pie Chart
[Link](2, 2, 4)
[Link](y4, labels=x, autopct='%1.1f%%')
[Link]("Pie Chart")

# Adjust layout
plt.tight_layout()
[Link]()

OUTPUT :

Result:
The program successfully displays four different types of plots (line, bar, scatter, and pie) in
a single window with proper layout and styling.
Practical 5: Mean, Median, Mode, Standard Deviation
Aim:
To write a Python program to calculate mean, median, mode, and standard deviation of a
list.

Code:
import statistics

data = [10, 20, 20, 30, 40, 50]

mean = [Link](data)
median = [Link](data)
mode = [Link](data)
std_dev = [Link](data)

print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
print("Standard Deviation:", std_dev)

OUTPUT :

Result:
The program successfully calculates and displays mean, median, mode, and standard
deviation.
Practical 5: Linear Regression using Scikit-learn
Aim:

To implement linear regression using scikit-learn in Python

Code:

from sklearn.linear_model import LinearRegression


import numpy as np
import [Link] as plt

# Dataset
X = [Link]([[1], [2], [3], [4], [5]])
y = [Link]([2, 4, 6, 8, 10])

# Create and train model


model = LinearRegression()
[Link](X, y)

# Prediction
pred = [Link]([[6]])
print("Prediction for 6:", pred)

# Extra predictions
pred_all = [Link](X)

# Graph
[Link](X, y) # actual data
[Link](X, pred_all) # regression line
[Link]("Linear Regression")
[Link]("X-axis")
[Link]("Y-axis")

[Link]()
OUTPUT :

Result
The program successfully creates a linear regression model, predicts values, and displays
the regression line on a graph.

You might also like