Skip to content

Commit bce2046

Browse files
Add files via upload
1 parent 68bf8bc commit bce2046

File tree

7 files changed

+250
-0
lines changed

7 files changed

+250
-0
lines changed

week_11/Abstraction.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
### Abstraction
2+
3+
Abstraction is the concept of hiding complex implementation details and showing only the necessary features of an object. It simplifies interactions by focusing on what an object does rather than how it does it.
4+
5+
For example, when you use a smartphone, you don’t need to understand the internal circuits and software. You simply use the phone's interface to make calls, send messages, or browse the internet. The intricate details are hidden, allowing you to focus on the phone’s features.
6+
7+
### Two Ways of Abstraction
8+
9+
1. **Abstract Class**:
10+
- An abstract class is a class that cannot be instantiated on its own and is meant to be subclassed. It can include abstract methods (methods without implementation) that must be implemented by subclasses.
11+
- **Python Example**:
12+
13+
```python
14+
from abc import ABC, abstractmethod
15+
16+
class Vehicle(ABC):
17+
@abstractmethod
18+
def start(self):
19+
pass
20+
21+
@abstractmethod
22+
def stop(self):
23+
pass
24+
25+
class Car(Vehicle):
26+
def start(self):
27+
print("Car is starting")
28+
29+
def stop(self):
30+
print("Car is stopping")
31+
32+
my_car = Car()
33+
my_car.start() # Output: Car is starting
34+
my_car.stop() # Output: Car is stopping
35+
```
36+
37+
2. **Abstract Method**:
38+
- An abstract method is a method defined in an abstract class without an implementation. Subclasses that inherit from the abstract class must provide their own implementation of these methods.
39+
- **Python Example**:
40+
41+
```python
42+
from abc import ABC, abstractmethod
43+
44+
class Shape(ABC):
45+
@abstractmethod
46+
def area(self):
47+
pass
48+
49+
class Rectangle(Shape):
50+
def __init__(self, width, height):
51+
self.width = width
52+
self.height = height
53+
54+
def area(self):
55+
return self.width * self.height
56+
57+
my_rectangle = Rectangle(4, 5)
58+
print(my_rectangle.area()) # Output: 20
59+
```
60+
61+
In these examples:
62+
- The **abstract class** `Vehicle` defines methods `start` and `stop` without implementing them. The subclass `Car` must provide implementations for these methods.
63+
- The **abstract method** `area` in the `Shape` class is implemented by the `Rectangle` subclass, which provides the specific logic for calculating the area of a rectangle.

week_11/Class & Object.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### Class and Object in Simple Words
2+
3+
- **Class ** & ** Object**:
4+
A class is just like a blueprint group of attributes and methods,
5+
6+
Attributes represented by variables that represent data
7+
Methods perform an action or task similar like function
8+
9+
10+
#### Example
11+
12+
Imagine a class called `Car`:
13+
14+
```python
15+
class Car:
16+
name="hello World" #atribute
17+
age=22 #atribute
18+
19+
def show(self): #method
20+
print(self.name,seld.age)
21+
22+
23+
#creating an object of class
24+
obj=Car()
25+
obj.show()
26+
print(obj.name)
27+
print(car.age)
28+
```
29+
The code defines a class named `Car` with two attributes, `name` set to `"hello World"` and `age` set to `22`, and a method `show` that prints these attributes. To use this class, an object `obj` is created from it using `obj = Car()`. This object now has access to all the attributes and methods defined in the class. When `obj.show()` is called, it prints the `name` and `age` of the `obj`, outputting `"hello World 22"`. Additionally, the attributes of the object can be accessed directly using `print(obj.name)` and `print(obj.age)`, which will print `"hello World"` and `22`, respectively. Note that there’s a typo in the original code snippet where `print(car.age)` should be `print(obj.age)` to correctly reference the created object.
30+
31+

week_11/Constructor.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
### Constructor
2+
3+
A constructor is a special method in a class that is automatically called when an object is created. It is used to initialize the object's attributes with specific values when the object is created.
4+
5+
#### Example Program
6+
7+
Here's a simple example of a class with a constructor in Python:
8+
9+
```python
10+
class Person:
11+
def __init__(self, name, age):
12+
self.name = name # Initialize the name attribute
13+
self.age = age # Initialize the age attribute
14+
15+
def display(self):
16+
print(f"Name: {self.name}, Age: {self.age}")
17+
18+
# Creating an object of the Person class
19+
person1 = Person("Alice", 30)
20+
21+
# Using the object's method
22+
person1.display() # Output: Name: Alice, Age: 30
23+
```
24+
`__init__` is the constructor method. It takes `self` (which represents the object itself) and two other parameters, `name` and `age`.
25+
26+
When `person1 = Person("Alice", 30)` is executed, the constructor initializes `person1`'s `name` and `age` attributes with `"Alice"` and `30`, respectively.
27+
28+
`person1.display()` then prints the values of `name` and `age` for the `person1` object.

week_11/Encapsulation.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
### Encapsulation
2+
3+
Encapsulation is a principle that involves bundling the data (attributes) and methods (functions) into a single unit, which is a class. It also involves restricting access to some of the object's components, which helps to protect the internal state of the object and control how it can be accessed or modified.
4+
5+
For example, think of a class as a box that contains both data and the operations that can be performed on that data. You only interact with the data through well-defined methods, which ensures that the data is used correctly and safely.
6+
7+
### Example of Encapsulation
8+
9+
Here's a simple Python example demonstrating encapsulation:
10+
11+
```python
12+
class BankAccount:
13+
def __init__(self, account_number, balance):
14+
self.__account_number = account_number # Private attribute
15+
self.__balance = balance # Private attribute
16+
17+
def deposit(self, amount):
18+
if amount > 0:
19+
self.__balance += amount
20+
print(f"Deposited {amount}. New balance: {self.__balance}")
21+
22+
def withdraw(self, amount):
23+
if 0 < amount <= self.__balance:
24+
self.__balance -= amount
25+
print(f"Withdrew {amount}. New balance: {self.__balance}")
26+
else:
27+
print("Insufficient funds or invalid amount")
28+
29+
def get_balance(self):
30+
return self.__balance
31+
32+
# Using the BankAccount class
33+
account = BankAccount("123456", 1000)
34+
account.deposit(500)
35+
account.withdraw(200)
36+
print(account.get_balance()) # Output: 1300
37+
38+
# Attempting to access private attributes directly (not recommended)
39+
print(account.__balance) # This will raise an AttributeError

week_11/OOP.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
**Object-Oriented Programming (OOP)** Object-oriented programming (OOP) in Python is a programming approach based on class and object
2+
3+
OOP is a programing methodology focusing on real world entity
4+
5+
6+
### Pillars of OOP:
7+
- **Class & Object**: Fundamental building blocks of OOP. A class defines a blueprint for objects, and an object is an instance of a class.
8+
- **Encapsulation**: Bundling the data and methods that operate on the data within a single unit, restricting access to some of the object's components.
9+
- **Inheritance**: Allows a new class to inherit properties and methods from an existing class, promoting code reuse.
10+
- **Polymorphism**: Enables one action to behave differently based on the object it is acting upon, allowing for flexibility in code.
11+
- **Abstraction**: Hiding the complex implementation details and showing only the essential features of an object.
12+
13+
### Programming Paradigms
14+
15+
There are several programming paradigms or styles that provide different ways of thinking about and structuring code. Here are the most common ones:
16+
17+
- **Procedural Programming**:
18+
- This is one of the simplest and oldest ways of programming. It focuses on a sequence of instructions or procedures to be executed.
19+
20+
- **Object-Oriented Programming (OOP)**:
21+
- Focuses on creating objects that represent real-world entities.
22+
23+
- **Functional Programming**:
24+
- focuse on the useing of functions and avoids dublication in the code

week_11/Polymorephism.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### Polymorphism
2+
3+
The word "polymorphism" means "many forms." In programming, polymorphism refers to the ability of methods, functions, or operators with the same name to operate on different types of objects or classes. It allows a single interface to be used for different underlying data types.
4+
5+
### Example of Polymorphism
6+
7+
Here's a simple Python example demonstrating polymorphism:
8+
9+
```python
10+
class Animal:
11+
def speak(self):
12+
print("Some generic animal sound")
13+
14+
class Dog(Animal):
15+
def speak(self):
16+
print("Woof!")
17+
18+
class Cat(Animal):
19+
def speak(self):
20+
print("Meow!")
21+
22+
# Function that uses polymorphism
23+
def make_animal_speak(animal):
24+
animal.speak()
25+
26+
# Using polymorphism
27+
dog = Dog()
28+
cat = Cat()
29+
30+
make_animal_speak(dog) # Output: Woof!
31+
make_animal_speak(cat) # Output: Meow!

week_11/inheritance.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Inheritance
2+
3+
Inheritance allows a class to inherit attributes and methods from another class. This helps in reusing code and creating a hierarchy of classes, making it easier to manage and extend code.
4+
5+
#### Example
6+
7+
Here's a simple example of inheritance in Python:
8+
9+
```python
10+
# Parent class
11+
class Animal:
12+
def __init__(self, name):
13+
self.name = name
14+
15+
def speak(self):
16+
print("Some generic animal sound")
17+
18+
# Child class inheriting from Animal
19+
class Dog(Animal):
20+
def speak(self):
21+
print("Woof!")
22+
23+
# Creating an object of the Dog class
24+
my_dog = Dog("Buddy")
25+
26+
# Using methods from both the parent and child classes
27+
print(my_dog.name) # Output: Buddy
28+
my_dog.speak() # Output: Woof!
29+
```
30+
In this example:
31+
32+
- **`Animal`** is the parent class with an attribute `name` and a method `speak`.
33+
- **`Dog`** is a child class that inherits from `Animal`. It overrides the `speak` method to provide a specific sound for dogs.
34+
- When `my_dog = Dog("Buddy")` is created, it inherits the `name` attribute from `Animal` and uses its own `speak` method to print `"Woof!"`.

0 commit comments

Comments
 (0)