Posts

OOP with Python - Part 2

OOP with Python - Part 2 OOP with Python - Part 2 We covered the basics of Object-Oriented Programming (OOP) , including Classes and Objects, Encapsulation, and Inheritance in OOP with Python - Part 1 . In this second part, we will discuss the remaining two key principles of OOP; Abstraction and Polymorphism . Abstraction Abstraction is the concept of hiding the complex implementation details of a system and exposing essential features of an object. This allows us to focus on what an object does but not how it is done. This can reduce programming complexity and enhance code maintainability. In Python, abstraction can be achieved using abstract classes and methods provided by the abc module. An abstract class is a class that cannot be instantiated and typically contains one or more abstract methods. An abstract method is a method that is declared but contains no implementation. Subclasses of the abstract class must provide implementations for all abstract m...

OOP with Python - Part 1

OOP with Python - Part 1 OOP with Python - Part 1 OOP ( Object Oriented Programming ) is a programming paradigm (a design style or an approach with its related design principles and techniques) which considers objects as the primary building block of a software program. In our previous articles, we were following a procedural approach where we were writing our code in a sequential manner. As compared to the procedural approach, OOP provides benefits like modularity, ease of maintenance, flexibility, scalability and reusability which makes it ideal for large scale real world projects. Python, being a general purpose language, supports both procedural and OOP paradigms. So, in this article let’s talk about the basics of OOP and how to implement them in Python. Class & Object An object is a real world entity with properties (aka attributes or data or members) and behaviors (aka methods or functions). For instance, a cat is an object. It has a name, color ...

Debugging with Python - Part 3

Debugging with Python - Part 3 Debugging with Python - Part 3 Refer this article for Debugging with Python - Part 1. Refer this article for Debugging with Python - Part 2 (Debugging techniques - Part 1). Debugging techniques - Part 2 5. Exception Handling : Exception handling is the process used to prevent runtime errors occurring at the execution time, so that these errors wouldn’t cause our program to crash. In python, we can use try and except blocks to help us handle runtime errors and print out useful error messages. Let’s modify the simple calculator program we used in our type casting article to handle exceptions of user input and division by zero. while True : try : number1 = float ( input ( "Enter 1st number: " ) ) number2 = float ( input ( "Enter 2nd number: " ) ) result = round ( number1 / number2 , 2 ) except ValueError : print ( "Incorrect value, Enter a...

Debugging with Python Part 2

Debugging with Python Part 2 Debugging with Python - Part 2 Debugging Techniques - Part 1 Refer this article for Debugging with Python - Part 1. Now that we know about errors, let’s try to fix them or prevent them from happening. This process of identifying and resolving errors in the program is known as Debugging . Debugging is a common concept In every programming language and following concepts are common despite the language. Moreover, Python offers a variety of Python specific tools and techniques with regard to these common debugging techniques. Let’s explore these common concepts in debugging and available Python specific tools for each concept. In order to rectify compile errors, we discussed how to interpret error messages. Additionally, we can use tools like linters for code quality checks and benefit from code reviews for collective insights. To find and fix runtime errors, we could use tools and techniques like logging, exception handling, debu...

Debugging with Python - Part 1

Debugging with Python Part 1 Errors Debugging is the process of identifying and fixing bugs (i.e. errors in the code which lead to incorrect software functionality). So, before we discuss “ fixing bugs ”, let’s explore the types of errors we might make in our programs. We will be looking at the three basic types of errors; namely compilation errors, runtime errors and logical errors. 1. Compilation Errors Compilation errors occur during the compilation phase and prevent the program from running. These are generally syntax errors (i.e. violations of the syntactic rules of the programming language). However, Python is an interpreted language, and the Python interpreter checks for syntax errors as it reads and parses the script. It won’t run the script and will output an error message indicating the type of compilation error and the line of code so that we can easily rectify our mistake. Let’s look at two of the most familiar compilation errors we might encount...

Strings and RegEx with Python

Strings and RegEx with Python Strings & RegEx with Python Strings A String or a str object in Python, is a sequence of Unicode characters. In python, strings are written within either single or double quotes. We may also use triple quotes ( ''' ) or ( """ ) for multiline strings. Strings may include letters, numbers, symbols, and even spaces. Since, Python does not have a character data type, a single character is defined as a single length String. Strings are immutable, hence once created, their values cannot be modified. Strings are considered as arrays of characters. Hence, they possess properties similar to arrays. Strings support common sequence operations such as concatenation ( + ), repetition ( * ), slicing, indexing etc. Let’s write a simple code snippet to extract words from a sentence using indexing and slicing. The variable word1 is assigned a substring ( sentence[6:11] ) sliced from the variable sentence . It inclu...

Algorithms with Python Part 2

Algorithms with Python Part 2 Algorithms with Python - Part 2 Refer this article for Algorithms with Python - Part 1. Merge Sort Merge Sort is a divide-and-conquer algorithm that divides a data structure into smaller sub-collections, sorts them, and then merges the sorted sub-parts to produce a final sorted data structure. Merge sort can be implemented using recursion . Let’s implement a merge sort algorithm to sort an unsorted list of square numbers. We have defined the merge_sort function to take a data structure ( list ) as its parameter. We use this function recursively to split the left half and the right half of the list until a list reaches the base case of len(list) > 1 . Merging is done with the help of three while loops. Firstly, we split the original list into two sub lists ( left_half and right_half ) from the first element to the mid element ( left_half ) and from the mid element to the last element ( right_half ). Then we make recursi...