Python Advanced For Loop With Example

Basically, For Loop reads input value and stores in a variable. So For Loop in Python needs two arguments. The two arguments are VARIABLE and IN. So far so good. The next part of the article and examples well explained how it works and how to get the index for each input supplied.

for loops in python

In Python, for loop list and count are related to the same logic. In the below example, I have taken one array and I counted using for loop, the number of elements in the input list.

For loop Syntax

for count in[1, 2, 3]: 

The first step, How the above syntax works are what I want to share with you. The 'in' represents input array for Python for loop. That means the for loop works 3 times in Python. It displays only 3 times. The third step is just to give 'yes', which will multiply with 'count' for 3 times.

This is the whole story of for loop in Python. In the second step, print just displays values of input that stored in the for loop variable of 'count'.

The 'count' gets values one by one from for loop input array. When you used the Shell to enter a loop, there was a reason that the interpreter waited to respond until after you entered an empty line: The interpreter did not know how long the loop block was going to be! The empty line is a signal to the interpreter that you are done with the loop block.

The index in For loop 

A small example is given to get index and value from for loop.
#Get index using for loop
#You will get index and value
mylist=[1, 2, 3]: 
for idx, val in enumerate(mylist):
    print(idx, val)

Video Tutorial

I have created a small video so that you can play quickly and check how for loop works in Python.
Also, read


Popular Posts

Hyperledger Fabric: 20 Real Interview Questions

7 AWS Interview Questions asked in Infosys, TCS

How to Fix Python Syntax Errors Quickly

Python 'getsizeof' Command the Real Purpose

Blue Prism complete tutorials download now

Python Dictionary Vs List With Examples

How to Use the ps Command in Linux

Vi Editor to Quit use Esc and Colon

5 HBase Vs. RDBMS Top Functional Differences

The Growth of Machine Learning till TensorFlow