### Week 14: Type Annotations, Modules, Inheritance, Access Modifiers, and Enums in Python
> By Akinlade Temitope
This week, I learned more about Python and how to write code that is easier to read, understand, and manage. The topics I focused on were type annotations, modules, inheritance, access modifiers, and enums.
#### TYPE ANNOTATIONS
Type annotations let us show what type of data a function or variable should use. This makes code easier to read and helps catch mistakes early.
Example:
```python
def add(x: int, y: int) -> int:
return x + y
```
Here, both x and y should be integers, and the function will return an integer.
#### MODULES
Modules are files that hold Python code, such as classes or functions. They help us organize our projects by keeping related code together. For example, we can write a class in one file and then import it into another file. This makes the project cleaner.
#### INHERITANCE
Inheritance means one class can take features from another class. This avoids repeating code.
Example:
```python
class Vehicle:
def move(self):
print("The vehicle is moving")
class Car(Vehicle):
def move(self):
print("The car is driving")
my_car = Car()
my_car.move()
```
Here, the Car class inherits from the Vehicle class but changes the move() method. This makes code reusable and simple.
#### ACCESS MODIFIERS
Access modifiers show how class variables or methods can be used:
Public: No special mark. Can be used anywhere.
Protected: One underscore _. Should be used only inside the class or subclasses.
Private: Two underscores __. Cannot be used directly outside the class.
Example:
```python
class BankAccount:
def __init__(self):
self.balance = 0 # Public
self._pin = 1234 # Protected
self.__password = "abc" # Private
```
This keeps sensitive data safe and used only in the right places.
#### ENUMS (ENUMERATIONS)
Enums are a way to group constant values with names. They make code easier to read and avoid using random numbers.
Example:
```python
from enum import Enum
class Menu(Enum):
RICE = 1
BEANS = 2
YAM = 3
```
Instead of just using 1, we can use Menu.RICE, which is clearer.
#### CONCLUSION
This week taught me how to write Python code that is simple and neat. Type annotations make code easy to read, modules keep it organized, inheritance saves time, access modifiers protect data, and enums make values clear. These tools help make programs better and easier to manage.