diff --git a/1.py b/1.py new file mode 100644 index 0000000..a9cb1d5 --- /dev/null +++ b/1.py @@ -0,0 +1,59 @@ +## 1. School + +# 1. Create a `School` class with instance attribute `capacity`. +# 2. Add `students` as the class attribute. This will be a list and keep track of the students in the school. +# 3. Create a `Student` class with attributes: `name`, `age`, `gender` +# 4. Add `__str__` method to this class to print the objects. +# 6. Add `add_student` method to the class. If capacity is full print error message else add the student. +# 7. Add `print_students` method to print the all existing students. Loop through the students list and print each student object. +# 8. Create a `School` object and threee students, add first 2 students to school. Print students and afterwards try to add the third student. +# 9. Use `__dict__` method to see attributes + +class School: + + def __init__(self,capacity): + self.students=[] + self.capacity=capacity + + def add_student (self,*student) : + if len(self.students) >= self.capacity : + print ('Capacity is full!') + else: + for s in student : #ability to add multiple students at once + self.students.append (s) + + + def print_students (self): + for s in self.students : + print (s.__str__()) + + +class Student: + def __init__(self , name , age , gender): + self.name=name + self.age=age + self.gender=gender + + + def __str__(self): + return f''' + {self.name } + {self.age } + {self.gender} + ''' + +student1=Student('Henry s',19,'m') +student2=Student('J s',18,'f') +student3=Student('Mary x',17,'f') +school1=School (2) + + +#adding first two students : +school1.add_student(student1,student2) +school1.print_students() +#adding last student, but capacity is full! +school1.add_student(student3) + + +print (student1.__dict__) +print (school1.__dict__) \ No newline at end of file diff --git a/2.py b/2.py new file mode 100644 index 0000000..3938a92 --- /dev/null +++ b/2.py @@ -0,0 +1,28 @@ + +# ## 2. Rectangle + +# 1. Write a `Rectangle` class, allowing you to build a rectangle with `length` and `width` attributes. +# 2. Create a `perimeter()` method to calculate the perimeter of the rectangle and an `area()` method to calculate the area of ​​the rectangle. +# 3. Create a method `display()` that displays the length, width, perimeter and area of an object created using an instantiation on `Rectangle` class. + +class Rectangle: + def __init__(self, length, width) : + self.length=length + self.width=width + + def perimeter(self): + return (self.length + self.width)*2 + + def area(self): + return self.length * self.width + + def display(self): + return f''' + length : {self.length } + width : {self.width } + perimeter :{ self.perimeter()} + area : {self.area()} + ''' + +r1=Rectangle(3,4) +print (r1.display()) \ No newline at end of file diff --git a/3.py b/3.py new file mode 100644 index 0000000..63994c1 --- /dev/null +++ b/3.py @@ -0,0 +1,38 @@ + +# ## 3. BankAccount + +# 1. Create a Python class called `BankAccount` which represents a bank account, having as attributes: `accountNumber`, `name` , `balance`. +# 2. Create a **constructor** with parameters: `accountNumber`, `name`, `balance`. +# 3. Create a `deposit()` method which manages the deposit actions. (deposit() method will take parameter d and you will increase the balance with the amount d) +# 4. Create a `withdrawal()` method which manages withdrawals actions. (withdrawal() method will take parameter w, you will reduce the amount of balance with w, if w is larger than the balance: then print `Impossible operation! Insufficient balance!"`) +# 5. Create a `bankFees()` method to apply the bank fees with a percentage of 5% of the balance account. (When this method is called, the balance amount should reduce 5%) +# 6. Create a `display()` method to display account details. + +class BankAccount: + def __init__(self ,accountNumber , name,balance ): + self.accountNumber = accountNumber + self.name=name + self.balance=balance + + def deposit(self,d): + self.balance+=d + + def withdrawal(self , w) : + if self.balance