Created
March 3, 2020 22:44
-
-
Save katronai/cd8d4eaf01c06acadd21c127cb3bee32 to your computer and use it in GitHub Desktop.
Looping in Python 3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Loop through list | |
for item in list: | |
print(item) | |
# Loop through list with index | |
for index, item in enumerate(list): | |
print(index, item) | |
# Loop through dictinary: | |
for key, value in dict.items(): | |
print(key, value) | |
# Loop through Numpy array: | |
for val in np.nditer(np_array): | |
print(val) | |
# Loop through DataFrame: type(row): Series | |
for label, row in df.iterrows(): | |
print(label) | |
print(row) | |
# Loop and decrease without being below a specific value (e.g. 0) | |
value = 30 | |
for i in range(10): | |
value = max(0, value - 1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment