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

Class

The document provides an example of a Python class named 'Car' with methods to initialize attributes, display car information, and simulate starting the engine. It includes a constructor (__init__) to set the make, model, and year of the car. Additionally, it demonstrates creating an instance of the Car class and calling its methods to display information and start the engine.

Uploaded by

yoeurthsaiyann20
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Class

The document provides an example of a Python class named 'Car' with methods to initialize attributes, display car information, and simulate starting the engine. It includes a constructor (__init__) to set the make, model, and year of the car. Additionally, it demonstrates creating an instance of the Car class and calling its methods to display information and start the engine.

Uploaded by

yoeurthsaiyann20
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Here's an example of a Python class with a method:

python

Copy

class Car:

def __init__(self, make, model, year):

# Constructor to initialize the attributes

self.make = make

self.model = model

self.year = year

def display_info(self):

# Method to display the information of the car

return f"{self.year} {self.make} {self.model}"

def start_engine(self):

# Method to simulate starting the car engine

return f"The {self.make} {self.model}'s engine is now running."

# Creating an instance of the Car class

my_car = Car("Toyota", "Corolla", 2020)

# Calling the methods

print(my_car.display_info()) # Output: 2020 Toyota Corolla

print(my_car.start_engine()) # Output: The Toyota Corolla's engine is now running.

Explanation:

1. __init__ method: This is the constructor method that initializes the object's attributes.

2. display_info method: This method returns a string containing information about the car.

3. start_engine method: This method simulates starting the car's engine

Here's an example of a Python class with a method:


python

Copy

class Car:

def __init__(self, make, model, year):

# Constructor to initialize the attributes

self.make = make

self.model = model

self.year = year

def display_info(self):

# Method to display the information of the car

return f"{self.year} {self.make} {self.model}"

def start_engine(self):

# Method to simulate starting the car engine

return f"The {self.make} {self.model}'s engine is now running."

# Creating an instance of the Car class

my_car = Car("Toyota", "Corolla", 2020)

# Calling the methods

print(my_car.display_info()) # Output: 2020 Toyota Corolla

print(my_car.start_engine()) # Output: The Toyota Corolla's engine is now running.

Explanation:

1. __init__ method: This is the constructor method that initializes the object's attributes.

2. display_info method: This method returns a string containing information about the car.

3. start_engine method: This method simulates starting the car's engine

You might also like