Posts

Showing posts with the label While loops

Nested Loops: Behind the Scenes!

Image
👋 Welcome! Hi! Welcome to this tutorial on  Nested Loops  where we will cover the basic principles with examples,  let’s begin!  👍 👀 First Example This is the basic structure of nested for loops. We have a for loop within another for loop. Let’s see an example in action! 📣 Note:  Please note that you should use different variables for each loop, since you might need to use these values in your code and if you use the same variable, the value can change and result in unexpected behaviors. 🔍 Step by Step! Now we have a real example of nested for loops. We have some code on the outer loop, then the inner loop and more code on the outer loop. Let’s walk through this code step by step. You can check the code and table below for a visual reference of the process. The outer loop  for i in range(3)  will start with an initial value of  0  for  i . The  print  statement will be executed. NOW.. You will...

'Break' Statement: Behind the Scenes!

Image
👋   Welcome! You will use  break  during the course and in your future projects. It's very important that you understand how it works and when to use it. 💻 Let’s begin!  👍 As you can see by the  (very subtle hahaha 😁)  graphics below, the  break  statement is the code equivalent of a  “STOP”  signal. 🔧  Use With the  break  keyword you can stop the execution of  while  loops and  for  loops. If you have nested loops, you will stop the nearest enclosing loop. (You will see examples in this tutorial ✨). ✅  Break Statement and While Loops! Let’s see a  break  statement inside a  while  loop and analyze the code in the  image below : Using  while True  means that you will enter an infinite loop if there is nothing stopping the loop because the condition will always evaluate to  True . Then, we check the if statement con...

Iterating over Lists and Tuples: BEHIND THE SCENES!

Image
Hi! Welcome to this section. This time, we will discuss iterables. During your problem sets you will discover the power of iterables and how they can completely change you software development thinking process. Formally, an iterable is "an object capable of returning its members one at a time . Some data types we have studied are iterables, as you can see in the slide below. Strings and lists and tuples are iterables. And later on you will study Dictionaries (you can iterate over their keys and their corresponding values). You can find more on iterables in Python's documentation:   https://docs.python.org/3.6/glossary.html But this concept seems so abstract, right? “Capable of returning its members one at a time” What does this mean? Iterables’ magic comes to life in for loops . Why? Because they return their elements one at a time on every iteration. This is better illustrated with an example to describe the required syntax: Iterables in Action!...

Control Flow: For Loops, While loops and range() BEHIND THE SCENES!

Image
Welcome to this section on Control Flow! Control flow  is an essential element of programming.  You can build very powerful programs with loops , so let's see how they work behind the scenes, and  how range() works , because you will use it in this course and in your future programming projects. So let's begin! Let's start with WHILE LOOPS: In While loops a block of code will be executed for as long as a condition is met. Every time the block of code is executed, the condition will be checked again, and if it evaluates to True, the code inside the loop runs again, otherwise it exits the loop and continues executing the code below it. When learning loops, a good strategy is to analyze each step and how values change when the code is executed. In this case, we determine if the code will be executed, the values for the variables involved each time the loop runs and what will be printed on each iteration. The value of n is updated every time the loop...