Learning Python

13. Loops

13.1. While loop

#!/usr/bin/env python3

def main():
    i = 1
    while i < 6:
        print(i)
        i += 1


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    i = 1
    while i < 6:
        print(i)
        if i == 3:
            break
        i += 1


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    i = 0
    while i < 6:
        i += 1
        if i == 3:
            continue
        print(i)


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    i = 1
    while i < 6:
        print(i)
        i += 1
    else:
        print("i is no longer less than 6")


if __name__ == "__main__":
    main()

13.2. For loop

#!/usr/bin/env python3

def main():
    fruits = ["apple", "banana", "cherry"]
    for x in fruits:
        print(x)


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    for x in "banana":
        print(x)


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    fruits = ["apple", "banana", "cherry"]
    for x in fruits:
        if x == "banana":
            break
        print(x)


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    fruits = ["apple", "banana", "cherry"]
    for x in fruits:
        if x == "banana":
            continue
        print(x)


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    for x in range(6):
        print(x)


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    for x in range(2, 6):
        print(x)


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    for x in range(2, 30, 3):
        print(x)


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    for x in range(6):
        print(x)
    else:
        print("Finally finished!")


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    for x in range(6):
        if x == 3:
            break
        print(x)
    else:
        print("Finally finished!")


if __name__ == "__main__":
    main()
#!/usr/bin/env python3

def main():
    adj = ["red", "big", "tasty"]
    fruits = ["apple", "banana", "cherry"]

    for x in adj:
        for y in fruits:
            print(x, y)


if __name__ == "__main__":
    main()