Daily Python Tip 🐍🐧 retweeted
Here's an interesting Python brainteaser that came from a member asking a question on The Python Coding Place forum…
Can you figure out why the outputs are the way they are for these two similar but not identical bits of code?
The answer gives a great insight on iterators…
![data = [1, 2, 3, 4, 5, 6, 7]
for number in range(1, 5):
data = (item for item in data if item != number)
print(list(data))
# [1, 2, 3, 5, 6, 7] Note 4 is missing
data = [1, 2, 3, 4, 5, 6, 7]
for number in range(1, 5):
data = [item for item in data if item != number]
print(list(data))
# [5, 6, 7]](https://pbs.twimg.com/media/GSg8DPIWcAAsCXW.jpg?name=small&format=webp)
ALT data = [1, 2, 3, 4, 5, 6, 7] for number in range(1, 5): data = (item for item in data if item != number) print(list(data)) # [1, 2, 3, 5, 6, 7] Note 4 is missing data = [1, 2, 3, 4, 5, 6, 7] for number in range(1, 5): data = [item for item in data if item != number] print(list(data)) # [5, 6, 7]
Daily Python Tip 🐍🐧 retweeted
New in #Python 3.12 -> math.sumprod()
Return the sum of products of values from two iterables p and q.
Given:
>>> p = (1, 2, 3)
>>> q = (4, 4, 4)
Before:
>>> import operator
>>> import itertools
>>> sum(itertools.starmap(operator.mul, zip(p, q, strict=True)))
24
(= 1*4 + 2*4 + 3*4)
Now:
>>> import math
>>> math.sumprod(p, q)
24
#tips
Daily Python Tip 🐍🐧 retweeted
Iterable unpacking (star in a #python list) makes a list from multiple iterables. map() is a handy way to call functions (or classes!) on a set of values. Together they are powerful:
Daily Python Tip 🐍🐧 retweeted
"Is this an integer?" Three different #python meanings:
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip
Oh, and I heard list comprehensions are deprecated. This is going to be a huge release...
xitter.cf/bascodes/status/150978…
+++ Breaking News +++
After adding Exception Groups and Task Groups, @ThePSF considers Python 3.11 ready for larger Workgroups.
Along with the announced changes, the PSF launched a pre-launch campaign with a clean and modern rebranding.
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip @bascodes
we are using this guy for years :) pretty similar, but has also configurable delay :)
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip
unyt is another option and arguably has a simpler api
>>> import unyt as u
>>> print((3 * u.m + 7 * u.inch).to(u.m))
3.1778 m
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip
Combine this with map, filter or reduce and it can be very handy
Daily Python Tip 🐍🐧 retweeted
Please check out dailypythontip.herokuapp.com… I created it to organize all the tips you post for the public. I can add a platform to it for submission of tips
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip @rickjdon
functions work that way too:
>>> def f(): print('hi')
...
>>> f.__call__()
hi
and even
>>> f.__call__.__call__.__call__.__call__.__call__.__call__()
hi
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip
You should try rich too github.com/willmcgugan/rich
Daily Python Tip 🐍🐧 retweeted
Help us map out an accurate landscape of the Python community – join the official Python Survey 2020. surveys.jetbrains.com/s3/c12…
Daily Python Tip 🐍🐧 retweeted
You probably mean #fastcore not #fastpages
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip
In cases where there are duplicate items, I find enumerate very helpful
>>> a = ['car', 'bike', 'scooter', 'bike']
>>> [n for n,i in enumerate(a) if i == 'bike']
[1, 3]
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip
This Meetup with Wild Code School - Dublin @Wild_Dublin could be fun especially for #codenewbies: Creating pixel art while learning #HTML, #CSS and #javascript 👾 meetu.ps/e/HSxKv/Hkct1/d
Daily Python Tip 🐍🐧 retweeted
The #CfP for #PyConCZ 2020 is open! Would you like to recommend some #Python #libraries? Submit a talk cz.pycon.org/2020/cfp/ and join us in Ostrava in June! 🌞
#czech #python #conference #ostrava #june #pycon
Daily Python Tip 🐍🐧 retweeted
Replying to @python_tip
Wait till you try:
>>> {float('nan') : 1, float('nan'): "Good night", float('nan'): "Bye"}























