How to Create a Patient Class in Python with Attributes?

Estimated read time 2 min read

To create a patient class in Python with attributes, you can define a new class using the class keyword and create instance variables (attributes) using the __init__() method. Here is an example code that creates a patient class with attributes:

class Patient:
    def __init__(self, name, age, gender, height, weight): = name
        self.age = age
        self.gender = gender
        self.height = height
        self.weight = weight

    def display_info(self):
        print('Age:', self.age)
        print('Gender:', self.gender)
        print('Height:', self.height)
        print('Weight:', self.weight)

# Create a new patient
p1 = Patient('John Doe', 35, 'Male', 175, 70)

# Display the patient's information

In this code, we define a new class Patient with the __init__() method. This method takes 5 arguments: name, age, gender, height, and weight. We use these arguments to initialize instance variables (attributes) with the same names using the self keyword.

We then define a display_info() method that prints out the patient’s information.

We create a new patient object p1 by calling the Patient() constructor with the patient’s information as arguments.

Finally, we display the patient’s information by calling the display_info() method on the p1 object.

After running this code, you should see the patient’s information printed out in the console.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply