From the course: Python Theory for Network Engineers

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Conditional logic: For and while loops

Conditional logic: For and while loops - Python Tutorial

From the course: Python Theory for Network Engineers

Conditional logic: For and while loops

- Let's talk about another powerful tool in Python, which is Loops. What is a Loop? A Loop is a block of code that executes many times. And typically we want to specify a certain condition for the block of code to run. And that condition could be a For Loop. A For Loop runs as many times as you specify or it could be repeated once for each of the item in a collection. This is typically used to iterate over the elements of a sequence, such as in a list or a tuple. The letter "I" in this example that we saw is just a place holder. That could be anything. In most cases, we want to make it explicit and match it to what we have. So, in the two For Loops that we saw, the "for I in range(10)" it basically repeats for all the numbers from zero to nine and "I" would just be a placeholder. So, the first time we run would be zero, and then one, and two, and three and four and so on, so forth. Or it could be repeated…

Contents