# The following prints Hello! 80 times (why?)
for y in range(10):
for x in range(8):
print("Hello!")
# How many times will Hello! get printed here:
for y in range(4):
for x in range(y):
print("Hello!")
# When y = 0, number of iterations of the inner loop is 0
# When y = 1, number of iterations of the inner loop is 1
# When y = 2, number of iterations of the inner loop is 2
# When y = 3, number of iterations of the inner loop is 3
# Total number of iterations: 6
# Print a rectangle composed of stars with the given height and width
def printRectangle(height, width):
for row in range(height):
for col in range(width):
print("*", end= " ")
print()
printRectangle(4, 3)
# This prints:
# * * *
# * * *
# * * *
# * * *
# Multiplication table (adjust the spacing to make it look pretty)
for y in range(1, 10):
for x in range(1, 10):
print(y*x, end=" ")
print()
# Print a triangle composed of stars with the given height
def printTriangle(height):
for y in range(height):
for x in range(height-y):
print("*", end=" ")
print()
printTriangle(4)
# This prints:
# * * * *
# * * *
# * *
# *
# You can "eliminate" nested loops using helper functions
def printTriangle(height):
for y in range(height):
printStarRow(height-y)
print()
def printStarRow(n):
for x in range(n):
print("*", end=" ")
# Given a string s, check for duplicated characters
def hasDuplicates(s):
for i in range(len(s)-1):
for j in range(i+1,len(s)):
if(s[i] == s[j]): return True
return False