Posts

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

Python List & Dictionary Comprehension: Behind the Scenes

Python List Comprehension  🎉 Hi! Welcome to this short tutorial on Python List Comprehension. You will learn about the basic syntax of this wonderful feature that Python offers and then we will dive into some examples. Sounds exciting, right? Let’s begin! 👍 First of all… what is list comprehension? It is a  more compact  way to write lists in Python. By using list comprehensions, you can merge several lines into a single line that indicates which elements should be added to the list. Let’s see how this works. Without list comprehension:  👋 Let’s say that you want to iterate over a sequence and only include items that meet specific criteria. You would usually write something like this using loops and conditionals: list1 = [] for i in range(15): if i % 2 == 0: list1.append(i) With list comprehensions:  👍 But... you could make an  equivalent code in just one line  by using list comprehension: list1 = [i for i in range(15)...

Lists vs. Tuples: Behind the Scenes

Image
Hi, welcome to this new tutorial! 😀 We will dive into the similarities and differences between lists and tuples. It’s very important that you understand their key differences, so let’s begin! 👍 🎓  Syntax and Structure The syntax used to define lists and tuples is very similar, but they differ in a very important aspect:  lists are enclosed in square brackets [ ] and tuples are enclosed in parentheses ( ). They  both contain values separated by commas  and those values can be of different types, so you can include integers, strings, floats, other lists, and even tuples as values. (Lists within lists are called “Nested Lists” and tuples within tuple are called “Nested tuples”). They have a  structure that is very similar to the grid that we used to work with strings . Each element can be accessed by using its corresponding index, like we did with strings in Weeks 1 and 2. The first value can be accessed using index 0, the second value using index 1...

Recursion: Behind the Scenes

Image
Welcome to Recursion! Hi! Welcome to this introduction to recursion, it’s great to have you here. 😀 This is a very powerful concept in programming…  a function that calls itself!  I know what you must be thinking: What?! The function calls itself? Why would you want to do that? 😅 Recursion is used when the  result of a problem depends on the result of smaller versions of the same problem . Sounds strange? Don't worry! You will learn what it means with this tutorial. 😀 Let’s dive into the details  with an example, calculating factorial. Meet Factorial 👋 The factorial of a positive integer n is the result multiplying all the integers less than or equal to n. For example, the factorial of 3 is 3 * 2 * 1 (by convention, the factorial of 0 is 1) Factorial is denoted as  n!  in mathematics. This is the code we will be working with. Please take a moment to analyze it. Base Case I’m sure that you noticed that there is a call to factorial...