Python Program for Dot Notation with Examples

Object-Oriented Programming is a programming paradigm that is built on the idea of real-world Objects. Every object includes attributes that describe its state and methods that allow it to perform a specific task (equivalent to executing a function). Python is one of these languages.

Almost every entity in Python is traded as an Object. Knowing this is essential for understanding the importance of dot (.) notation.

Dot Notation

To put it simply, the dot (.) notation is a method of accessing the attributes and methods of each method of instances of various object classes.

It is normally preceded by the object instance, while the right end of the dot notation contains the attributes and methods.

Here is an example to create a class with many methods and then access them with the dot(.) notation.

Example

Approach:

  • Create a class say Employdetails.
  • Inside the class, initialize a constructor by passing the arguments as id, jobrole.
  • Initialize the value of id with the argument value(id).
  • Initialize the value of jobrole with the argument value(jobrole).
  • Create a function which prints some sample text.
  • Create a function that prints the value of the variables.
  • Create an object for the above class by passing the arguments as id and jobrole.
  • Print the attribute values using the dot notation.
  • Access the attribute values using the dot notation.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Employdetails.
class Employdetails():
    # Initialize a constructor by passing the arguments as id, jobrole
    def __init__(self, id, jobrole):
        # Initialize the value of id with the argument value(id)
        self.id = id
        # Initialize the value of jobrole with the argument value(jobrole)
        self.jobrole = jobrole
    # Create a function which prints some sample text

    def printSample(self):
        print("welcome to Python-programs")
    # Create a function  which prints the value of the variables

    def printjobrole(self):

        print(f"He is a {self.jobrole} at TCS")

# Create an object for the above class by passing the arguments as id and jobrole
Employdetails_obj = Employdetails(2122, "developer")

# Print the attribute values using the dot notation
print("Employee ID: ", Employdetails_obj.id)
print("Employee jobrole: ", Employdetails_obj.jobrole)
print()
# Access the attribute values using the dot notation
Employdetails_obj.printSample()
Employdetails_obj.printjobrole()

Output:

Employee ID:  2122
Employee jobrole:  developer

welcome to Python-programs
He is a developer at TCS
Where does the dot notation is used?

Anyone who has dealt with Python will be familiar with (.) notations. Here are a few instances you’ve probably seen before.

Example1: To get the list length

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the list using the len() function and store it in another variable.
  • Print the length of the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 3, 4, 5]
# Calculate the length of the list using the len() function
# and store it in another variable.
lst_len = len(gvn_lst)
# Print the length of the given list.
print("The length of the given list = ", lst_len)

Output:

The length of the given list =  5

The len() method is accessed using dot notation.

Example2: To split the string

# Give the list as static input and store it in a variable.
gvn_str = "welcome to-python programs-helloall"
# Split the given string by using the split() function based on the
# separator given and store it in another variable.
# If no separator is given it takes space by default.
rslt = gvn_str.split("-")
# Print the splitted string based on the separator given.
print("The splitted string based on the separator given:")
print(rslt)

Output:

The splitted string based on the separator given:
['welcome to', 'python programs', 'helloall']