π©β¨ Python Party Trick vs. Stone Age Code! β¨π©
Hey Python pals! π Tired of coding like a caveman? π¦ Let's upgrade that Stone Age script with a slick Python party trick! π§ββοΈ
The Long Way (aka Caveman Coding):
names = ['Alice', 'Bob', 'Charlie']
ages = [30, 25, 35]
for i in range(len(names)):
name = names[i]
age = ages[i]
print(f'{name} is {age} years old')
The Pythonic Party Trick (aka Zip & Star Power):
names = ['Alice', 'Bob', 'Charlie']
ages = [30, 25, 35]
for name, age in zip(names, ages):
print(f'{name} is {age} years old')
Say goodbye to dragging your knuckles through endless loops and hello to coding joy! ππ» #PythonPartyTrick #UpgradeYourCode π
Top comments (2)
Yes, zip is great. But your original is only longer because it sets name and age as variables, either for readability, better debugging or unfamiliarity with format string capabilities.
One unfamiliar with zip would write the first one as:
And then it's equally long.
I admire your mindset; it's a fantastic approach! Essentially, what I'm aiming for is to create very simple code tailored for budding ninja coders. I provide small examples showcasing the usage of the built-in 'zip' function.