0% found this document useful (0 votes)
14 views

Python 5 Unit

Uploaded by

kripalaalal4
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)
14 views

Python 5 Unit

Uploaded by

kripalaalal4
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
You are on page 1/ 4

UNIT-5

1. Python program to check whether a JSON string contain complex object or


not
import json

def is_complex_object(obj):

"""Check if the object is complex (i.e., a list or a dictionary)."""

return isinstance(obj, (dict, list))

def check_json_complexity(json_string):

"""Check if the given JSON string contains complex objects."""

try:

# Parse the JSON string

parsed_obj = json.loads(json_string)

# Check if the parsed object is complex

if is_complex_object(parsed_obj):

return True

else:

return False

except json.JSONDecodeError:

print("Invalid JSON string.")

return None

def main():

# Example JSON strings

json_strings = [

'{"name": "John", "age": 30}', # Simple JSON object

'[1, 2, 3, 4]', # Simple JSON array

'{"employees": [{"name": "John"}, {"name": "Jane"}]}', # Complex JSON object


'42', # Simple JSON value

'"Hello, World!"' # Simple JSON string

for json_string in json_strings:

result = check_json_complexity(json_string)

if result is not None:

if result:

print(f"The JSON string '{json_string}' contains a complex object.")

else:

print(f"The JSON string '{json_string}' does not contain a complex object.")

if __name__ == "__main__":

main()

Output:

The Json string (“name”:”john”,”age”:30)contains a complex object

The Json string ‘[1,2,3,4]’ contains a complen object

The Json string ‘{‘employes”:}”name”:”john”},”name”:”john”}}

The Json string does not contain a complex object

The Json string “hello; world”, does not contain a complex object

2.python program to demonstrate Numpy arrays creation using array()


function
import numpy as np

def main():

# Create a one-dimensional array

one_d_array = np.array([1, 2, 3, 4, 5])


print("One-dimensional array:")

print(one_d_array)

# Create a two-dimensional array (matrix)

two_d_array = np.array([[1, 2, 3], [4, 5, 6]])

print("\nTwo-dimensional array:")

print(two_d_array)

# Create a three-dimensional array

three_d_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

print("\nThree-dimensional array:")

print(three_d_array)

# Create an array of floats

float_array = np.array([1.5, 2.5, 3.5])

print("\nArray of floats:")

print(float_array)

# Create an array with a specific data type

int_array = np.array([1, 2, 3], dtype=np.int32)

print("\nArray with specific data type (int32):")

print(int_array)

# Create an array of strings

string_array = np.array(['apple', 'banana', 'cherry'])


print("\nArray of strings:")

print(string_array)

if __name__ == "__main__":

main()

Output:

One dimensional array: [1 ,2 ,3]

Two dimensional array: [1 ,2 ,3] [4 ,5, 6]

Three dimensional array: [[1, 2 ]] [3,4]][[5,6][7,8]]

Array of floats:[1.5 2.5 3.5]

Array with specific d type: ( int 3 2)

Array ofstring:[“apple,banana,cherry”]

You might also like