What's new

Welcome to ComSci.

Welcome to our Computer Science Forum! Dive into discussions, resources, and collaborations with fellow enthusiasts. Let's explore the endless possibilities of technology together!

Ask question

Ask questions and get answers from our community

Answer

Answer questions and become an expert on your topic

Contact us

Contact the site administrator directly.

class example

Chrollo

Administrator
Staff member
Joined
May 11, 2024
Messages
16
Reaction score
0
Points
6
Python:
class phm:
    def __init__(self, name, company):
        self.name = name
        self.company = company
 
    def show (self):
        print ("hello my name is: " + self.name + " and I'm working at: " + self.company + ".")
     
obj = phm ("John","Phantasma")
obj.show()
Try it Yourself

Explanation:

In this example, we are creating a phm class and we have created the name, and company instance variables in the constructor. We have created a method named show() which returns the string “Hello my name is ” + {name} +” and I work in “+{company}+”.”.We have created a person class object and we passing the name John and Company Phantasma to the instance variable. Finally, we are calling the show() of the class.

Python:
class phm:
    def __init__(self, name, company):
        self.name = name
        self.company = company
 
    def show (self):
        print ("hello my name is: " + self.name + " and I'm working at: " + self.company + ".")
     
name = (input("Enter your name: "))
company = (input("Enter your company: "))
print("\n")
obj = phm (name, company)
obj.show()
Try it Yourself

Explanation:

I used the same manner, but the difference is that I used the input() function. This set of code requests user input and stores it in the message variable, which contains the name and company.
 
shape1
shape2
shape3
shape4
shape5
shape6
Top