In Python, the dot (.) operator is used to access attributes and methods of an object.
Attributes
An attribute is a characteristic or property of an object. For instance, a car object might have attributes like color
, make
, and model
.
car = {
"color": "white",
"make": "Suzuki",
"model": "Dzire"
}
print(car.color) # Output: white
Methods
A method is a function associated with an object. It performs an action on or with the object. For example, a list object has methods like append
, remove
, and sort
.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
Common Use Cases:
- Accessing object properties:
car.color
- Calling object methods:
my_list.append(6)
- Importing modules:
import math
- Accessing module functions:
math.sqrt(25)