Classes in Python

 Classes in Python


Classes are used to define new types of objects in Python. Here's an example:


python


class Person:

    def __init__(self, name, age):

        self.name = name

        self.age = age


    def greet(self):

        print("Hello, my name is " + self.name + " and I am " + str(self.age) + " years old.")


person = Person("Alice", 30)

person.greet()

In this example, we define a class called Person that has two attributes, name and age, and a method called greet that prints a greeting message. We create a new Person object with the name "Alice" and the age 30, and call the greet method on the object.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...