class with operation in pyhon

class ComplexNumber:
    def __init__(self, r=0, i=0):
        self.real = r
        self.imag = i

    def get_data(self):
        print(f'{self.real}+{self.imag}j')


# Create a new ComplexNumber object
num1 = ComplexNumber(2, 3)

# Call get_data() method
# Output: 2+3j
num1.get_data()

# Create another ComplexNumber object
# and create a new attribute 'attr'
num2 = ComplexNumber(5)
num2.attr = 10

# Output: (5, 0, 10)
print((num2.real, num2.imag, num2.attr))

# but c1 object doesn't have attribute 'attr'
# AttributeError: 'ComplexNumber' object has no attribute 'attr'
print(num1.attr)

3.75
4
Nearlife 75 points

                                    class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y


p1 = Point(1, 2)
p2 = Point(2, 3)
print(p1+p2)

3.75 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
class method example in python what is @ in python class class(object) python create class from type python python class function python create class what is the use of class in python define object python python class with functions python create class using type classes in python3 how to create a class python new class python create new class object python what is a class function in python what is class in python python class tutorial Class properties Python classs tutorial in python what are objects in python class method in py python class declaration] python object definition how to create class in python class declaration in python class in python class method in python classes and objects python python define a class with attributes python function class how to call class in python what is object of a class in python class function python python objects how to declare classes in python what is a class in python class and object in python python objects and classes pyton class simple python code with class and object python classs python class and object python create class object python what is class python classes and objects what is a class in python underneath class in py python simple class type create class python how to define class in python what does class do in python python classess implement a class in python pyhon class objects python example python class usage python what is an object class creating classes in python tutorial creating a class in python classes in python example how to create a class in python class inpython making a class python how to write class in python how methods work in classes python class object in python how to start a class in python class object python pythonn class is class an object in python create class python classes and objects in python create a class in python python class example objects in python classes python use class in python classes in python object in python class with operation in pyhon class basics python class in python 3 how to write a class in python python class(object) python new class class python what are classes in python python classes object class in python class python example def class python python object python class object python class method class methods in python python class class definition python syntax what is class and function in python object and class in python class object in function python how to use class python new class isntance python use + on class python what is a class class objecs in python how to make an item part of a class in python define methods in python class making object instance python python variable instance what do you call the variables in a class ouside of a method in python Class object doest have attribute type variable python simple example of person classes python python class ShanonFano how to create model class in python why does python thing some operations are not defined for some types although I have defined them which class gets declared automatically in the middle of the of the expression automatically and can be public , private in python find python class with two names class in python how to make a class python def is function or class in python {} object python self.answer pytho types of objkects in python python cvlass python how to use classes what does python class all do? what is a class puthon how to use python object how to use a class python object definition python pytyhon class example class pyhton class implementation in python how to declare a object in python types of objects in python craete object inpython example python class functions python for i in object class in class python object python python class code python objectsa and classes declare new object python python function between class class and functions in python methods and classes python how to view python classes designing classes in python how to access class defined under class in python class 'function' python get running objects of a class python python object declaration classes detailed python python declare class python class vs onject python new object create sample python class person use an object in class python class object method python what is used to create an object in python It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class: You can delete properties on classes by using the del keyword create python course python obj object orientation python w3schools python to using classes how to use classes python functions and classes in python what is class in python? can a class define an object in python define function using class python class inside class w3 python python class with methods class solution python python classes and objects explained python create a object how to define object in python learn classes nad all in python what is class on python python creating object function how an object is created in python python create class de python create new class an assign values class pyton definition of class in python how to create a class with attribute in python python object variables puyhon class can we make classes in python python simople class to demonstrate with statement classes python explained define an object in python python user class example
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source