DEV Community

Cover image for Python Tips & Tricks Day 1
ahmed elboshi
ahmed elboshi

Posted on

3

Python Tips & Tricks Day 1

🎩✨ 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')

Enter fullscreen mode Exit fullscreen mode

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')

Enter fullscreen mode Exit fullscreen mode

Say goodbye to dragging your knuckles through endless loops and hello to coding joy! 🚀💻 #PythonPartyTrick #UpgradeYourCode 🌟

Top comments (2)

Collapse
 
msopacua profile image
msopacua

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:

names = ['Alice', 'Bob', 'Charlie']
ages = [30, 25, 35]

for i, name in enumerate(names):
    print(f'{name} is {ages[i]} years old')

Enter fullscreen mode Exit fullscreen mode

And then it's equally long.

Collapse
 
ahmed__elboshi profile image
ahmed elboshi

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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay