For Loop

For loop

- For loop is used to repeat a list, set, tuple, dictionary, or string.

- For loop written by for keyword.


For loop on “chirag” string:

- Return Output as Column(Vertically).

- Why do we print x because x gets all the values of a string. (In the below code)

- We can add another character or name instead of x.
 

Output:

       


For loop on cheems list:

- Return Output as Column(Vertically).



For loop break statement:

- Break statement is used to stop the loop where we add the break condition.

- Break statement written by break keyword.




- In the below code, “dogelina” is excluded in the output because we add the break condition at "dogelina" in the if statement before print.

- Syntax: if c == "Value Name":
                    break






- In the below code, “dogebhai” is included in the output because we print the loop before the break condition at "dogebhai" in the if statement.



For loop continue statement:

- Continue statement is used to exclude the specific value and continue the loop with the next value.

- Continue statement written by continue keyword.




- In the below code, “dogelina” is excluded in the output because we add the continue condition at "dogelina" in the if statement before print.

Syntax: if c == "Value Name":
                    continue



For loop range() function:

- For loop range() function is used to print the numbers with a specific number of times.

- The Range of Numbers starts with 0 (by default), increments with 1 (by default), and ends with a specific number.

Syntax: for c in range(a, b, c):
                    print(c)

- a is the starting number.
- b is the ending number.
- c is the increment number.




- Return 0 to 4 numbers, 5 is excluded. (In the below code)

- Starts with 0 (by default), that's why 5 is the ending number(In the below code)

- Increment by 1 (by default).






- Return 1 to 4 numbers, 5 is excluded(In the below code)

- 1 is the starting number and 5 is the ending number(In the below code)

- Increment by 1 (by default).






- Return 1 to 9 numbers, 10 is excluded(In the below code)

1 is the starting number10 is the ending number, and 3 is the increment number(In the below code)

- Increment by 3.



Nested for loop:

- Nested for loop means a loop inside a loop.

- c is the outer loop and d is the inner loop. (In the below code)



Important-
- If we print the outer loop(c loop) then the inner loop range is executed for every single value of the outer loop.

- If we print the inner loop(d loop) then the outer loop range is executed for all values of the inner loop.

Output:

(c loop) 
 ๐Ÿ ‡ (d loop)
 ๐Ÿ ‡  ๐Ÿ ‡


end=’ ’ statement:

- The end=’ ’ condition adds spaces or characters between executed values.

- The end=’ ’ condition 
Returns the Output as Row(Horizontally).

- Without end=' ' condition the Output is Column(Vertically).




- The end=' ' condition with space: (In the below code)


Output:




- The end=' ' condition with character: (In the below code)

Output:





- The end=' ' condition in NESTED LOOP with space: (In the below code)

Output:





- The end=' ' condition in NESTED LOOP with character: (In the below code)


Output:

Comments