9 – L3 08 While Loops V3

“For loops” are an example of definite iteration, meaning that the loop’s body is run a predefined number of times. A “for loop” over a list, executes the body once for each element in the list. A “for loop” using the range function will execute the number of times specified by the range function. This … Read more

8 – For Loops

Now that you’ve learned about conditional statements, let’s move on to loops, which allow us to repeat blocks of code. Python has two kinds, for loops and while loops. Let’s first take a look at the for loop, which we can use to iterate over an iterable. An iterable is an object that can return … Read more

7 – Truth Value Testing

So far, the conditions we’ve used were Boolean expressions that evaluate to a Boolean object, either true or false. If we put some other object that is not a Boolean in the IF statement, Python will check for its truth value and use that to decide whether or not to run the indented code. Here, … Read more

6 – Good And Bad Examples

We’ll go over some good and bad examples of Boolean expressions used in if statements. While true is a valid Boolean expression, it’s not exactly useful as a condition as it always evaluates to true. So the intended code will always get run. Similarly, if false is not a condition you should run either, the … Read more

5 – Complex Boolean Expressions

All the if and elif statements we’ve seen so far have been followed by a single Boolean expression that checks only one condition. However, more complicated Boolean expressions can be useful as well. If the condition is working with the numerical variable, you might want to check whether a value lies in a certain range … Read more

4 – Indentation

As you have just seen, indentation is important. It’s how we tell Python what code is in the body of an if statement, and what code is outside of it. Indentation doesn’t just matter in if statements. Soon, you’ll see it used in other contexts. Some other languages use braces to show where blocks of … Read more

3 – If Elif and Else

Now you know how to execute a block of code if a condition is true. But what if you have a different block of code that you want to execute when that condition is false? You can use the ‘else’ keyword to do so. Consider this code, which prints a message indicating whether an integer … Read more

2 – If Statements

We’ve been running code that simply executes every line one by one from the top down. Many times however, we want to run code only if a particular condition holds. To demonstrate this concept, let’s take a look at this billing system for a pay-as-you-go mobile phone. Say a customer has a credit balance for … Read more

12 – List Comprehensions

In Python, you can create lists really quickly and concisely with a cool tool called List Comprehensions. In the cities example from earlier, we created a list of capitalized cities from the cities list in a for loop. With a list comprehension, we can get the same result like this. List comprehensions allow us to … Read more

11 – Zip and Enumerate

Looking back at our list of cargo, notice each element is a tuple of size two. Iterating through a list with multiple values, can be pretty helpful. It’s actually really easy to combine and split lists like this. If we originally started, with these two separate lists, items and weights, and wanted to combine them … Read more

10 – Break and Continue

For loops iterate over every element in a sequence, and while loops iterate until they’re stopping condition is met. This is sufficient for most purposes but we sometimes need more precise control over when a loop should end. In these cases, we use the break keyword. A loop will terminate immediately if it encounters a … Read more

1 – Introduction

Welcome to this lesson on Control Flow. Here, you’ll learn how to add more functionality to your code by being able to use conditional statements and loops. You’ll learn how to implement decision-making with if statements, repeat code with for and while loops, exit or skip loops with break and continue, use helpful built-in functions … Read more

3-1-3-34. Conclusion

Congratulations on completing this lesson on Control Flow. You learned how to use conditional statements, loops, useful built-in functions, and list comprehensions to add more functionality to your code. Next, you’ll learn how to organize your code into functions. Great job. 제어 흐름에 대한 이 수업을 완료한 것을 축하합니다. 조건문, 루프, 유용한 내장 함수, 코드에 … Read more

3-1-3-32. Quiz: List Comprehensions

Quiz: Extract First Names Use a list comprehension to create a new list first_names containing just the first names in names in lowercase. Quiz: Multiples of Three Use a list comprehension to create a list multiples_3 containing the first 20 multiples of 3. Quiz: Filter Names by Scores Use a list comprehension to create a list of names passed that only include those … Read more

3-1-3-30. Solution: Zip and Enumerate

Quiz Solution: Zip Coordinates Output: Notice here, the tuple was unpacked using * in the format method. This can help make your code cleaner! Quiz Solution: Zip Lists to a Dictionary Output: The order of elements in this output may vary since dictionaries are unordered. Quiz Solution: Unzip Tuples Output: Quiz Solution: Transpose with Zip Output: This is a … Read more