#!/usr/bin/python3 # -*- coding: utf-8 -*- """ Loops - The first major way to repeat sequences of statements Recursion is the other. General form: for element in iterable: statements, potentially using element """ from typing import List # Indentation matters here too for letter in "primes": print(letter) # Preview: this is a List (more next time) primes: List[int] = [2, 3, 5, 7] # It is "iterable" thus and can be looped over for prime in primes: print(prime) """ For loops iterate over lists prints: dog is a mammal cat is a mammal mouse is a mammal """ for animal in ["dog", "cat", "mouse"]: # You can use format() to interpolate formatted strings print("{} is a mammal".format(animal)) """ "range(number)" returns an iterable of numbers from zero to the given number prints: 0 1 2 3 """ for i in range(4): print(i) """ "range(lower, upper)" returns an iterable of numbers from the lower number to the upper number prints: 4 5 6 7 """ for i in range(4, 8): print(i) """ "range(lower, upper, step)" returns an iterable of numbers from the lower number to the upper number, while incrementing by step. If step is not indicated, the default value is 1. prints: 4 6 """ for i in range(4, 8, 2): print(i) """ To loop over a list, and retrieve both the index and the value of each item in the list prints: 0 dog 1 cat 2 mouse """ animals = ["dog", "cat", "mouse"] for i, value in enumerate(animals): print(i, value) """ While loops go until a condition is no longer met. prints: 0 1 2 3 """ x: int = 0 while x < 4: y = 45 print(x) x += 1 # Shorthand for x = x + 1 for x in range(5): print(x) # A common pattern, accumulating values onto the end of an array or string: myaccumarray: List[int] = [] for x in range(3, 6): myaccumarray.append(x) print(x) print(myaccumarray) for x in range(3, 8, 2): print(x) for letter in "Python": # traversal of a string sequence print("Current Letter :", letter) fruits: List[str] = ["banana", "apple", "mango"] for fruit in fruits: # traversal of List sequence print("Current fruit :", fruit) fruits = ["banana", "apple", "mango"] for index in range(len(fruits)): print("Current fruit :", fruits[index]) count: int = 0 while count < 9: print("The count is:", count) count = count + 1 print("Good bye!") count = 0 while count < 5: print(count) count += 1 # This is the same as count = count + 1 # while true loop (aka while 1:) # NOTE: this is a common strategy, pay attention here! count = 0 while True: print(count) count += 1 if count >= 5: break for letter in "Python": # First Example if letter == "h": break print("Current Letter :", letter) var: int = 10 # Second Example while var > 0: print("Current variable value :", var) var = var - 1 if var == 5: break # Prints the odd numbers: for x in range(10): if x % 2 == 0: continue print(x) 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) # While has an else count = 0 while count < 5: print(count) count += 1 else: print("count value reached %d" % (count)) # for also has an else for i in range(1, 10): if i % 5 == 0: break print(i) else: print("this is not printed, because break terminates for loop") # Why use that else? condition: bool = True flag: bool = False thing: int = 5 while thing < 10: if condition: flag = True break thing += 1 if not flag: print("not break") # Becomes thing = 5 while thing < 10: if condition: break thing += 1 else: print("not break") # nested loops. for i in range(1, 11): for j in range(1, 11): k = i * j print(k, end="") print() # can make something like a do while loop: i = 1 while i < 2: # Loop expression print("these are a bit wierd") i += 1 # Loop body: Sub-statements to execute if # the expression evaluated to True else: print("but they exist") # Else body: Sub-statements to execute once # if the expression evaluated to False print("Statements to execute after the loop") for char in "iterable": print(char) # Loop body: Sub-statements to execute # for each item in iterable else: print("nothing here this time") # Else body: Sub-statements to execute # once when loop completes print("Statements to execute after the loop") # to get item number in loop, you might try this: words = ["cool", "powerful", "readable"] for i in range(0, len(words)): print((i, words[i])) # but this is easier for index, item in enumerate(words): print((index, item)) # recall: num1, num2 = [350, 400] # or num1, num2 = (350, 400) # enumerate is similar: origins: List[int] = [4, 8, 10] for (myindex, myvalue) in enumerate(origins): print("Element %d: %s" % (myindex, myvalue)) # don't need the () tuple syntax origins = [4, 8, 10] for myindex, myvalue in enumerate(origins): print(myindex, myvalue)