Adding material properties in blender using python?

I have created a cube in blender using Python API. The code I used is

import bpy

vertices=[(1,1,-1),(1,-1,-1),(-1,-1,-1),(-1,1,-1),(1,1,1),(1,-1,1),(-1,-1,1),(-1,1,1)]
edges=[]
faces=[(0,1,2,3),(4,5,6,7),(0,4,7,3),(0,1,5,4),(1,2,6,5),(7,6,2,3)]

new_mesh=bpy.data.meshes.new("new_mesh")
new_mesh.from_pydata(vertices, edges, faces)
new_mesh.update()

new_object = bpy.data.objects.new("new_object", new_mesh)
view_layer=bpy.context.view_layer
view_layer.active_layer_collection.collection.objects.link(new_object)

Now, I want to convert two faces → Face 1 =(0,1,2,3) and Face 2 = (4,5,6,7) of the cube into a glass material. This is what I did for adding a new glass material.

# Create a new material
face_to_convert_to_glass = [(0,1,2,3),(4,5,6,7)]
material_glass = bpy.data.materials.new('Green')
material_glass.use_nodes = True
p_BSDF = material_glass.node_tree.nodes["Principled BSDF"]
p_BSDF.inputs[0].default_value = (0, 1, 0, 1)
p_BSDF.inputs[7].default_value = 0
p_BSDF.inputs[15].default_value = 1
bpy.context.object.data.materials.append(material_glass)

It may be a simple thing but being a novice I am not able to link the glass material with the new_object I created. How do I achieve this using Python ?

Moved to : Coding – Python Support

your code is assuming that the object you created is selected, which it won’t be- assuming there’s not something else going on in your code that you have omitted. append the material directly to the mesh you created instead:

new_mesh.materials.append(material_glass)