Python for loop with example

Python for loop- Here we will discuss the python for loop, and how we will write that in python for loop syntax and code.python for loop is one of the basic step, if you are learning python basics.

Before Proceed, lets first understand what is for loop, why we use loop and what are the advantages of using for loop.

 

For Loop

Before learning for loop in python, In general term, if we say, For loop is iteration of some code which execute repeatedly with some specific condition. For example, if we want to print the no. from 1 to 10, we can print this by using for loop, where we know the condition that we have to print only no. from 1 to 10.

General flow chart of for loop is

Python for loop

Conclusion from the above flow chart diagram

  • For loop needs some initialize variable to start with.
  • After that it will check, if particular condition is satisfied or not.
  • If yes, then it will execute the particular statements inside for loop.
  • It will continue to execute the steps, till condition is satisfied.
  • If condition is not satisfied, it will end the for loop

So, in our above example flowchart of for loop will be like:

For (initialize variable (1); condition (variable is <=10; increment (variable++)) {

READ  Python Continue

Print variable;

}

For loop in Python 

 

Python for loop Syntax

Before hands on python for loop program, let’s see the syntax of python for loop.

for some_variable in dataset:

print (some_variable)

  • Python for loop will start from keyword for
  • some_variable is any temporary variable, in which we will fetch the value from range, one by one.
  • dataset is some data; on which we are going to apply for loop.
  • at the end, we will print some_variable value.

Python for loop examples

So if we take same example of printing no. from 1 to 10, then we will write python for loop this this

for i in range(10):
print (i)

it will print following output on your console:

0

1

2

3

4

5

6

7

8

9

 

Some observation from the python for loop output

  • If you noticed, it has printed values from 0 to 9, actually we did not initialize the variable i with 1. By default, it started with 0
  • It only prints till 9, but our requirement was to print the value from 1to 10

As we have used range keyword in our example, so it will loop the data in between the range, it will not consider 10, as 10 is not in range. This code give instruction to python that print all no. between 10. Range function will always print 1 less than its mentioned. We will study range function in our further posts.

So if we want to print values from 1 to 10, then our code will be like this

for i in range(1,11):
print (i)

Output will be

1

2

3

4

5

6

7

8

9

10

here we have given instructions to range function that print all the values starting with 1 and less than 11. Here our range function is like

READ  local vs global vs non local variable in Python

range(start, stop)

let’s take some more examples

example 2:

if we will want to run the loop with given set of data say some numbers, we can do it by following way

data= (1,4,5,7)

for i in data:

print (i)

It will print

1

4

5

7

In this example, we have data with some values 1,4,5,7., so we print items of data one by one.

 

Example 3- Python for loop example with Strings

Here we will see how to apply python for loop in Strings.

pythonData=["Python","for","loop"]

for i in pythonData:

print (i)

Output

Python

for

loop

In this example, we took one python list, if you don’t know about python lists, then don’t worry, in coming posts, we will study python lists in details. So here pythonData is list, with for python for loop, we have printed values inside pythonData list.

Please follow and like us:

3 Comments to “Python for loop with example”

  1. […] iterator could be list, set, tuple etc. Below we will see Python for loop example. See this post Python for loop  for detail about for […]

  2. […] continue helps to move the for loop one step […]

  3. […] that, we have used Python for loop which will run till the number which user inputs and will call the fibonacci […]

Leave a Reply