Loops (for, while) in python

Loops (for, while) in python

Loops in Python programming are typically used to repeat a program code logic statement or execute it a finite or unlimited number of times. Generally, for loop and while loop are used in Python programs to repeat a particular block of program code a certain number of times. For loop and while loop help you automatically repeat the repetition task in Python programs by repeating sequence data types such as list, tuple, dictionary, and string.

Loops (for while) in python

So let us understand for and while loop better in Python programming.

python for loop.

For loop in Python programs is used to repeat sequence data types (such as list, tuple, string or variable data range) or any repetition object. It checks and executes a block of code repeatedly for each item in a data sequence from the start element to the end element of a list, tuple, data range.

python for loop syntax.

for item in iterable:

# it execute the for loop block of code when condition is true

statement

statement

Here item in for loop is a declared data type program variable that processes and displays each value from the stored element sequence repeated in each for code repetition.

Remember that under Python for loop syntax, the program code is executed once for each item in the block repetition.

Python for loop example.

Execute a for loop on a list.

courses = [“python”, “java”, “c language”, “Javascript”,”html”]

for course in courses:

print(course)

Iterate over a string in a for loop.

for char in “Vcanhelpsu.com”:

print(char)

Iterate over a range of numbers in a for loop.

for integer in range(5, 15):

print(integer)

Python while loop.

The while loop program in Python repeats a block of source code as long as a specific condition is true. It then repeats it up to the limit specified. But sometimes when you do not know in advance how many times the while loop needs to be executed, then use the while loop.

python while loop syntax.

while condition:

# it Execute the block of while loop code till it repeats

statement

statement

Here, keep in mind that the while loop continues to execute in the program as long as the condition is true.

If at any point the while loop condition becomes false, the while loop execution stops automatically, and the while loop control moves to the next statement after the loop.

while loop Example.

integer = 5

while integer <= 15:

print(integer)

integer += 1

Loop Control Statements in Python Programs.

Python programming provides programmers with several while loop for loop controls that control or modify the default execution of the while loop for loop.

break statement – The break statement stops the current loop prematurely at a user defined break point.

continue statement – Where you use the continue statement after the break, it skips the remaining part of the current iteration, and continues with the repetition at the next loop element index location.

pass statement – Here it is used as a placeholder in the current program. If seen, the pass statement does nothing, and continues the loop body in the program.

Example of break statement in Python program.

courses = [“python”, “java”, “sql”,”html”,”javascript”]

for course in courses:

if course == “sql”:

break

print(course)

Using the continue statement.

for integer in range(5, 15):

if integer == 9:

continue

print(integer)

Using the pass statement.

for integer in range(5, 10).

pass # use pass statement to pass from current loop position

Python infinite loops.

Many times you do not terminate the condition properly in for loop or while loop. And it automatically becomes an infinite loop, (where the loop condition never becomes false) to avoid this, you should use the while loop carefully and correctly. So that your current program does not hang or the running Python software does not crash.

Python nested loops.

When you use loops within loops in Python programming, it is called nested loops. Or sometimes you can use for and while loops nested inside each other for more complex program repetitions.

for p in range(1, 7):

for q in range(1, 7):

print(p, q)

In Python programming, for loop and while loop are specifically used to repeat a particular data structure from start to end point in a series of repetitions. You can use for loop and while loop in Python programs as per your discretion.