-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassandobj.py
More file actions
313 lines (230 loc) · 5.65 KB
/
Copy pathclassandobj.py
File metadata and controls
313 lines (230 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# class SharmaVishnu: ##Blue print of Object
# a = "lolo" # Class ke andar variable--> Attributes
# def sample(): #-- this is local function and if function not come inside the class then it is said to be global function
# print('This is sample function')
# def sample2(): ## class ke andar function-->Methods
# print("This is sample2 function")
# SharmaVishnu.sample()
# SharmaVishnu.sample2()
# print(SharmaVishnu.a)
# class Animal:
# #Attribute
# name = "Animal"
# #Method
# def greet(self): ## jab bhi class ke andar ke function ko object ke help se call karoge toh ek "PARAMETER" Set karna hoga.
# print("This is animal class")
# tau = Animal() #here tau is object.
# ##object ka naam same as hota hai as name of variable
# tau.greet()
# print(tau.name)
"""
create a class which will perform 2 task:
1.greet the user --"This is ------ class"
2.adding up two numbers."""
# class student():
# def greet(self):
# print("this is student class")
# def add(self):
# a = 10
# b = 30
# print(a+b)
# Ak = student()
# Ak.greet()
# Ak.add()
####Practice Question
# Q1. Create a Student Class
# ## Problem Statement
# Create a class named `Student` and create an object of that class.
# Print:
# - Student Name
# - Student Age
# ## Sample Output
# ```python
# Name: Raj
# Age: 17
# ```
# class Student:
# def __init__(self,name,age):
# self.name = name
# self.age = age
# def detail(self):
# print(f"Name:{self.name} \n Age = {self.age}")
# Jd = Student("Raj",17)
# Jd.detail()
# Q2. Create a Car Class
# ## Problem Statement
# Create a class named `Car`.
# Using object, store:
# - Car Name
# - Car Price
# Print both values.
# ## Sample Output
# ```python
# Car Name: BMW
# Car Price: 5000000
# ```
class Car:
def __init__(self,name,price):
self.name = name
self.price = price
def values(self):
print(f"Car name :- {self.name}\nCar price :-{self.price}")
ak = Car("BMW",5000000)
ak.values()
# # Q3. Use self Keyword
# ## Problem Statement
# Create a class named `Employee`.
# Create a method using `self` keyword to print:
# - Employee Name
# - Employee Salary
# ## Sample Output
# ```python
# Name: Rahul
# Salary: 45000
# ```
class Employee:
def __init__(self,name,salary):
self.name = name
self.salary = salary
def detail(self):
print(f"Employee name = {self.name}\nEmployee Salary = {self.salary}")
dk = Employee("Rahul",45000)
dk.detail()
### Q4. Mobile Class
# ## Problem Statement
# Create a class named `Mobile`.
# Store:
# - Brand Name
# - RAM
# Using object, print both values.
# ## Sample Output
# ```python
# Brand: Samsung
# RAM: 8GB
# ```
class Mobile:
def __init__(self,name,Brand):
self.name = name
self.Brand = Brand
def detail(self):
print(f"Brand Name = {self.name}\nRAM = 8GB")
ak = Mobile("Samsaung","8GB")
ak.detail()
# # Q5. Student Details using Method
# ## Problem Statement
# Create a class named `Student`.
# Create a method `show()` using `self` keyword and print:
# - Name
# - Course
# ## Sample Output
# ```python
# Name: Aman
# Course: Python
# ```
class Student:
def show(self):
self.name = "Aman"
self.course = "Python"
print(f"Name :- {self.name}")
print(f"Course:-{self.course}")
s = Student()
s.show()
# # Q6. Laptop Class
# ## Problem Statement
# Create a class named `Laptop`.
# Store:
# - Laptop Brand
# - Processor
# Print both details using object.
# ## Sample Output
# ```python
# Brand: HP
# Processor: i5
# ```
class Laptop:
def __init__(self,Brand,Processor):
self.Brand = Brand
self.Processor = Processor
def detail(self):
print(f"Brand name ={self.Brand}\nProcessor = {self.Processor}")
obj = Laptop("HP","i5")
obj.detail()
# # Q7. Book Class
# ## Problem Statement
# Create a class named `Book`.
# Create attributes:
# - Book Name
# - Book Price
# Print details using object.
# ## Sample Output
# ```python
# Book Name: Python Basics
# Book Price: 499
# ```
class Book:
def __init__(self,name,Price):
self.name = name
self.Price = Price
def detail(self):
print(f"Book Name = {self.name}\nBook Price = {self.Price}")
a = Book("Python Basics", 499)
a.detail()
# # Q8. Teacher Class using self
# ## Problem Statement
# Create a class named `Teacher`.
# Create method using `self` keyword to print:
# - Teacher Name
# - Subject
# ## Sample Output
# ```python
# Teacher Name: Neha
# Subject: Maths
# ```
class Teacher:
def __init__(self,name,subject):
self.name = name
self.subject = subject
def detail(self):
print(f"Teacher Name = {self.name}\nSubject = {self.subject}")
a = Teacher("Neha","Maths")
a.detail()
# # Q9. Create Multiple Objects
# ## Problem Statement
# Create a class named `Animal`.
# Create two different objects and print:
# - Animal Name
# ## Sample Output
# ```python
# Dog
# Cat
# ```
class Animal:
def __init__(self, animal_name):
self.animal_name = animal_name
def show(self):
print(self.animal_name)
a1 = Animal("Dog")
a2 = Animal("Cat")
a1.show()
a2.show()
# # Q10. Simple Bank Class
# ## Problem Statement
# Create a class named `Bank`.
# Store:
# - Account Holder Name
# - Balance
# Print details using object.
# ## Sample Output
# ```python
# Name: Ritik
# Balance: 50000
# ```
class Bank:
def __init__(self,name,Balance):
self.name = name
self.Balance = Balance
def sample(self):
print(f"NAME = {self.name}")
print(f"Balance = {self.Balance}")
a = Bank("Ritik",50000)
a.sample()