OldGeeksGuide · @oldgeeksguide
2 followers · 12 posts · Server mstdn.social

decimal module is cool, but be careful to use strings for initialization, or you might not get what you want:

>>> Decimal(1.5)
Decimal('1.5')
>>> Decimal(1.6)
Decimal('1.600000000000000088817841970012523233890533447265625')
>>> Decimal('1.6')
Decimal('1.6')

#repostfromapastlife #pythontip #Python

Last updated 2 years ago

OldGeeksGuide · @oldgeeksguide
17 followers · 87 posts · Server mstdn.social

decimal module is cool, but be careful to use strings for initialization, or you might not get what you want:

>>> Decimal(1.5)
Decimal('1.5')
>>> Decimal(1.6)
Decimal('1.600000000000000088817841970012523233890533447265625')
>>> Decimal('1.6')
Decimal('1.6')

#repostfromapastlife #pythontip #Python

Last updated 2 years ago

Bruno Rocha :rd: :vfy: · @rochacbruno
902 followers · 40 posts · Server bolha.us

Do you know that `0` when passed to `open` function is a shortcut to open stdin stream input in ?

#python #pythontip

Last updated 2 years ago

Geeksta · @geeksta
0 followers · 6 posts · Server mastodon.social
Florian Pircher · @florian
75 followers · 47 posts · Server typo.social

In case you ever need to jump around in Python code without using functions

#python #pythontip

Last updated 2 years ago

Bite Code · @bitecode
119 followers · 4936 posts · Server framapiaf.org

RT @python_tip@twitter.com

Get key with minimum value in a dict:

>>> d = {'a':11, 'b':2, 'c':14, 'd':2}
>>> min(d, key=d.get)
'b'

>>> min_value = min(d.values())
>>> [k for k, v in d.items() if v==min_value]
['b', 'd']

adapted from by @bguberfain@twitter.com

🐦🔗: twitter.com/python_tip/status/

#pythontip

Last updated 3 years ago

Bite Code · @bitecode
121 followers · 4883 posts · Server framapiaf.org

RT @python_tip@twitter.com

If you want to get multiple items from a dictionary, you might either use a list comprehension

>>> [mydict[x] for x in mykeys]

or you can use `operator.itemgetter` as follows

>>> from operator import itemgetter
>>> itemgetter(*mykeys)(mydict)

from @IDryer@twitter.com 👇 twitter.com/IDryer/status/1196

🐦🔗: twitter.com/python_tip/status/

#pythontip

Last updated 5 years ago

Bite Code · @bitecode
121 followers · 4883 posts · Server framapiaf.org

RT @python_tip@twitter.com

Use beautiful_date when need to create date/datetime objects in a simple way.

pip install beautiful-date

from beautiful_date import *

>>> d = 5/Mar/2019
BeautifulDate(2019, 3, 5)

>>> d[4:13]
datetime.datetime(2019, 3, 5, 4, 13)

from @KuzmovychBox@twitter.com

🐦🔗: twitter.com/python_tip/status/

#pythontip

Last updated 5 years ago