Posts

Showing posts from September, 2019

Intro to Python Errors and Exceptions: Behind the Scenes

Image
📌 Intro to Python Errors and Exceptions: Behind the Scenes ⚠️ Hi! Welcome to Week 4 and to this short introduction to Python errors and exceptions. This topic is essential for the process of debugging. When there are errors or bugs in a program (syntax or logical errors), you can usually detect them and correct them by reading the error message displayed. It’s like working as a detective 🔎. You need to read the error message, interpret it, and try to guess what might be causing the problem. It’s actually pretty fun if you think about it. You can fix the program by solving the “puzzle” and we usually learn very much from long debugging sessions. You need to do research, experiment, test, and fix, so they are amazing opportunities to learn. It all begins with a simple error message, and this is what we will discuss in this short introduction, a few of the different types of error messages that you can get in a Python program. Let’s begin! Vocabulary Exception : an excepti

Try-Except-Else-Finally: Behind the Scenes

Image
📌 Try-Except-Else-Finally: Behind the Scenes Hi! Welcome to this short tutorial where you will learn how  exception handling  works behind the scenes when you use  try-except-else-finally  blocks. Let’s begin! 👍 This is the basic structure used to handle exceptions in Python (diagram below). We have four blocks: try:  this is the code that you will try to run. What happens after this depends on whether an exception was thrown or not. except:  this block handles the exception if it occurs when the try block is executed. else:  this code block runs only if no exceptions were thrown in the try block. finally:  this block always runs, even if there were exceptions in the try block. As noted in the lecture, this block if very helpful for “clean up code” such as closing files. 💡 Note:  the else and finally blocks are optional. If there is a finally block, else must be included before the finally block. 🚩 Exception Thrown in the Try Block Let’s see what ha

Python Numeral Systems (Decimal, Binary, Octal, Hexadecimal): Behind the Scenes

Image
📌  Welcome to… Python Numeral Systems! 👋 Welcome to this new tutorial. This time, you will learn about a very interesting aspect of Python, how you can work with numbers in different numeral systems (decimal, binary, octal, and hexadecimal). Let’s begin!  😀 ✨ Intro to Numeral Systems Typically, when you work with numbers in Python, you use the  decimal number system . For example, when you enter the number 1101 in the interactive shell, you see this: >>> 1101 1101 The same number is returned because the “default” numeral system in Python is the decimal numeral system (base 10). Note:  the  base  of a numeral system is a key concept. The decimal numeral system has base 10 because it represents numbers using 10 digits (0-9) The resulting number is obtained by multiplying the digit by 10 raised to its corresponding power, moving from 0 for the rightmost element and increasing by one as we move to the left, as you can see in the following diagram: That

String Slicing using Negative Indices: Behind the Scenes

Image
📌  String Slicing using Negative Indices: Behind the Scenes  ⭐️ Welcome to this tutorial 👋 Now you will learn how to slice strings using negative Indices. Let's begin! You already know that strings have an internal structure that is very similar to a grid (please read  this tutorial  first if you are not familiar with this. You can also visualize it below). But  did you know that you can also refer to indices using negative numbers?  Yes, this surprised me very much as well! 😱 This is the grid structure of the  "Hello, World!"  string with its corresponding positive indices: You can now see the grid structure in the diagram below, with the corresponding  negative  indices: Notice that now we are counting backwards, starting from -1 for the last index all the way to -13 for the first index. We subtract one when we move one index to the left (or add one when we move one index to the right, which is equivalent). String slicing using negative ind