# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/ansif-mhmd/learn-python3/blob/master/notebooks/beginner/notebooks/for_loops.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="Pl9sS5bdJHkf"
# # [`for` loops](https://docs.python.org/3/tutorial/controlflow.html#for-statements)

# + [markdown] id="DsgzuVswJHkp"
# ## Looping lists

# + id="Lf-k0-vCJHkr" outputId="a4a4a6e1-d18d-4c2a-f6cf-65c886888544" colab={"base_uri": "https://localhost:8080/"}
my_list = [1, 2, 3, 4, 'Python', 'is', 'neat']
for item in my_list:
    print(item)

# + [markdown] id="YBKSAFvHJHku"
# ### `break`
# Stop the execution of the loop.

# + id="lnlocbXmJHkv" outputId="b4d9eeda-2a1e-4eff-bfe6-700dccaf6471" colab={"base_uri": "https://localhost:8080/"}
for item in my_list:
    if item == 'Python':
        break
    print(item)

# + [markdown] id="nebLktZUJHkw"
# ### `continue`
# Continue to the next item without executing the lines occuring after `continue` inside the loop.

# + id="DQRaJwQ2JHky" outputId="71ca3e9f-2b9d-4d52-fa1d-0c37f7b3fa7e" colab={"base_uri": "https://localhost:8080/"}
for item in my_list:
    if item == 1:
        continue
    print(item)

# + [markdown] id="4zMx3-LCJHk0"
# ### `enumerate()`
# In case you need to also know the index:

# + id="ITTsbFSxJHk1" outputId="cf368696-f847-42b3-ecb2-46c429c53b00" colab={"base_uri": "https://localhost:8080/"}
for idx, val in enumerate(my_list):
    print('idx: {}, value: {}'.format(idx, val))

# + [markdown] id="w-IxHOfJJHk2"
# ## Looping dictionaries

# + id="kCHu0uIdJHk3" outputId="375b15d6-181b-4e4f-b4eb-865b5da283e6" colab={"base_uri": "https://localhost:8080/"}
my_dict = {'hacker': True, 'age': 72, 'name': '<NAME>'}
for key in my_dict:
    print(key)

# + id="D4s9YWo6JHk4" outputId="40e2d8ce-366f-4968-a894-bd4a7775ac7f" colab={"base_uri": "https://localhost:8080/"}
for key, val in my_dict.items():
    print('{}={}'.format(key, val))

# + id="40NlfntSPTbA" outputId="9c06b8ec-cc7f-47ba-a000-3eccab18f067" colab={"base_uri": "https://localhost:8080/"}
for key in my_dict.values():
    print(key)

# + id="L9tuNzTcPd67" outputId="4f7ca8d2-21c2-4c25-f508-2534f1a41e0d" colab={"base_uri": "https://localhost:8080/"}
for key in my_dict.keys():
    print(key)

# + [markdown] id="e0SHrCfKJHk5"
# ## `range()`

# + id="o2BXwt18JHk5" outputId="3ea3911d-79fc-4a84-dcb4-e608afc5e9f1" colab={"base_uri": "https://localhost:8080/"}
for number in range(5):
    print(number)

# + id="jbMkK-ZoJHk6" outputId="d304fe4f-6c96-487c-9acf-dc3fe66c6720" colab={"base_uri": "https://localhost:8080/"}
for number in range(2, 5):
    print(number)

# + id="RNXk5IDQJHk7" outputId="9bfd0fe6-aea6-4f96-c597-4f6ef6523652" colab={"base_uri": "https://localhost:8080/"}
for number in range(0, 10, 2):  # last one is step
    print(number)

# + id="Wyba02wRPO4u"


StarcoderdataJupyterScriptsDedupFiltered