In this tutorial, we are going to discuss How to use for loop in python 3. We will give a brief description of for loop by the syntax, flowchart, example and use of the for loops. The python language has the for loop used in a programming language.
Definition of for loop :-
The programmers use the piece of code and repeat it n times.
The loop is used for the sequential traversal and the list of string, array, and lists.
In the python programming there is no c type for loop there is a “for in” loop which is same as for each loop.
The python programming language is used for iterating by the tuple, list, string, and dictionary.
The set of programs can be executed with a set of statements and for each item, etc.
If the block of codes is executed then for loop is used and repeated at fix number of time.
The block of code is executed and also the condition is checked.
This for loop is a control flow statement used in the python programming language.
The syntax and programs are clear and support seven sequence data types as, list, tuples, xrange, bytearray, unicode string.
The for loop in the python programming language is the same as the while loop and used for several times.
The for loop is the programming concept used for the implementation and the execution of the code.
The specific number and time is defined for the loop and sequences are given.
This code is executed several times again and again by sequence.
For creating for loop the components are as follows,
Body of for
The val is the variable which takes the item inside a sequence of iteration.
The loop will continue till we reach the last item in a sequence and the body of the loop is separated from code using indentation.
Statements(s)
The for loop will iterate sequence of numbers using the range and xrange functions.
The xrange and the range will have difference as the range function will return a new list and xrange will return an iterator which is important.
Flowchart:-
The flowchart is of the for loop, in this we first check whether or not we have reach the last item in sequence.
If it is not then the body of for loop is executed and again flow of the program will jump out from the loop
trees= ["apple”,”cherry”,”banana”]
for x in trees:
print(x)
Output:-
apple
cherry
banana
This statement will stop the loop before looping through items.
Example:-
trees= [“apple”,”cherry”,”banana”]
for x in trees:
print(x)
if x==”banana”
break
Output:-
Apple
Cherry
Banana
5. Else in for loop:-
This keyword is used in the for loop that will specify the code which is to be executed after loop execution.
Example:-
The printing of numbers from 1 to 5 and last message.
for x in range (5)
else:
print(“Finished”)
Output:-
1
2
3
4
5
Finished
Output:-
tasty cherry
tasty apple
red cherry
red apple
Term |
Meaning |
Iter() |
This build-in function us used to obtain the iterator from iterable. |
Iterator() |
The object will produce items or values from iterables. |
Iterable() |
The adjective used to describe the given object |
Iteration() |
The given looping process through the object in the collection is called iteration. |