Python For loop is used to execute a loop between all the items in a sequence. Like Python, we will loop through all the letters in the word, and print the letters one by one. For him:

```
for letter in 'Python':
print 'Current Letter :', letter
```

Here the letter is a variable. For each letter of “Python” we use a loop for each letter in ‘Python’. For the first time, Python’s P letter is assigned to the letter, for the letter in ‘Python’: within the line. Which is automatic async. Then we printed the letter. The second letter is executed while executing the second loop. When you see that there is no letter left in Python, the loop is done.

#### We can also loop in an array, for that:

```
friends = ['Rahim', 'Karim', 'Arif']
for friend in friends:
print 'Happy New Year:', friend
print 'all done!'
```

Friends are an array that contains the names of your friends. Let’s say you have three friends, and their names are Rahim, Karim, Arif

Assuming you are a lazy person. You have to write many times to greet everyone on the first day of Baishakh. You have intelligently written a program that will send an SMS to each friend.

Python for loop through SMS, iterates between the frames, and we write for friends in friend.

Rahim’s name will be here for the first time. Then will print Happy New Year: Rahim

will be Karim’s name for the second time. Then print Happy New Year: Karim

will be the third time a friend’s name is Arif. Then print Happy New Year: Arif

After that we have no more friends in the array. So get out of the for loop. And let you know, sending SMS to all is over! all done!

Now you create a huge array with all your friends’ names. Modify the program above, and see what output is available.

We can also write the program “I won’t do naughty anymore” with a while loop for:

```
for a in range(1,100):
print "ami r dustumi korbo na"
```

What to do, write for you 3 times for you “I won’t do naughty anymore”

**We have a list of prime numbers from 1 to 5, as below:**

`prime =[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]`

We want to print one by one. By running iterations with a for loop, we can print prime objects like this:

```
prime =[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
for number in prime:
print number
```

So what it will do is print one of the prime numbers above.

You may be wondering, how many primes are there from 1 to 5? As you have a list, you can count it one by one. But you are the programmer. Fooling like others is a problem. When you write a small program, it will tell you how many prime numbers are between 1 and 100. For him:

```
prime =[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
count = 0;
for number in prime:
count++
print 'Total Prime number in 1-100:, count
```

That way, no matter how large a list you have, you can easily calculate how many primes are in a list.

**Find out the list of prime numbers**

We already had a list of prime numbers. Now if you don’t have a list of prime numbers, what to do? Yes, then we will print the prime numbers. Because we now know programming, as we would print prime numbers between 3 and 5. For him:

```
for num in range(1,100):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
```

Here’s the range for the first num in range (1,100): We will calculate the prime number between the range of how many ranges. There are prime numbers between 1-1000 numbers, so we’ll figure out. You can see here with any other range like 500-50000 instead of 1-1000.

Prime numbers are greater than 1. And the prime number cannot be negative. So we first checked whether our number is greater than 1 if with num> 1: condition. If the condition is true, then the code inside it will be executed.

A number will be prime only if the number is not divisible by a number other than 1. If a number is divisible by another number, then the dividend ends, we can check with%, whether a number is divisible or not. If the divide goes, it will be 1. If you do not share, then there will be another number. To see if we divide, we divided the whole number by 2 to 5 numbers. With the second for loop. For example, to check if 1 is prime, we have divided up all the numbers by 2-5 to see if the share goes away. If divided, then not prime, and if not divided, then prime number.

Now if a number is divisible by a number smaller than that, then we don’t have to run the loop anymore, because if divided, it’s not a prime number anymore. So we get out of the second for loop, with a break.

##### And if you can’t divide a number by checking it with else, I’ll print that number.

If the above program is difficult to understand, then I can write a much easier program. Where a number is prime or not, it will check. For him:

```
num = 5
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
```

Above num = 5; It can be checked with another number instead. The program will tell you what number, if any, is prime. And if it’s not Prime then it’ll say.

Now, the above list of prime list and the code of the prime check will be seen when you see the same code. Here we have checked a number. And for the list, we have checked all the numbers in the range with for. If I saw Prime, then I printed it.

If we want the number we can get input from the console / USA. The program will first ask you to input a number:

```
num = int(input("Enter a number: "))
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
```

Now after input, the number will tell whether the prime is a prime or not. Now if the user input 5 or a negative number, he or she will not be able to prime. So we will check it at the beginning of our program:

```
num = int(input("Enter a number: "))
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
```

You can now modify the Prime List Extraction program so that it takes input from the user. One will be upper range one will be lower range. As in our Prime List Extraction program for num in range (1,100) here 1 is the lower range, 100 is the upper range.

#### **Nested for**

we can use one for loop inside and another for loop. Let’s look at an example. To print the pattern. For example:

*

* * * *

* * *

* * * *

* * * *

We want to get the above pattern as output. We will write a program for that. We can write this way using the for loop:

for i in range (0, 5):

for j in range (0, i + 1):

print (“*”, end = “”)

print (“\ r”)

Here, how many lines will be controlled by controlling the first for loop. We gave the range 5, so the first for loop will rotate 3 times and print five lines. The second for loop function is to print the stars. And it controls how many stars it will print on each line. For example one in the first line, two in the second line, three in the third line, etc.

Here is the new line with print (“\ r”). When the second loop is done, it will execute.

If we print the value of j instead of printing * in the above program, we will get a nice number pattern. Try it out You can also print I and j instead of j.

Note: More Info (Tech24InfoBD) (Python for loop)