Super Useful Python Function!! #python #programming #coding

preview_player
Показать описание
This short video shows the usefulness of the Python zip function.

Background Music:
Creative Commons Attribution 3.0 Unported License
Рекомендации по теме
Комментарии
Автор

This is fun for something quick and dirty, but you'll save your sanity if you just make an "Item" class.

latveria
Автор

congratulations, you found a use case for classes

PBalint
Автор

You could also just store the values in a dict. Key=item_name and value=list(attr)

sagetarus
Автор

Most useful I'd think is directly looping over zipped iterables. In this case that could be:
for i, r, w in zip(items, rarity, weight):
...

ewerybody
Автор

why not use classes for this? I think that would be more efficient.

twopolaar
Автор

Sees problem:
Happy dict noises! :D

Sees solution:
Sad compiler noises! :(

fightme
Автор

Coming from node I love python. It’s got so many nifty things makes working with data so much easier

dixztube
Автор

Not only a like, u got a new subscriber. This' really helpful. Thank you 👍

kiddora
Автор

Thanks for the beautiful content.
I appreciate it 🙏🏽🙏🏽

korayalkan
Автор

Bro how the hell did yt know I needed this info? You're a lifesaver, dude. Tysm!!

benjaminmacdavid
Автор

You really should use a class here for the items and maybe even the player. Cleaner more understandable code

johns
Автор

zip works great, but I've learned my lesson, just make a class containing each item and a list of each object item.

jratnerd
Автор

As a c++ dev, I find python scary. Simple and useful, yes. But I am so used to being in control of every single operation that it is scary to see sometimes like that.

matheoml
Автор

from dataclasses import dataclass

@dataclass
class Item:
item_name: str
item_rarity: int
item_weight: float

mystic_sword = Item('Mystic Sword', 99, 1.30)
wooden_shield = Item('Wooden Shield', 56, 1.10)
rock = Item('Rock', 8, 0.01)

inventory = [mystic_sword, wooden_shield, rock]



Done like this you can easily handle the inventory much better instead of zipping it, as you can easily iterate through specific parts of the item. Let's say you wanna view the item rarities by themselves. Simply do this:

for item in inventory:
print(item.item_rarity)

GOTHICforLIFE
Автор

Or a multi dictionary.
Its values are a list of objects or data.

Dokattak
Автор

Thank you 🙏 I have it make a card game in python and each card has its suit, number, and point value (it’s crazy 8s) and this will be useful

orotewilderness
Автор

Why would you ever make 3 lists like this for related items

Logan
Автор

Just wondering, wouldn't a dictionary be much better? (Or is this just to showcase if there is some rare exception and you can't use dicts and you could use this?)

fabiozfortnite
Автор

Sometimes I feel like it would be faster to list all the things an asterisk doesn't do.

nickcampbell
Автор

If you do this, you should read about OOP.

Patrick-vvig