Discover the intricacies of using a for loop in Python, an essential tool for any coding professional. Art, a Python instructor, walks you through the process step-by-step, demonstrating how to automate greeting each of your friends individually.
Key Insights
- The video provides an in-depth tutorial on how to use a for loop in Python.
- Art demonstrates how to use a for loop to iterate through a list, in this case, greeting a list of friends individually.
- The for loop in Python is referred to as a definite loop as the number of times it will run is known beforehand.
- To use a for loop in Python, it's necessary to start with the keyword 'for', followed by a variable name.
- The loop then goes through a sequence of items, assigning each to the defined variable, in this case, 'name'.
- In future videos, Art will cover the use of a while loop and the differences between a for loop and a while loop.
In this video, we're going to look at how to use a For Loop in Python
Video Transcription
Hi, my name is Art and I teach Python at Noble Desktop. In this video, I'm going to explain to you how to use a for loop in Python. You probably heard that there are different types of loops in Python. There is a for loop, which we call a definite loop because we know how many times it will run. Then there is a while loop, which is a different type of loop and it depends on the condition.
Here, I'm going to show you how to use a for loop. It's really simple. Suppose you want to say hello to all of your friends. Feel free to use your own friends and let's say I have a list container with many names: John, Mark, and Mary. So suppose I want to say hello to Mark, hello to John, and hello to Mary. How would I do that?
I need to iterate, I need to go through this list and grab value by value, so that's why we need to use a for loop. To use a for loop, you always need to begin with the keyword 'for'. Then we have to come up with some kind of variable name, let's say 'name'. Then there is a colon and indentation. Here, I'm going to print 'hello' and comma 'name'.
If I run this, you see I'm getting 'hello Mark', 'hello Mary', 'hello John'. That's what the for loop does. It kinda goes through that sequence of items, grabs an item and assign it to 'name'. So under the hood it works like this: 'name' is being defined as 'John', 'Mark', and 'Mary'.
That's it! Watch my next video and I'll show you how to use a while loop and I'll explain you the difference between a definite for loop and a while loop.