The for loop
The for loop is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary.
The syntax of for loop in python is:

<sequence> is a collection of objectsβfor example, a list or tuple. The <statement(s)> in the loop body are denoted by indentation, as with all Python control structures, and are executed once for each item in <sequence>. The loop variable <var> takes on the value of the next element in <sequence> each time through the loop. Letβs code something to understand this syntax.

We have a string βPythonβ. We have to print every letter of this word βPythonβ. So we are taking a variable letter. There are 6 letters, P, y, t, h, o, n. So this loop will iterate for 6 times. When the letter is 1, P will be printed. Then the letter will become 2 and y will be printed, and so on, till the letter is 6 and n is printed.
Here, letter is a variable and we can use any name in place of letter.

We can also print the position of our words in string. In the above code, x is printing P, y, t, h, o, n. If we assign x = 1 and increment it with one after each iteration, then instead of printing P, it will print 1 and then x will become incremented to a value 2. It will then print 2 instead of y. This will iterate till the length of the string. Letβs see how to write it.

Letβs learn one more important function which will be used frequently. We see that everytime the output is being printed in a new line. What if we want to print the output in the same line? For this, we will use the end function as follows.

This line executes like this. After printing letter = P, this function ends the space with a single space denoted in (β β). Before this, after letter = P, there was an infinite space. Now itβs terminated with a single space. Another value of the letter will be printed after this single space and so on. Infact, we can place anything between this double quote and itβll be printed. Letβs place a comma and see.

I think itβs clear now. Using end is absolutely your choice as to how you want to display your answer. But as you see the above output, even after printing the final letter, there is still a comma. This is not desirable (even in terms of English punctuation rules also). How can we eliminate it? Well, for this specific question above we will learn one important property of string. Although we will learn and practice a lot about this in the chapter of Strings, here we will have a glimpse of it. Itβs called accessing a string value by itβs position called index position. The string βPythonβ has an index position starting from zero for P, one for y, two for t, and so on. We have to assign this string to some variable, say a. The way of accessing the specific word from the string is by writing the position in square brackets along with the variable name. Letβs see.

In python, we can also access our string values from right to left. The extreme right index position is -1. Then, -2, -3 and so on.

Ok. Now letβs return to our question. We access our last value of string by writing a[-1]. If this last value becomes equal to our letter also, i.e., a[-1] will be n here, and our last letter will be n. If a[-1] becomes equal to the letter, then we can end the space with a single space. In all other cases, we want commas in place of space. It means that, now, we need If condition, else condition and this if- else condition will execute till for loop executes. Letβs code it down.

Itβs working as expected. Moving on to another example
# WAP to print all factors of any number
So what is this question? The question asks for Writing A Program (thatβs WAP) to print or display all factors of any number. For example, the number is 10. So our output should be 1, 2, 5 and 10. The factors are those values which divide our given number completely with zero remainder. If we take a number 10, then, 1, 2, 5 and 10 will divide 10 completely with zero remainder. So what will be our approach?
- Take a number.
- Divide that number by 1, 2, 3β¦ and so on till that number itself.
- If the remainder is zero, then only those numbers will be our factors.
Now, letβs put it in our for loop syntax.
- We want a number. Letβs take a variable a and assign it a value, a = 10.
- We have to tell Python that the number is a numeric value. This is done by assigning the int() function. So, our user defined run-time input assignment will be written as, a = int(input(“Enter a number:”))
- Let me enter number 10. Now I want to divide my number with 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Why upto 10? A number can only be divided by up to itself for zero remainder. If I divide 10/11 or 10/12 or any greater number, the remainder will never be zero. Similarly, any number cannot be divisible by zero. So, we need to start from 1 and up to that number. Here comes the loop. As per syntax, itβll be written as: for i in range (1, a). Here, i is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, but, one at a time. We can read it as, i = 1 and it is in range from 1 to 10. Then, i = 2 and it is in range from 1 to 10. Then, i = 3 and it is in range from 1 to 10, and so on, till, i = 10 and it is in range from 1 to 10.
- After each check, we will divide our number 10 with each value of i. If the remainder becomes zero, we need this value of i. For this, we can write if a%i is equal to 0, then this i is our answer. We will not need other values of i. What is this %? As we saw in operators, % is used to find the remainder. Finally, to display, we will use print (i).
- Letβs code it down.

So, we have got everything as expected. But if you observe, 10/10 is also having zero remainder. But here 10 is not printed. Why? This is because, in a for loop when we write for i in range (1,a), it runs for a-1 iterations. If we want to run it for a iterations, we have to write it as (a + 1).

Also we must not forget the indentation rule. We know that i = 2 is in range of 1 to 10 and if this happens 10 has to be divided by this value of i. Similarly, we again know that i = 3 is in range of 1 to 10 and if this happens 10 has to be divided by this value of i. But our Python does not know this. Thatβs why we use 4 spaces or 1 tab to position our cursor inside a loop. It will read like this:
The value of i is 1. It lies in-range between 1 to 10. That’s for loop line. It’s ok then get inside and check for the next line. It’s the if statement. If a, that is, 10, is divisible by i, which at present is 1, (a%i ==0), print 1. Then it will again jump to the for loop line. Now I have the value of 2. 2 is in the range of 1 to 10. If 10%2==0, print 2. It jumps to the for loop again. Now it becomes 3. 3 is in the range of 1 to 10. 10%3 is not equal to 0. So 3 will not be printed. This looping cycle goes until i becomes 10.
Here, if statement executes when for loop executes. Similarly, print () statement executes when if statement executes. They are dependent. Python understands this when we write in the above manner; within each other. Since there are no brackets and semicolons in Python, alignment are the only way by which Python comes to know which line has to be executed next. This pattern of writing is called Indentation. But one thing to feel confident is that, when only Indentation Error occurs, your code is correct and only alignment is a problem.
How to use the end function here?

This line executes like this. After printing i = 1, end the space with a single space denoted in (β β). Before this, after i = 1, there was infinite space. Now itβs terminated with a single space. Another value of i will be printed after this single space and so on. Infact, we can place anything between this double quote and itβll be printed. Letβs place a comma and see.

Here also itβs printing a comma even after getting the last number. Since input is a number, we donβt need index positioning. We want that when this i becomes equal to the number itself, after that nothing should be printed. That is, in this case when i = 10 and a = 10, print 10 and end with a space. For all other cases, proceed as above. Letβs check it out.

So, we have understood in detail about how a loop works and how a for loop works. Like while- else, python provides a for- else loop as well. It means that, find some item in the iterable of the for loop, else if none was found, execute the statement written in the else condition.
Letβs try our next program. We have a password as input. If the user enters 3 incorrect passwords, then a message is displayed that 3 incorrect passwords were attempted. We will write this message in the else condition. The program will look like this.

The program I think is now clear to understand. Now Iβll give the correct input to check that only if condition executes or else also executes.

Only the if condition is executed. What if there is no break statement?

Since, there is no break condition, even after getting the correct password the for loop executed for two more times. I entered the wrong password twice. After executing for loop, the else condition was also executed.
Now, itβs practice time.
# WAP to print the table of given numbers?
I have to print a table of any number. Let the number be 10. Table looks like, 10 x 1 = 10, 10 x 2 = 20, 10 x 3 = 30, and so on. Till when? Till 10 x 10 = 100 (say). We can have any end value. So, 10 will not change, multiply sign x and equal to sign = do not change. The changing value is 1, then 2, then 3, and so on. Let it be i. The code looks like this.

# WAP to print the factorial of a given number?
As per mathematics, factorial of a number is multiplication of all digits with one another up to that number. For example, factorial of 5 will be written as 1x2x3x4x5 = 120. So, factorial will start with 1. Then it will be multiplied with 1 and the new value of the factorial will become 1. When the value of i = 2, factorial will become 1×2. For i = 3, factorial will become 1x2x3, and so on. Letβs code it down.

We will check it for another input 100.

If you are familiar with programming languages like C, C++ or Java, then you must know that for an input 100, the basic factorial program logic wonβt work. In Java, there is a separate class called BigInteger class to handle big values. In C, depending on the compiler, either an error or garbage value is generated. Try using it if you havenβt yet.
# WAP to print the fibonacci series of given numbers?
The Fibonacci series starts with two numbers: a, b. The third number generated will be the sum of (a + b). For example, if the number upto which the Fibonacci series will be printed is 5, and let the first two numbers are 1, 1. Then, the series is generated like this: 1, 1, 2(1+1), 3(1+2), 5(3+2). New number is generated by adding the previous two numbers. We will start with a, b and then will assign the value (a+b) to a and b to a till the loop iterates.

# WAP to print the Fibonacci series of given numbers using while loop?

# WAP to check if a given number is prime or not prime?
If a number is divisible by 1 and by the number itself only, then itβs called prime. Also 2 is the prime number. Therefore, our loop starts with 2 and will go till the number-1. This is because the number is divisible by itself. We donβt need to check the last number. If the number is divisible by any number lying between 2 and upper range, then it’s not a prime number.

Checking for another input, 12.

# WAP to print the sum of digits of any number?
Take the value of the integer and store it in a variable. Using a while loop, get each digit of the number and add the digits to a variable. Finally, print the sum of the digits of the number.

# WAP to check if the given number is an Armstrong Number?
A n-digit number in which the sum of each digit raised to power n is equal to the number itself, is called an Armstrong number or Narcissistic Number. For example, 153 has 3 digits, therefore, 1 raised to power 3 plus 5 raised to power 3 plus 3 raised to power 3 equal to 153. Hence, 153 is an Armstrong number.
General equation is,

Example of 153 is:


# WAP to print the sum of digits of any number until it becomes a single digit?

# WAP to print all prime numbers in a range?

# WAP to print the following pattern?

We need two loops here, one inside another. Inner loop controlled by j loop will print columns. Outer loop controlled by i loop will print rows. As per the pattern, given, number of rows, a = 4, i.e., i = 0, 1, 2, 3. When i = 0, its first row
j = 0, 1, 2, 3 (each column number) and range of j = 4, hence, 4 stars are printed in the 1st row and each column (j = 0, 1, 2, 3) of the first row. Next line is blank. Jumps to i loop.
When i = 1, its second row
j = 0, 1, 2, 3 (each column number) and range of j = 4, hence, 4 stars are printed in the 2nd row and each column (j = 0, 1, 2, 3) of the second row. Next line is blank. Jumps to i loop.
When i = 2, its third row
j = 0, 1, 2, 3 (each column number) and range of j = 4, hence, 4 stars are printed in the 3rd row and each column (j = 0, 1, 2, 3) of the third row. Next line is blank. Jumps to i loop.
When i = 3, its fourth row
j = 0, 1, 2, 3 (column) and range of j = 4, hence, 4 stars are printed in the 4th row and each column (j = 0, 1, 2, 3) of the fourth row. Next line is blank. Jumps to i loop.
When i = 4, it comes out of the loop, because the given number is 4. The code will be

# WAP to print the following pattern?

We need two loops here. One will print rows and another will print columns. As per the pattern, given, number of rows a = 4, i.e., i = 0, 1, 2, 3
When i = 0, its first row
j = 0, 1, 2, 3 (each column number) and range of j = 0 + 1, hence, 1 star is printed in the 1st row and first column of the first row. But all other columns of the first row will remain empty. Next line is blank. Jumps to i loop.
When i = 1, its second row
j = 0, 1, 2, 3 (each column number) and range of j = 1 + 1, hence, 2 stars are printed in the 2nd row and first two columns of the second row. Next two columns will remain blank. Next line is blank. Jumps to i loop.
When i = 2, its third row
j = 0, 1, 2, 3 (each column number) and range of j = 2 + 1, hence, 3 stars are printed in the 3rd row and first three columns of the 3rd row. Next line is blank. Jumps to i loop.
When i = 3, its fourth row
j = 0, 1, 2, 3 (each column number) and range of j = 3 + 1, hence, 4 stars are printed in the 4th row and all columns of the 4th row. Next line is blank. Jumps to i loop.
When i = 4, it comes out of the loop, because given number is 4.

# WAP to print the following pattern?

We need two loops here. One will print rows and another will print columns. As per the pattern, given, number of rows, a = 4, i.e., i = 0, 1, 2, 3
When i = 0, its first row
j = 0, 1, 2, 3 (each column number) and range of j = 4 – 0, hence, hence, 4 stars are printed in the 1st row and all columns of the 1st row. Next line is blank. Jumps to i loop.
When i = 1, its second row
j = 0, 1, 2, 3 (each column number) and range of j = 4 – 1, hence, 3 stars are printed in the 2nd row and first three columns of the 2nd row. Last column will be empty. Next line is blank. Jumps to i loop.
When i = 2, its third row
j = 0, 1, 2, 3 (each column number) and range of j = 4 – 2, hence, 2 stars are printed in the third row and first two columns of the 3rd row. Next line is blank. Jumps to i loop.
When i = 3, its fourth row
j = 0, 1, 2, 3 (each column number) and range of j = 4 – 3, hence, 1 star is printed in the 4th row and the 4th column. All other columns are empty. Next line is blank. Jumps to i loop.
When i = 4, it comes out of the loop because given number is 4.

# WAP to print the following pattern?

We need two loops here. One will print rows and another will print columns. As per the pattern, given, number of rows, a = 4, i.e., i = 1, 2, 3, 4, because i has range from 1 to 5. So itβll run from 1 to 4.
When i = 1, its first row
j = 0, 1, 2, 3 (each column number), and because the range of j is from 1 to 2, hence, value 1 is printed in the 1st column of the first row. All others columns will be empty. Next line is blank. Jumps to i loop.
When i = 2, its second row
j = 0, 1, 2, 3 (each column number) and because the range of j is from 1 to 3, hence, values 1 and 2 are printed in the 2nd row and first two columns of the second row. The next two columns will remain empty. Next line is blank. Jumps to i loop.
When i = 3, its third row
j = 0, 1, 2, 3 (each column number) and because the range of j is from 1 to 4, hence, values 1, 2 and 3 are printed in the first 3 columns of the third row. Jumps to i loop.
When i = 4, its fourth row
j = 0, 1, 2, 3 (each column number) and because the range of j is from 1 to 5, hence, values 1, 2, 3 and 4 are printed in the 4 columns of the 4th row. Then it jumps to the i loop.
When i = 5, it comes out of loop, because the given number is 4.

# WAP to print the following pattern?

Itβs similar to the logic above. The only difference will be that now I have to print the value of i instead of j.

So, coming back to the definition of python, if a programing language makes extensive use of the structured control flow constructs of selection (if/then/else) and repetition (while and for), with an aim to improve the clarity, quality, and development time of a computer program, then, it is called Structured Programming.
