Accessing Graph Types

Accessing Node Properties

Select the correct method to access the name property from a node with a default value of "Unknown" if the property doesn’t exist.

python
node = result['person']
name = node.#select:get("name", "Unknown")
  • ❏ property("name", "Unknown")

  • ❏ value("name", "Unknown")

  • ✓ get("name", "Unknown")

  • ❏ fetch("name", "Unknown")

Hint

The get() method allows you to specify a default value if the property doesn’t exist, similar to Python dictionaries.

Solution

The correct answer is get(). This method allows you to safely access a node property while providing a default value if the property doesn’t exist.

python
name = node.get("name", "Unknown")

While you can also use node["name"], using get() is safer as it won’t raise an error if the property doesn’t exist.

Accessing Relationship Type

Select the correct property to access the type of relationship.

python
relationship = result['acted_in']
rel_type = relationship.#select:type
print(f"Type: {rel_type}")  # prints: Type: ACTED_IN
  • ❏ name

  • ❏ relationship_type

  • ✓ type

  • ❏ label

Hint

The relationship type (e.g., ACTED_IN`) is accessed through a simple property on the relationship object.

Solution

The correct answer is type. This property returns the type of the relationship as a string.

python
relationship = result['acted_in']
rel_type = relationship.type  # returns: "ACTED_IN"

The type property is the standard way to access the relationship type in Neo4j’s Python driver.

Lesson Summary

In this lesson, you correctly identified how to access properties from nodes and the type of a relationships.

In the next lesson, you will learn how to work with dates and times.