.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/basics/for_loops.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_basics_for_loops.py: ================= 1.10 for loops ================= This lesson introduces ``for`` loops in python. .. important:: This lesson is still under development. .. GENERATED FROM PYTHON SOURCE LINES 13-17 Just like while loops, ``for`` loops allow an instruction to be executed a certain number of times. How many times? It depends upon iterator. As per wikipedia 11 people have received Nishan-e-Haider [1]_ award in Pakistan. Let's say we want to iterate over this list. .. GENERATED FROM PYTHON SOURCE LINES 17-26 .. code-block:: default NH_receivers = ['Saif Ali Janjua', 'Muhammad Sarwar', 'Tufail Muhammad', 'Aziz Bhatti', 'Rashid Minhas', 'Muhammad Akran', 'Shabbir Sharif', 'Muhammad Husain Janjua', 'Muhammad Mahfuz', 'Sher Khan', 'Lalak Jan'] for shaheed in NH_receivers: print(shaheed) .. rst-class:: sphx-glr-script-out .. code-block:: none Saif Ali Janjua Muhammad Sarwar Tufail Muhammad Aziz Bhatti Rashid Minhas Muhammad Akran Shabbir Sharif Muhammad Husain Janjua Muhammad Mahfuz Sher Khan Lalak Jan .. GENERATED FROM PYTHON SOURCE LINES 27-36 In above example, a list is acting as an iterator. In fact it can be a tuple, string or any other sequence. The basic syntax of for loop in python is: .. code-block:: python for variable in sequence: do something The `variable` in above syntax is assigned a new new value from `sequence` upon every iteration. .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: default for podcast in ("Hujjat", "The east is the podcast", "Philosophise this"): print("I am listening to: ", podcast) .. rst-class:: sphx-glr-script-out .. code-block:: none I am listening to: Hujjat I am listening to: The east is the podcast I am listening to: Philosophise this .. GENERATED FROM PYTHON SOURCE LINES 43-44 The variable `podcast` is assigned a new value from the sequence (tuple in this case). .. GENERATED FROM PYTHON SOURCE LINES 46-48 **Question:** Print first 5 natural numbers using for loop. .. GENERATED FROM PYTHON SOURCE LINES 51-53 We can also run a for loop on items of a dictionary. If we want to iterate over both keys and values of a dictions, we would do as below .. GENERATED FROM PYTHON SOURCE LINES 53-66 .. code-block:: default scholars = { "Baqir al sadr": 1980, "Murtaza Mutahri": 1979, "Allama Iqbal": 1938, "Jamal ul din Afghani": 1897, "Ali Shariati": 1977, "Moh Husain Tabatabai": 1981 } for scholar, date_of_death in scholars.items(): print(scholar, "died in ", date_of_death) .. rst-class:: sphx-glr-script-out .. code-block:: none Baqir al sadr died in 1980 Murtaza Mutahri died in 1979 Allama Iqbal died in 1938 Jamal ul din Afghani died in 1897 Ali Shariati died in 1977 Moh Husain Tabatabai died in 1981 .. GENERATED FROM PYTHON SOURCE LINES 67-77 **Question:** .. code-block:: python for a in range(0, 10): if a>5: a = a + 100 print(a) What will be the output of above code? .. GENERATED FROM PYTHON SOURCE LINES 79-94 `for` with else --------------- Just like `while` loops, the code under else gets executed if everything goes well within `for` loop. If let's say, we are running for loop to search an item in the iterator, and when we find the item, there is not point in continuing the `for` loop further. Hence we `break` out of for loop. In such a case code under `else` will not be executed. .. code-block:: python for variable in sequence: do something else: do at last .. GENERATED FROM PYTHON SOURCE LINES 96-104 .. code-block:: default for shaheed in NH_receivers: if shaheed == 'Rashid Minhas': print("Person from air force found") break else: print('Search completed') .. rst-class:: sphx-glr-script-out .. code-block:: none Person from air force found .. GENERATED FROM PYTHON SOURCE LINES 105-106 We can create a simple sequence using ``range`` function .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: default range(5) .. rst-class:: sphx-glr-script-out .. code-block:: none range(0, 5) .. GENERATED FROM PYTHON SOURCE LINES 112-116 .. code-block:: default for i in range(5): print(i) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 1 2 3 4 .. GENERATED FROM PYTHON SOURCE LINES 117-118 We can alo iterate over a list using range. .. GENERATED FROM PYTHON SOURCE LINES 120-124 .. code-block:: default for i in range(5): print(NH_receivers[i]) .. rst-class:: sphx-glr-script-out .. code-block:: none Saif Ali Janjua Muhammad Sarwar Tufail Muhammad Aziz Bhatti Rashid Minhas .. GENERATED FROM PYTHON SOURCE LINES 125-127 However, the above example is not a very clever approach as we could have simple done ``for i in NH_receivers``. A more useful example would be: .. GENERATED FROM PYTHON SOURCE LINES 127-131 .. code-block:: default for i in range(2, 5): print(NH_receivers[i]) .. rst-class:: sphx-glr-script-out .. code-block:: none Tufail Muhammad Aziz Bhatti Rashid Minhas .. GENERATED FROM PYTHON SOURCE LINES 132-134 We can also include step argument in range, which decides how big the jump/step we want to have in our iterator. In this way we can skip every nth value in a squence/iterator. .. GENERATED FROM PYTHON SOURCE LINES 136-140 .. code-block:: default for i in range(2, 8, 2): print(NH_receivers[i]) .. rst-class:: sphx-glr-script-out .. code-block:: none Tufail Muhammad Rashid Minhas Shabbir Sharif .. GENERATED FROM PYTHON SOURCE LINES 141-142 We can also go backwards in the iterator .. GENERATED FROM PYTHON SOURCE LINES 144-148 .. code-block:: default for i in range(8, 2, -1): print(NH_receivers[i]) .. rst-class:: sphx-glr-script-out .. code-block:: none Muhammad Mahfuz Muhammad Husain Janjua Shabbir Sharif Muhammad Akran Rashid Minhas Aziz Bhatti .. GENERATED FROM PYTHON SOURCE LINES 149-153 .. code-block:: default for i in range(8, 0, -2): print(NH_receivers[i]) .. rst-class:: sphx-glr-script-out .. code-block:: none Muhammad Mahfuz Shabbir Sharif Rashid Minhas Tufail Muhammad .. GENERATED FROM PYTHON SOURCE LINES 154-156 nested for loops ----------------- .. GENERATED FROM PYTHON SOURCE LINES 156-161 .. code-block:: default for name in NH_receivers: for char in name: print(char) .. rst-class:: sphx-glr-script-out .. code-block:: none S a i f A l i J a n j u a M u h a m m a d S a r w a r T u f a i l M u h a m m a d A z i z B h a t t i R a s h i d M i n h a s M u h a m m a d A k r a n S h a b b i r S h a r i f M u h a m m a d H u s a i n J a n j u a M u h a m m a d M a h f u z S h e r K h a n L a l a k J a n .. GENERATED FROM PYTHON SOURCE LINES 162-165 accessing index ---------------- If we want to access index itself, we can do this by using ``enumerate``. .. GENERATED FROM PYTHON SOURCE LINES 167-171 .. code-block:: default for index, item in enumerate(NH_receivers, start=0): # default value of start is zero. print(item, ' is Nishan - Haider receiver number ', index) .. rst-class:: sphx-glr-script-out .. code-block:: none Saif Ali Janjua is Nishan - Haider receiver number 0 Muhammad Sarwar is Nishan - Haider receiver number 1 Tufail Muhammad is Nishan - Haider receiver number 2 Aziz Bhatti is Nishan - Haider receiver number 3 Rashid Minhas is Nishan - Haider receiver number 4 Muhammad Akran is Nishan - Haider receiver number 5 Shabbir Sharif is Nishan - Haider receiver number 6 Muhammad Husain Janjua is Nishan - Haider receiver number 7 Muhammad Mahfuz is Nishan - Haider receiver number 8 Sher Khan is Nishan - Haider receiver number 9 Lalak Jan is Nishan - Haider receiver number 10 .. GENERATED FROM PYTHON SOURCE LINES 172-173 which is equivalent to .. GENERATED FROM PYTHON SOURCE LINES 175-181 .. code-block:: default index = 0 for item in NH_receivers: print(item, ' is Nishan - Haider receiver number ', index) index += 1 .. rst-class:: sphx-glr-script-out .. code-block:: none Saif Ali Janjua is Nishan - Haider receiver number 0 Muhammad Sarwar is Nishan - Haider receiver number 1 Tufail Muhammad is Nishan - Haider receiver number 2 Aziz Bhatti is Nishan - Haider receiver number 3 Rashid Minhas is Nishan - Haider receiver number 4 Muhammad Akran is Nishan - Haider receiver number 5 Shabbir Sharif is Nishan - Haider receiver number 6 Muhammad Husain Janjua is Nishan - Haider receiver number 7 Muhammad Mahfuz is Nishan - Haider receiver number 8 Sher Khan is Nishan - Haider receiver number 9 Lalak Jan is Nishan - Haider receiver number 10 .. GENERATED FROM PYTHON SOURCE LINES 182-183 This is another way to keep track that how many times the loop has been executed. .. GENERATED FROM PYTHON SOURCE LINES 185-192 **Question:** .. code-block:: python for i in enumerate(range(5)): pass print(type(i)) .. GENERATED FROM PYTHON SOURCE LINES 192-195 .. code-block:: default # What will be the output of above code? .. GENERATED FROM PYTHON SOURCE LINES 196-200 Iterating over more than one sequences -------------------------------------- If we want to iterate over more than one sequences, we can do this using built-in function ``zip``. .. GENERATED FROM PYTHON SOURCE LINES 200-208 .. code-block:: default scholars = ['Baqir al sadr', 'Murtaza Mutahri', 'Allama Iqbal', 'Jamal ul din Afghani', 'Ali Shariati', 'Moh Husain Tabatabai'] date_of_death = [1980, 1979, 1938, 1897, 1977, 1981] for scholar, dod in zip(scholars, date_of_death): print(scholar, ' died in year ', dod) .. rst-class:: sphx-glr-script-out .. code-block:: none Baqir al sadr died in year 1980 Murtaza Mutahri died in year 1979 Allama Iqbal died in year 1938 Jamal ul din Afghani died in year 1897 Ali Shariati died in year 1977 Moh Husain Tabatabai died in year 1981 .. GENERATED FROM PYTHON SOURCE LINES 209-214 .. code-block:: default date_of_birth = [1935, 1919, 1877, 1838, 1933, 1904] for scholar, dod, dob in zip(scholars, date_of_death, date_of_birth): print(scholar, ' was born in ', dob, ' and died in year ', dod) .. rst-class:: sphx-glr-script-out .. code-block:: none Baqir al sadr was born in 1935 and died in year 1980 Murtaza Mutahri was born in 1919 and died in year 1979 Allama Iqbal was born in 1877 and died in year 1938 Jamal ul din Afghani was born in 1838 and died in year 1897 Ali Shariati was born in 1933 and died in year 1977 Moh Husain Tabatabai was born in 1904 and died in year 1981 .. GENERATED FROM PYTHON SOURCE LINES 215-216 But what if lengths of lists are not equal? .. GENERATED FROM PYTHON SOURCE LINES 218-228 .. code-block:: default scholars = ['Baqir al sadr', 'Murtaza Mutahri', 'Allama Iqbal', 'Jamal ul din Afghani', 'Ali Shariati', 'Moh Husain Tabatabai'] date_of_death = [1980, 1979, 1938, 1897, 1977, 1981, 1989] print(len(scholars), len(date_of_death)) for scholar, dod in zip(scholars, date_of_death): print(scholar, ' died in year ', dod) .. rst-class:: sphx-glr-script-out .. code-block:: none 6 7 Baqir al sadr died in year 1980 Murtaza Mutahri died in year 1979 Allama Iqbal died in year 1938 Jamal ul din Afghani died in year 1897 Ali Shariati died in year 1977 Moh Husain Tabatabai died in year 1981 .. GENERATED FROM PYTHON SOURCE LINES 229-232 Simple ``zip`` will iterate over the point when all lists are equal and ignore if any sequence is larger than the others. If we want to iterate until the longest sequence, we have to use ``zip_longest`` from ``itertools`` .. GENERATED FROM PYTHON SOURCE LINES 234-244 .. code-block:: default from itertools import zip_longest scholars = ['Baqir al sadr', 'Murtaza Mutahri', 'Allama Iqbal', 'Jamal ul din Afghani', 'Ali Shariati', 'Moh Husain Tabatabai'] date_of_death = [1980, 1979, 1938, 1897, 1977, 1981, 1989] for scholar, dod in zip_longest(scholars, date_of_death): print('name: ', scholar, ' date of death: ', dod) .. rst-class:: sphx-glr-script-out .. code-block:: none name: Baqir al sadr date of death: 1980 name: Murtaza Mutahri date of death: 1979 name: Allama Iqbal date of death: 1938 name: Jamal ul din Afghani date of death: 1897 name: Ali Shariati date of death: 1977 name: Moh Husain Tabatabai date of death: 1981 name: None date of death: 1989 .. GENERATED FROM PYTHON SOURCE LINES 245-248 Notice the last printed line. If we want to access the previous and next value during iteration, we must start from 1 and end before last item in order to print correct values .. GENERATED FROM PYTHON SOURCE LINES 248-252 .. code-block:: default for i in range(1, len(NH_receivers) - 1): print(NH_receivers[i], ' came before ', NH_receivers[i + 1], ' and after ', NH_receivers[i - 1]) .. rst-class:: sphx-glr-script-out .. code-block:: none Muhammad Sarwar came before Tufail Muhammad and after Saif Ali Janjua Tufail Muhammad came before Aziz Bhatti and after Muhammad Sarwar Aziz Bhatti came before Rashid Minhas and after Tufail Muhammad Rashid Minhas came before Muhammad Akran and after Aziz Bhatti Muhammad Akran came before Shabbir Sharif and after Rashid Minhas Shabbir Sharif came before Muhammad Husain Janjua and after Muhammad Akran Muhammad Husain Janjua came before Muhammad Mahfuz and after Shabbir Sharif Muhammad Mahfuz came before Sher Khan and after Muhammad Husain Janjua Sher Khan came before Lalak Jan and after Muhammad Mahfuz .. GENERATED FROM PYTHON SOURCE LINES 253-260 We can change loop variable inside the loop. However, this is not a good practice and is prone to bugs. Let's say we want to iterate over sequence from 0 to 10 i.e `0,1,2,3,4,5,6,7,8,9` but after `5` we want to jump to `8` and then carry on. It means we should change the loop variable inside the loop. A naive mind might thing following approach would work %% .. GENERATED FROM PYTHON SOURCE LINES 260-266 .. code-block:: default for i in range(10): if i == 5: i += 3 print(i) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 1 2 3 4 8 6 7 8 9 .. GENERATED FROM PYTHON SOURCE LINES 267-271 But we had wished the output as `0,1,2,3,4,5,8,9`. The reasons is, for loop iterated over `0,1,2,3,4,5,6,7,8,9` and if we changed current value of `i`, it will not affect next value of `i` at next iteration. We have to use `while` loop in such a scenario. .. GENERATED FROM PYTHON SOURCE LINES 273-281 .. code-block:: default i = 0 while i < 10: if i == 5: i += 3 print(i) i += 1 .. rst-class:: sphx-glr-script-out .. code-block:: none 0 1 2 3 4 8 9 .. GENERATED FROM PYTHON SOURCE LINES 282-284 If we have a list of lists, and we want to flatten all the elements of that list into one list, we can use a nested for loop to achieve this. .. GENERATED FROM PYTHON SOURCE LINES 286-300 .. code-block:: default prime_ministers = [['zafrullah jamali', 'chaudhry shujaat', 'shaukat aziz'], ['yousuf raza gilani', 'raja pervaiz ashraf'], ['nawaz sharif', 'shahid khaqan']] print(len(prime_ministers)) all_pms = [] for era in prime_ministers: for pm in era: all_pms.append(pm) print(all_pms) .. rst-class:: sphx-glr-script-out .. code-block:: none 3 ['zafrullah jamali', 'chaudhry shujaat', 'shaukat aziz', 'yousuf raza gilani', 'raja pervaiz ashraf', 'nawaz sharif', 'shahid khaqan'] .. GENERATED FROM PYTHON SOURCE LINES 301-305 .. code-block:: default print(len(all_pms)) .. rst-class:: sphx-glr-script-out .. code-block:: none 7 .. GENERATED FROM PYTHON SOURCE LINES 306-310 list comprehension ------------------- One of the reasons, python is beautiful is because of its poetry like syntax. Consider following loop .. GENERATED FROM PYTHON SOURCE LINES 310-316 .. code-block:: default numbers = [] for i in range(10): numbers.append(i**2) print(numbers) .. rst-class:: sphx-glr-script-out .. code-block:: none [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] .. GENERATED FROM PYTHON SOURCE LINES 317-318 Now, what if I told you that we can acheive this in one line? .. GENERATED FROM PYTHON SOURCE LINES 318-321 .. code-block:: default numbers = [i**2 for i in range(10)] print(numbers) .. rst-class:: sphx-glr-script-out .. code-block:: none [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] .. GENERATED FROM PYTHON SOURCE LINES 322-326 The method of writing for loop inside the list as we have done above is called list comprehension. We can also have ``if`` and ``else`` statement in list comprehension .. GENERATED FROM PYTHON SOURCE LINES 326-330 .. code-block:: default M = [name for name in NH_receivers if 'Muhammad' in name] print(M) .. rst-class:: sphx-glr-script-out .. code-block:: none ['Muhammad Sarwar', 'Tufail Muhammad', 'Muhammad Akran', 'Muhammad Husain Janjua', 'Muhammad Mahfuz'] .. GENERATED FROM PYTHON SOURCE LINES 331-334 .. code-block:: default my_list = [i**2 if i>5 else i**3 for i in range(10)] print(my_list) .. rst-class:: sphx-glr-script-out .. code-block:: none [0, 1, 8, 27, 64, 125, 36, 49, 64, 81] .. GENERATED FROM PYTHON SOURCE LINES 335-337 list comprehension for nested for loops ---------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 339-343 continue --------- The ``continue`` keyword is used inside the for loop when we want to skip some commands in a particular iteration. .. GENERATED FROM PYTHON SOURCE LINES 343-350 .. code-block:: default for president in ['clinton', 'bush', 'obama', 'trump', 'biden']: if president == 'trump': continue # ok sorry they actually wanted to export freedom! print(f"{president}: Let's go to war") .. rst-class:: sphx-glr-script-out .. code-block:: none clinton: Let's go to war bush: Let's go to war obama: Let's go to war biden: Let's go to war .. GENERATED FROM PYTHON SOURCE LINES 351-358 Above we did not want to print "Let's go to war" when the value of `president` was equal to `trump` so we used ``continue`` statement. That was a too simple example. We had better avoid writing `trump` in the list instead of adding two lines inside the for loop. Usually, the conditioning variable (`president` in our case above) appears after doing some calculations inside for loop. .. GENERATED FROM PYTHON SOURCE LINES 360-362 **Question:** Print all the prime numbers between 1 and 100 using for loop. .. GENERATED FROM PYTHON SOURCE LINES 364-367 break --------- The ``break`` keyword is used if we want to stop the iterations of for loop. .. GENERATED FROM PYTHON SOURCE LINES 367-375 .. code-block:: default saving = 5000 for items in ['fridge', 'laptop', 'mobile', 'tablet', 'tv', 'fryer']: saving = saving - 1000 if saving < 0: print("no more purchase please!") break .. rst-class:: sphx-glr-script-out .. code-block:: none no more purchase please! .. GENERATED FROM PYTHON SOURCE LINES 376-378 If you want to dig deep into how the ``for`` loops work in python, you can jump to :ref:`sphx_glr_auto_examples_oop_magical_methods.py`. .. GENERATED FROM PYTHON SOURCE LINES 381-393 **Question:** What will be the output of following code? .. code-block:: python for i in range(0, 3): print(a) for j in range(0, 2): print(j) for k in range(0, 1): print(k) .. GENERATED FROM PYTHON SOURCE LINES 395-396 .. [1] ``_ .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.016 seconds) .. _sphx_glr_download_auto_examples_basics_for_loops.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/basics/for_loops.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: for_loops.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: for_loops.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_