FizzBuzz in Python and Swift

Let’s take a look at the classic FizzBuzz problem. First in Python:

for i in range(1, 21):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

In Swift:

for i in 1...20 {
if i % 3 == 0 && i % 5 == 0 {
print("FizzBuzz")
}
else if i % 3 == 0 {
print("Fizz")
}
else if i % 5 == 0 {
print("Buzz")
}
else {
print(i)
}
}

Now look at how both languages handle FizzBuzz with arrays. Python first:

result = []

for i in range(1, 21):
if i % 3 == 0 and i % 5 == 0:
result.append("FizzBuzz")
elif i % 3 == 0:
result.append("Fizz")
elif i % 5 == 0:
result.append("Buzz")
else:
result.append(str(i))

print(result)

And in Swift:

var result: [String] = []

for i in 1...20 {
if i % 3 == 0 && i % 5 == 0 {
result.append("FizzBuzz")
}
else if i % 3 == 0 {
result.append("Fizz")
}
else if i % 5 == 0 {
result.append("Buzz")
}
else {
result.append(String(i))
}
}

print(result)

I am loving Swift so far. In something basic like a FizzBuzz maybe knowing Python has helped out a bit.