TERM-2
Artificial Intelligence
Practical File
Submitted By- Aadeev Raina
Class: IX-A
Session- 2024-25
Class Roll No:1
List of Codes: -
1. Write a program to find square of number 7: -
# Program to find the square of a number
Number = 7
square = number ** 2
print (f"The square of {number} is {square}")
O/P
The square of 7 is 49
2. Write a Python program to print table of 5 upto
10 terms: -
# Program to print the table of 5 up to 10 terms
number = 5
terms = 10
print (f"Table of {number} up to {terms} terms:")
for i in range (1, terms + 1):
print(f"{number} x {i} = {number * i}")
O/P
Table of 5 up to 10 terms: 5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
3. Write a Python program to calculate Area and
Perimeter of a rectangle: -
# Program to calculate the area and perimeter of a rectangle
# Input the length and width of the rectangle
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Calculate area and perimeter
area = length * width
perimeter = 2 * (length + width)
# Display the results
print(f"\nThe area of the rectangle is: {area}")
print(f"The perimeter of the rectangle is: {perimeter}")
O/P
Enter the length of the rectangle: 5
Enter the width of the rectangle: 3
The area of the rectangle is: 15.0
The perimeter of the rectangle is: 16.0
[Link] a Python program to calculate average marks of 3
subjects: -
# Program to calculate the average marks of three subjects
# Input marks for three subjects
subject1 = float(input("Enter marks for Subject 1: "))
subject2 = float(input("Enter marks for Subject 2: "))
subject3 = float(input("Enter marks for Subject 3: "))
# Calculate the average
average_marks = (subject1 + subject2 + subject3) / 3
# Display the result
print(f"\nThe average marks of the three subjects are: {average_marks:.2f}")
O/P
Enter marks for Subject 1: 85
Enter marks for Subject 2: 90
Enter marks for Subject 3: 78
The average marks of the three subjects are: 84.33
5. Write a program to calculate Simple Interest. Accept
value of Principal, Rate and Time from the user: -
# Program to calculate Simple Interest
# Input values for Principal, Rate, and Time
principal = float(input("Enter the Principal amount: "))
rate = float(input("Enter the Rate of interest (in %): "))
time = float(input("Enter the Time period (in years): "))
# Calculate Simple Interest
simple_interest = (principal * rate * time) / 100
# Display the result
print(f"\nThe Simple Interest is: {simple_interest:.2f}")
O/P
Enter the Principal amount: 1000
Enter the Rate of interest (in %): 5
Enter the Time period (in years): 2
The Simple Interest is: 100.00
6. Write a program to print odd numbers from 1 to 100: -
# Program to print odd numbers from 1 to 100
print ("Odd numbers from 1 to 100:")
for number in range (1, 101, 2): # Start from 1, increment by 2 to get odd
numbers
print (number, end=" ")
O/P
Odd numbers from 1 to 100:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57
59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
7. Create a menu-based Calculator using Python: -
# Menu-based Calculator
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Error! Division by zero."
while True:
# Display menu
print("\nCalculator Menu:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Exit")
# Get user choice
choice = input("Enter your choice (1-5): ")
if choice == '5':
print("Exiting the calculator. Goodbye!")
break
if choice in ['1', '2', '3', '4']:
# Input numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Perform operation
if choice == '1':
result = add(num1, num2)
print(f"Result: {num1} + {num2} = {result}")
elif choice == '2':
result = subtract(num1, num2)
print(f"Result: {num1} - {num2} = {result}")
elif choice == '3':
result = multiply(num1, num2)
print(f"Result: {num1} * {num2} = {result}")
elif choice == '4':
result = divide(num1, num2)
print(f"Result: {num1} / {num2} = {result}")
else:
print("Invalid choice! Please select a valid option.")
O/P
Calculator Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice (1-5): 1
Enter the first number: 10
Enter the second number: 5
Result: 10 + 5 = 15.0
Calculator Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice (1-5): 5
Exiting the calculator. Goodbye!
8. Write a program in Python to input a number between
1 to 7 and print the corresponding day of a week (day
name): -
# Program to print the corresponding day of the week
# Input a number from the user
day_number = int(input("Enter a number between 1 and 7: "))
# Map numbers to days of the week
days_of_week = {
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
7: "Sunday"
# Check if the input is valid and print the corresponding day
if 1 <= day_number <= 7:
print(f"The day corresponding to {day_number} is {days_of_week[day_number]}.")
else:
print("Invalid input! Please enter a number between 1 and 7.")
O/P
Enter a number between 1 and 7: 3
The day corresponding to 3 is Wednesday.
O/P (case 2)
Enter a number between 1 and 7: 8
Invalid input! Please enter a number between 1 and 7.
9. Write a program to accept a number from user and to
check whether the entered number is even/odd
number: -
# Program to check if a number is even or odd
# Input a number from the user
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
print(f"The number {number} is an Even number.")
else:
print(f"The number {number} is an Odd number.")
O/P (Case 1): -
Enter a number: 8
The number 8 is an Even number.
O/P (Case 2): -
Enter a number: 7
The number 7 is an Odd number.
10. Write a program to accept radius of circle from user
and find the area of the circle using the math module:
-
# Import the math module to use the constant pi
import math
# Input the radius of the circle from the user
radius = float(input("Enter the radius of the circle: "))
# Calculate the area of the circle
area = [Link] * radius ** 2
# Display the result
print(f"The area of the circle with radius {radius} is: {area:.2f}")
O/P
Enter the radius of the circle: 5
The area of the circle with radius 5.0 is: 78.54
11. WAP in Python to find mean, median and mode and
standard deviation for the following data collected
from cars:
speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]
import statistics
# Data collected from cars
speed = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
# Calculate mean, median, mode, and standard deviation
mean_value = [Link](speed)
median_value = [Link](speed)
mode_value = [Link](speed)
std_deviation = [Link](speed)
# Display the results
print(f"Mean: {mean_value:.2f}")
print(f"Median: {median_value}")
print(f"Mode: {mode_value}")
print(f"Standard Deviation: {std_deviation:.2f}")
O/P
Mean: 89.77
Median: 87
Mode: 86
Standard Deviation: 10.51
Write a program to display bar chart Consider the following
12.
data of a school class IX students in Mathematics.
Name = ["Riya","Priya",”Ranak”,”Rita”]
Marks = [50,70,90,70]
import [Link] as plt
# Data for the bar chart
names = ["Riya", "Priya", "Ranak", "Rita"]
marks = [50, 70, 90, 70]
# Create the bar chart
[Link](names, marks, color='skyblue')
# Add titles and labels
[Link]("Class IX Mathematics Marks")
[Link]("Students")
[Link]("Marks")
# Display the chart
[Link]()
13. Write a program to display pie chart using matplotlib
library: -
import [Link] as plt
# Data for the pie chart
labels = ["Riya", "Priya", "Ranak", "Rita"]
marks = [50, 70, 90, 70]
colors = ["lightblue", "lightgreen", "orange", "pink"]
# Create the pie chart
[Link](marks, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
# Add a title
[Link]("Class IX Mathematics Marks")
# Display the chart
[Link]()
[Link] a program to draw line plot using matplotlib
library: -
import [Link] as plt
# Data for the line plot
students = ["Riya", "Priya", "Ranak", "Rita"]
marks = [50, 70, 90, 70]
# Create the line plot
[Link](students, marks, marker='o', linestyle='-', color='blue', label="Marks")
# Add titles and labels
[Link]("Class IX Mathematics Marks")
[Link]("Students")
[Link]("Marks")
# Add grid and legend
[Link](True)
[Link]()
# Display the plot
[Link]()
15.
Write a program to draw histogram charts using
matplotlib library: -
import [Link] as plt
# Data for the histogram
marks = [50, 70, 90, 70, 85, 95, 65, 75, 60, 55, 80, 100, 45, 65, 75, 85]
# Create the histogram
[Link](marks, bins=5, color='skyblue', edgecolor='black')
# Add titles and labels
[Link]("Distribution of Marks")
[Link]("Marks Range")
[Link]("Frequency")
# Display the plot
[Link]()
16. Create a List in Python of children selected for science quiz with
following
names-Arjun, Sonakshi, Vikram, Sandhya, Sonal, Isha and Kartik. Perform
the
following tasks on the list in sequence-
a) Print the whole list
b) Delete the name “Vikram” from the list
c) Add the name “Riya” at the end.
d) Remove the item which is at fourth position.
e) Print the length of the list.
f) Print the elements from second to fourth position using positive
indexing.
g) Reverse the list items.
# Create the initial list
children = ["Arjun", "Sonakshi", "Vikram", "Sandhya", "Sonal", "Isha", "Kartik"]
# a) Print the whole list
print("Initial list of children:", children)
# b) Delete the name "Vikram" from the list
[Link]("Vikram")
print("After removing 'Vikram':", children)
# c) Add the name "Riya" at the end
[Link]("Riya")
print("After adding 'Riya':", children)
# d) Remove the item at the fourth position (index 3)
removed_item = [Link](3)
print(f"After removing the fourth item '{removed_item}':", children)
# e) Print the length of the list
length = len(children)
print("Length of the list:", length)
# f) Print elements from second to fourth position (positive indexing)
sublist = children[1:4]
print("Elements from second to fourth position:", sublist)
# g) Reverse the list items
[Link]()
print("Reversed list:", children)
O/P
Initial list of children: ['Arjun', 'Sonakshi', 'Vikram', 'Sandhya', 'Sonal', 'Isha', 'Kartik']
After removing 'Vikram': ['Arjun', 'Sonakshi', 'Sandhya', 'Sonal', 'Isha', 'Kartik']
After adding 'Riya': ['Arjun', 'Sonakshi', 'Sandhya', 'Sonal', 'Isha', 'Kartik', 'Riya']
After removing the fourth item 'Sonal': ['Arjun', 'Sonakshi', 'Sandhya', 'Isha', 'Kartik', 'Riya']
Length of the list: 6
Elements from second to fourth position: ['Sonakshi', 'Sandhya', 'Isha']
Reversed list: ['Riya', 'Kartik', 'Isha', 'Sandhya', 'Sonakshi', 'Arjun']