for loops- Python

So, in for loops if a series contains a list of expressions, it is previewed. Then, the first thing in a row is given the variable iterating_var. Next, a statement block is executed. Each element on the index is assigned to a variable, and the statement block (s) are used throughout the whole series.

Python for loops syntax

Python for Loops Statements

Example of a for Loop

for l in 'Python':
   print (l)

Output

P
y
t
h
o
n

Use of else Statement with For Loop

So, the else statement is there to use anything while the program dissatisfies the loop the else is executed in the program.

Python supports having another statement associated with the loop statement. If one statement is used with a loop, another statement is used when the loop has finished repeating the list. The following example shows a combination of another statement with a statement looking for key numbers from 10 to 20.

Sequence of For loops

In the sequence section of the loop, there can be many things. Like there can be range functions, strings, any data structures(lists, tuples, etc).

For example

A for loop with a list in sequence.

C= ["Red", "Blue", "Black"]
for x in C:
  print(x)

A for loop with a string in sequence.

for x in "Simitech":
  print(x)

Also, in the “for” loops, you can use all the loop control statements.

Also, visit Simiservice.com

Previous post

Rate this post