Python Lesson 4 ( How to use loops in python For/While) # Python Lesson 4 ( How to use loops in python For/While)

Thank you for joining with my forth tutorial about Python programming language. During my last tutorial, I have given the proper instructions about how to use conditions such as if and else. Now we know how to use conditions.If you know the basics of python, You can have a look at my advanced python tutorials about flask or tensorflow. Today we are going to talk about two different kinds of loops we can use with Python language. First of all we are going to look into “for” loop and we are going to look into “while” loop later. In here I am not going to tell you what is a loop and why we need loops. But if you go through with my code examples you will understand where to use those loops and how to use them properly.

As an example, we are going to loop through each and every letter of a word and print them separately. Please have a look at the following example. In this example I am going to loop through each and every letter of my name and print them separately.

```for letter in 'Pasindu':
print 'Letters :', letter```

As you can see here below, Python will give you each and every letter in separate row. That means , the code will read the each and every letter of the name and print it. The loop will end after going through with each and every letter the name.

Lets have a look at another example. Lets say you want to loop specific rounds.

```for i in range(5):
print(i)```

As you can see above, this loop will go through five times. Below will be the output. As you can see, We know how many times the loop should loop before the loop starts. In the first example, we looped 7 times. Because “Pasindu” has 7 letters. In the second example, loop went 5 times, because we indicated range as 5.

Now lets have a look at the “While” loop. Using while loop, the loop will keep looping until some condition gets completed. Lets have a look at this example.

```i = 1
while i <= 10:
print(i)
i += 1```

As you can see here, I have initiated a variable and assigned a value. So inside the while loop, that loop will run until the condition complete. After the print of the variable, I’m incrementing  the variable value by one. Lets have a look at the answer now. 