5 WEEK Python Programs
5 WEEK Python Programs
AIM: Python program to check whether a JSON string contains complex object or not
PROGRAM:
import json
def is_complex_object(data):
if isinstance(data, dict):
return True
return True
return False
def check_json_complexity(json_string):
try:
data = json.loads(json_string)
return is_complex_object(data)
except json.JSONDecodeError:
return False
# Example usage
json_string1 = '{"name": "Alice", "age": 30, "details": {"hobbies": ["reading", "hiking"]}}'
OUTPUT:
True
False
False
RESULT: Python program for checked a JSON string contains complex object or not is successfully
executed.
AIM: Python Program to demonstrate NumPy arrays creation using array () function.
PROGRAM:
import numpy as np
print("1D Array:")
print(array_1d)
print("\n2D Array:")
print(array_2d)
# Creating a 3D array (a list of lists of lists)
print("\n3D Array:")
print(array_3d)
print("\nFloat Array:")
print(float_array)
print(int_array)
print("\nArray of Zeros:")
print(zeros_array)
print("\nArray of Ones:")
print(ones_array)
OUTPUT:
1D Array:
[1 2 3 4 5]
2D Array:
[[1 2 3]
[4 5 6]]
3D Array:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Float Array:
[1 2 3]
Array of Zeros:
[[0 0 0]
[0 0 0]]
Array of Ones:
[[1 1 1]
[1 1 1]]
RESULT: : Python Program to demonstrate NumPy arrays creation using array () function is successfully
executed.
PROGRAM:
import numpy as np
# Creating a 1D array
print("1D Array:")
print(array_1d)
print("Shape:", array_1d.shape)
print()
# Creating a 2D array
print("2D Array:")
print(array_2d)
print("Shape:", array_2d.shape)
print()
# Creating a 3D array
print("3D Array:")
print(array_3d)
print("Shape:", array_3d.shape)
print()
print("Float Array:")
print(float_array)
print("Shape:", float_array.shape)
OUTPUT:
1D Array:
[1 2 3 4 5]
Shape: (5,)
2D Array:
[[1 2 3]
[4 5 6]]
Shape: (2, 3)
3D Array:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Shape: (2, 2, 2)
Float Array:
Shape: (3,)
RESULT:
27) Python program to demonstrate basic slicing, integer and Boolean indexing.
AIM: A Python program to demonstrate basic slicing, integer and Boolean indexing.
PROGRAM:
import numpy as np
print("Original 2D Array:")
print(array_2d)
# Basic Slicing
print(sliced_array)
# Integer Indexing
print(integer_indexed_array)
# Boolean Indexing
boolean_indexed_array = array_2d[boolean_mask]
print(boolean_indexed_array)
condition_indexed_array = array_2d[condition_mask]
print(condition_indexed_array)
OUTPUT:
Original 2D Array:
[[ 10 20 30 40]
[ 50 60 70 80]
[[30 40]
[70 80]]
[ 70 100]
[10 20 30 40 50 60 70 80 90]
RESULT:
28) Python program to find min, max, sum, cumulative sum of array
AIM: A Python program to find min, max, sum, cumulative sum of array
PROGRAM:
import numpy as np
print("Original Array:")
print(array)
min_value = np.min(array)
print("\nMinimum Value:")
print(min_value)
max_value = np.max(array)
print("\nMaximum Value:")
print(max_value)
sum_value = np.sum(array)
print(sum_value)
cumulative_sum = np.cumsum(array)
print(cumulative_sum)
OUTPUT:
Original Array:
[10 20 30 40 50]
Minimum Value:
10
Maximum Value:
50
150
Cumulative Sum of Array:
[ 10 30 60 100 150]
RESULT:
29) Create a dictionary with at least five keys and each key represent value as a list where this list contains
at least ten values and convert this dictionary as a pandas data frame and explore the data through the
data frame as follows:
data = {
'Product': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
'Price': [10.99, 12.50, 8.99, 14.99, 7.50, 20.00, 15.75, 9.99, 13.50, 11.25],
'Quantity': [100, 150, 200, 120, 180, 90, 50, 300, 250, 80],
'Rating': [4.5, 4.0, 3.5, 4.7, 4.2, 3.9, 4.8, 4.1, 3.7, 4.6]
df = pd.DataFrame(data)
print("DataFrame head:")
print(df.head())
OUTPUT:
DataFrame head:
PROGRAM:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Hannah', 'Isaac', 'Jack'],
'Age': [24, 30, 22, 29, 25, 35, 27, 32, 28, 31],
'Salary': [50000, 60000, 45000, 70000, 52000, 80000, 75000, 58000, 62000, 90000],
'Department': ['HR', 'IT', 'Finance', 'IT', 'HR', 'Finance', 'HR', 'IT', 'Finance', 'IT'],
'Experience': [2, 5, 1, 4, 3, 8, 6, 5, 7, 9]
df = pd.DataFrame(data)
print("DataFrame:")
print(df)
# a) Applying head() function to the DataFrame
print("\nDataFrame head:")
print(df.head())
print(df['Name'])
print(df[['Name', 'Salary']])
print(df.iloc[:3])
OUTPUT:
DataFrame:
0 Alice 24 50000 HR 2
1 Bob 30 60000 IT 5
3 David 29 70000 IT 4
4 Eve 25 52000 HR 3
6 Grace 27 75000 HR 6
7 Hannah 32 58000 IT 5
9 Jack 31 90000 IT 9
DataFrame head:
0 Alice 24 50000 HR 2
1 Bob 30 60000 IT 5
3 David 29 70000 IT 4
4 Eve 25 52000 HR 3
0 Alice
1 Bob
2 Charlie
3 David
4 Eve
5 Frank
6 Grace
7 Hannah
8 Isaac
9 Jack
Name Salary
0 Alice 50000
1 Bob 60000
2 Charlie 45000
3 David 70000
4 Eve 52000
5 Frank 80000
6 Grace 75000
7 Hannah 58000
8 Isaac 62000
9 Jack 90000
0 Alice 24 50000 HR 2
1 Bob 30 60000 IT 5
9 Jack 31 90000 IT 9
Name Department
1 Bob IT
2 Charlie Finance
3 David IT
1 Bob 30 60000 IT 5
3 David 29 70000 IT 4
7 Hannah 32 58000 IT 5
9 Jack 31 90000 IT 9
30)select any two columns from the above data frame ,observe the change in
one attribute with respect to other attribute with scatter and plot operations
in matplotlib
Aim : write a python program to select any two columns from the above data
frame ,observe the change in one attribute with respect to other attribute with
scatter and plot operations in matplotlib
Program:
import pandas as pd
import matplotlib.pyplot as plt
# Create a DataFrame
df = pd.DataFrame(data)
output:
Result : The above select any two columns from the above data frame
,observe the change in one attribute with respect to other attribute with
scatter and plot operations in matplotlib program is successfully executed.
BEYOND SYLLABUS
1) Write a program to find a factorial of a given number
AIM: To write a program factorial of a given number.
PROGRAM:
def factorial_iterative(n):
if n < 0:
return "Factorial is not defined for negative numbers."
result = 1
for i in range(1, n + 1):
result *= i
return result
number = int(input("Enter a number to find its factorial: "))
print(f"The factorial of {number} is {factorial_iterative(number)}")
OUTPUT
Enter a number to find its factorial: 5
The factorial of 5 is 120
PROGRAM:
def is_perfect_number(n):
if n < 1:
sum_of_divisors = 0
if n % i == 0:
sum_of_divisors += i
return sum_of_divisors == n
# Example usage
if is_perfect_number(number):
else:
OUTPUT
Enter a number to check if it is a perfect number: 6
6 is a perfect number.