How to use for loop in python 3

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,

  1. The variable
  2. The repeating code
  3. For keyword
  4. In keyword
  5. Range()

Syntax of for loop in Python 3:-

  1. for val in sequence :

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.

  1. for iterator_var in sequence:

 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:-

How to use for loop in python 3
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

For Loop Python Example:-

  1. for loop:-

trees= ["apple”,”cherry”,”banana”]
for x in trees:
print(x)
Output:-
apple
cherry
banana

  1. Break statement:-

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

3. Looping through string:-

The string is sequence of characters also called as the iterable objects.
The following example shows the looping of string,
Example:-
for x in “apple”:
print(x)
Output:-
   a
p
p
l
e
4. Continue statement:-
This statement will be used to stop the current iteration of loop and continue the next iteration.
Example:-
birds= [“parrot”,”butterfly”,”cock”]
for x in fruits:
if x==”butterfly”:
continue
print(x)
Output:-
parrot
cock

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

6. Nested For loop in Python 3 :-

The nested loop is the loop within the loop and inner loop will be executed one time for each iteration of outer loop
Example:-
adj= [“tasty”, “red”]
fruits= [“cherry”,”apple”]
for x in adj:
for y in fruits:
print(x, y)

Output:-
tasty cherry
tasty apple
red cherry
red apple

7. Range function:-

This loop will set code of specified number and used the range function ().
The range function is written as range ().
Also returns sequence of numbers and increment by 1.and ends at specified number.
Example:-
For x in range (5):
Print (x)
Output:-
1
2
3
4
The terms used in the for loop are given as follows and explained with the meaning.


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.

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India