1class LuckyLemur():
2 def __init__(self, description):
3 self.description = description
4
5 def reveal_description(self):
6 print(f'Lucky Lemur is {self.description}')
7
8lucky_lemur = LuckyLemur('Pro')
9lucky_lemur.reveal_description()
1class ComplexNumber:
2 def __init__(self, r=0, i=0):
3 self.real = r
4 self.imag = i
5
6 def get_data(self):
7 print(f'{self.real}+{self.imag}j')
8
9
10# Create a new ComplexNumber object
11num1 = ComplexNumber(2, 3)
12
13# Call get_data() method
14# Output: 2+3j
15num1.get_data()
16
17# Create another ComplexNumber object
18# and create a new attribute 'attr'
19num2 = ComplexNumber(5)
20num2.attr = 10
21
22# Output: (5, 0, 10)
23print((num2.real, num2.imag, num2.attr))
24
25# but c1 object doesn't have attribute 'attr'
26# AttributeError: 'ComplexNumber' object has no attribute 'attr'
27print(num1.attr)
1class Person:
2 "This is a person class"
3 age = 10
4
5 def greet(self):
6 print('Hello')
7
8
9# Output: 10
10print(Person.age)
11
12# Output: <function Person.greet>
13print(Person.greet)
14
15# Output: "This is a person class"
16print(Person.__doc__)
1class Vehicle:
2 def __init__(self, brand, model, type):
3 self.brand = brand
4 self.model = model
5 self.type = type
6 self.gas_tank_size = 14
7 self.fuel_level = 0
8
9 def fuel_up(self):
10 self.fuel_level = self.gas_tank_size
11 print('Gas tank is now full.')
12
13 def drive(self):
14 print(f'The {self.model} is now driving.')
15
12+3j
2(5, 0, 10)
3Traceback (most recent call last):
4 File "<string>", line 27, in <module>
5 print(num1.attr)
6AttributeError: 'ComplexNumber' object has no attribute 'attr'
1class LambdaClass:
2 x = lambda a, b, c, d, e, f: a + b + c + d + e + f
3 print(x(31231, 312, 312, 31, 12, 31))
4
5print(LambdaClass)