What is the use of for in Python?

In the previous section, we have discussed the if statement. In this section we will learn how to use a for loop in python.

We have been discussing the control flow in python.

However, the for statement in python differs from other for statements used in languages like C, or Pascal.

If you come from a different programming language background, you will notice the difference.

In C or Pascal, what did we see?

We iterate over an arithmetic progression of numbers. 

Let’s see a similar example used in Dart programming language.

void main() {
  for (int i = 0; i < 5; i++) {
    print('hello ${i + 1}');
  }
}
// output:
hello 1
hello 2
hello 3
hello 4
hello 5

In such examples we can define each iteration step and even halt the condition.

But python’s for statement iterates over the items of any sequence. 

That might be a list, a String or a Map.

For example, we consider a list of strings that progress in length.

small_to_big_words = ['Pat', 'Moving', 'Humongous', 'Confabulate']

for word in small_to_big_words:
  print(word, len(word))

# output:
Pat 3
Moving 6
Humongous 9
Confabulate 11

The for statement in python iterates over the items in the order that they appear in the sequence. 

As a result, we can measure the length of the words at the same time.

The same way, we can loop over the copy of a collection. 

This collection refers to a map with the key-value pair. 

The key refers to the name of the learners and value refers to the result. 

Now we can easily create a new collection of learners who have not passed the examinations.

learners = {'John': 'pass', 'Trevis': 'fail', 'Emily': 'pass', 'Json': 'pass', 'Sanjib': 'fail'}

for learner in learners:
  print('Name of all learners:', learner)

print('****************')

failed_learners = {}

for learner, status in learners.items():
  if status == 'fail':
    failed_learners[learner] = status

print('Name of failed learners:', failed_learners)


print('****************')

# output:
Name of all learners: John
Name of all learners: Trevis
Name of all learners: Emily
Name of all learners: Json
Name of all learners: Sanjib
****************
Name of failed learners: {'Trevis': 'fail', 'Sanjib': 'fail'}

On the other hand, while iterating the collection, we can match the status and delete learners who have not passed.

In addition, we can create a new list and equate that list to the rest of the learners who have passed.

for learner, status in learners.copy().items():
  if status == 'fail':
    del learners[learner]

for passed_learner in learners:
  print('Name of passed learners: ', passed_learner)


print('****************')

passed_learners = {}

for learner, status in learners.items():
  if status == 'pass':
    passed_learners[learner] = status

print('Name of passed learners:', passed_learners)

# output:
Name of passed learners:  John
Name of passed learners:  Emily
Name of passed learners:  Json
****************
Name of passed learners: {'John': 'pass', 'Emily': 'pass', 'Json': 'pass'}

In short, we can use the for statement in many ways in python.

However, we have not finished discussing this topic yet.

So stay tuned.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Flutter, Dart and Algorithm

Twitter


Posted

in

,

by

Comments

One response to “What is the use of for in Python?”

  1. […] Let’s see a simple code sample where we have used a for loop to iterate over a sequence. […]

Leave a Reply