Learn Python like a Back of Your Hand

Here is the explanation of python class given below


  • class Person :
    • This defines the class named 'Person' 
    • Classes are a way to create new objects in Python
    • Here we are creating a blueprint for creating objects in Python 
  • def __init_(self, name , age) :
    • This is a special method called a constructor
    • __init__ is used to initialize objects
    • It takes at least one argument
    • In this case argument is 'self'
    • Self refers the object itself
    • Here it takes other two arguments as well 'name' and 'age'

  • self.name = name
    • This means each object of this class has its own attribute called 'name'
  • self.age = age
    • This means each object of this class has its own attribute called 'age'
  • def info(self) :
    • This is a method 
    • It takes one argument 
    • self refers to the object itself
  • print(f"{}") :
    • log the information
    • f refers to the formatted strings
    • {}. use to represent parameters
  • person = Person("Emily", 34)
    • create an object called 'person' from the Person class
    • with variables values as 'Emily' and  '34'
  • person.info() ;
    • called the info() method on the person object

 

 

     

 

 

 

 

 

 

 

Comments