The 4 Pillars (Principles) of OOP in Python
The 4 Pillars (Principles) of OOP in Python
1. Encapsulation
Wrapping data & methods into a single unit (Class) and restricting direct access.
2. Abstraction
Hiding internal implementation details and showing only essential features.
3. Inheritance
Acquiring properties and methods of a Parent class in a Child class.
4. Polymorphism
One interface/method name behaving differently for different objects.
Explanation
- Encapsulation groups data with behaviour and abstraction exposes a simple interface.
- Inheritance reuses behaviour and polymorphism allows different objects to respond to the same operation.
5 Types of Inheritance Supported in Python
5 Types of Inheritance Supported in Python
1. Single
Parent A
↓
Child B
↓
Child B
2. Multilevel
Grandparent A
↓
Parent B → Child C
↓
Parent B → Child C
3. Multiple
Father A Mother B
↘ ↙
Child C
↘ ↙
Child C
4. Hierarchical
Parent A
↙ ↘
Child B Child C
↙ ↘
Child B Child C
5. Hybrid
Combination of two or more types of inheritance
Explanation
- Single, multiple and multilevel inheritance describe different parent paths.
- Hierarchical and hybrid structures show how several such relationships fit together.
Flow of try - except - else - finally in Python
Flow of try - except - else - finally in Python
1. try Block
Contains risky code that might raise an exception at runtime.
2. except Block
Catches and handles the exception if an error occurs inside
try.3. else Block (Optional)
Runs ONLY if NO exception was raised in the
try block.4. finally Block
Always executes (error or no error). Used to close files or DB connections!
Explanation
- An exception in try transfers control to a matching except clause.
- Else runs on success and finally normally runs as control leaves the statement.
Top Functions in Python's re Module
Top Functions in Python's re Module
1. re.findall(pattern, text)
Returns a list containing all non-overlapping matches in the string.
2. re.search(pattern, text)
Scans the entire string and returns a Match object for the first match (or
None).3. re.match(pattern, text)
Checks for a match ONLY at the very beginning (index 0) of the string.
4. re.sub(pat, repl, text)
Replaces (substitutes) all occurrences of the pattern with
repl.5. re.split(pattern, text)
Splits the string into a list wherever the pattern matches.
Explanation
- Search looks for a match anywhere and match tests from the start.
- Fullmatch checks the whole input, while sub and split transform or divide text using matches.