Posts

Data Structures - Part 1

Data Structures - Part 1 Data Structures (Abstract Data Types) - Part 1 (Array) Data structures are specific methods of organizing and storing collections of data in a way that enables efficient access and manipulation. In a previous article , we discussed python specific data structures (built-in data types: list, tuple, set & dictionary) . In this series of articles, we will explore common abstract data types (ADTs) that are language-agnostic. These ADTs can be classified into two categories: linear and non- linear. Linear data structures; Array , Linked List , Stack , Queue Non-linear data structures; Tree , Hash Map , Graph 1. Array Arrays are one of the most fundamental data structures, designed to store collections of data (elements) of the same data type . These elements are stored in contiguous memory locations, allowing access to each element using its index. Arrays are prone to an exception if accessed with an index that exceeds the array’s boun...

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...