.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python\demo_decision_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 .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_demo_decision_loops.py: Decisions, loops ================ Decision making .. GENERATED FROM PYTHON SOURCE LINES 6-11 .. code-block:: default var = 100 if var == 100: print("value of expression is 100") .. rst-class:: sphx-glr-script-out .. code-block:: none value of expression is 100 .. GENERATED FROM PYTHON SOURCE LINES 13-14 While loop .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: default count = 0 while count < 9: print("The count is: ", count) count = count + 1 .. rst-class:: sphx-glr-script-out .. code-block:: none The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 .. GENERATED FROM PYTHON SOURCE LINES 20-21 While loop with else statement .. GENERATED FROM PYTHON SOURCE LINES 21-28 .. code-block:: default count = 0 while count < 5: print(count, " is less than 5") count = count + 1 else: print(count, " is not less than 5") .. rst-class:: sphx-glr-script-out .. code-block:: none 0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5 .. GENERATED FROM PYTHON SOURCE LINES 29-30 For loop .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: default for letter in "Python": # First Example print("Current Letter :", letter) fruits = ["banana", "apple", "mango"] for fruit in fruits: # Second Example print("Current fruit :", fruit) .. rst-class:: sphx-glr-script-out .. code-block:: none Current Letter : P Current Letter : y Current Letter : t Current Letter : h Current Letter : o Current Letter : n Current fruit : banana Current fruit : apple Current fruit : mango .. GENERATED FROM PYTHON SOURCE LINES 38-39 Iterating by sequence index .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: default fruits = ["banana", "apple", "mango"] for index in range(len(fruits)): print("Current fruit :", fruits[index]) .. rst-class:: sphx-glr-script-out .. code-block:: none Current fruit : banana Current fruit : apple Current fruit : mango .. GENERATED FROM PYTHON SOURCE LINES 44-45 For loop with else .. GENERATED FROM PYTHON SOURCE LINES 45-55 .. code-block:: default for num in range(10, 20): # to iterate between 10 to 20 for idx in range(2, num): # to iterate on the factors of the number if num % idx == 0: # to determine the first factor jdx = num / idx # to calculate the second factor print("%d equals %d * %d" % (num, idx, jdx)) break # to move to the next number, the #first FOR else: # else part of the loop print(num, "is a prime number") break .. rst-class:: sphx-glr-script-out .. code-block:: none 10 equals 2 * 5 11 is a prime number .. GENERATED FROM PYTHON SOURCE LINES 56-57 Nested loops .. GENERATED FROM PYTHON SOURCE LINES 57-68 .. code-block:: default val = 2 while val < 100: val2 = 2 while val2 <= (val / val2): if not (val % val2): break val2 = val2 + 1 if val2 > val / val2: print(val, " is prime") val = val + 1 .. rst-class:: sphx-glr-script-out .. code-block:: none 2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime 53 is prime 59 is prime 61 is prime 67 is prime 71 is prime 73 is prime 79 is prime 83 is prime 89 is prime 97 is prime .. GENERATED FROM PYTHON SOURCE LINES 69-70 Break statement .. GENERATED FROM PYTHON SOURCE LINES 70-82 .. code-block:: default for letter in "Python": # First Example if letter == "h": break print("Current Letter :", letter) var = 10 # Second Example while var > 0: print("Current variable value :", var) var = var - 1 if var == 5: break .. rst-class:: sphx-glr-script-out .. code-block:: none Current Letter : P Current Letter : y Current Letter : t Current variable value : 10 Current variable value : 9 Current variable value : 8 Current variable value : 7 Current variable value : 6 .. GENERATED FROM PYTHON SOURCE LINES 83-84 Continue statement .. GENERATED FROM PYTHON SOURCE LINES 84-96 .. code-block:: default for letter in "Python": # First Example if letter == "h": continue print("Current Letter :", letter) var = 10 # Second Example while var > 0: var = var - 1 if var == 5: continue print("Current variable value :", var) .. rst-class:: sphx-glr-script-out .. code-block:: none Current Letter : P Current Letter : y Current Letter : t Current Letter : o Current Letter : n Current variable value : 9 Current variable value : 8 Current variable value : 7 Current variable value : 6 Current variable value : 4 Current variable value : 3 Current variable value : 2 Current variable value : 1 Current variable value : 0 .. GENERATED FROM PYTHON SOURCE LINES 97-98 Pass statement .. GENERATED FROM PYTHON SOURCE LINES 98-103 .. code-block:: default for letter in "Python": if letter == "h": pass print("This is pass block") print("Current Letter :", letter) .. rst-class:: sphx-glr-script-out .. code-block:: none Current Letter : P Current Letter : y Current Letter : t This is pass block Current Letter : h Current Letter : o Current Letter : n .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.011 seconds) .. _sphx_glr_download_11_demos_python_demo_decision_loops.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_decision_loops.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_decision_loops.ipynb `