Posts

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

Recursion with Python

Recursion with Python Recursion with Python Recursion is a concept in programming that allows problem solving by dividing the problem into smaller recurring steps. Generally, this is done via recursive functions. A recursive function is a function that invokes itself. It comprises two fundamental components: the base case and the recursive case. Try googling the term “recursion” accurately. Base Case The base case is an exit condition. This prevents the function invoking itself indefinitely. Recursive Case The recursive case is where the function invokes itself. For each invocation, function arguments are updated so that the function can reach towards the base case. Let’s implement a recursive function to calculate the factorial of a number. def factorial ( num ) : # base case if num == 1 : return 1 # recursive case return num * factorial ( num - 1 ) NUM = 3 print ( f "Factorial of {NUM} is {fa...

Algorithms with Python

Algorithms with Python Algorithms with Python - Part 1 An algorithm, in layperson’s terms, is a sequence of instructions. They provide a step-by-step procedure to be followed by a computer, in order to solve a problem or make decisions. In this article, we will only be discussing the searching and sorting algorithms; albeit, there are more algorithms in the programming realm. Search Searching algorithms are used to locate particular elements within a data structure. Linear search and binary search are two prevalent searching algorithms. Linear Search Linear or sequential search is the simplest searching algorithm. This is implemented by iterating through the elements of the data structure one at a time and comparing each element against the target search item. Therefore, the time complexity of the algorithm - O( n n n ) - depends on the number of elements, making it suitable for smaller and simpler programs. Let’s implement a linear search algorithm to ...