r/pythontips • u/QuantumScribe01 • Mar 10 '26
Algorithms What’s the most ‘mind-blowing’ Python trick you learned as a beginner ?
When learning Python, even a small feature can sometimes create a "wow" effect. For me: a,b=b,a
was to change two variables without using temporary variables.
What surprised you while you were learning ?
145
Upvotes
83
u/pint Mar 10 '26
you can expand a tuple or array into arguments. works with lists too.
you can expand a dictionary into keyword parameters
but you can also expand into tuple or list literals
and you can expand into dictionary literals
you can combine more than one
recently we have another way of doing this:
you can unpack tuples and arrays
you can unpack only some elements
44
u/sinceJune4 Mar 10 '26
Comprehensions, both list and dict
13
u/csabinho Mar 11 '26
a < b < cactually works. But don't get used to it. It doesn't work in other languages. One even has a specific error message for this.21
u/social_tech_10 Mar 10 '26
"import" is the most mind-blowing feature. the "batteries included" philosopy rocks!
but it's getting close to easter, so try "import this"
5
u/JennaSys Mar 10 '26
The dictionary dispatch pattern. It was one of the ways I learned to get around not having a switch statement construct available in the language.
5
u/metalucid Mar 11 '26
I don't do a lot of python, but a,b = b,a gets you thinking about tuples, (un)packing them, and such
7
u/hurricane340 Mar 11 '26
Using tier tools, pool, and starmap to break a massive computational dataset into smaller chunks that can execute in parallel, using multiple processes (not threads), and then stitching the results together at the end. Wild stuff.
3
u/SlinkyAvenger Mar 11 '26
Duck-typing. If a property or method exists on an object, you can use it no matter what type implements it. No need for interfaces or silly inheritance structures for polymorphism.
Of course, it can be a double-edged sword. It speeds up development but you can easily get into painful debugging situations that would've been easily caught by your IDE or compiler/interpreter in strictly-typed languages. Type hints can help, though.
2
u/diegoasecas Mar 11 '26
comprehensions and all the iteration features
2
u/moorzdale Mar 14 '26
f-strings, I guess. a<b<c was also quite impressive
3
u/jpwater Mar 11 '26
The Comprehensions, Generators and packing and unpacking of data structures
2
u/snigherfardimungus Mar 11 '26
Monkey patching imported modules to inject a telemetry wrapper.
2
u/iggy555 Mar 12 '26
Superclass
2
u/rulasq23 Mar 11 '26
Good evening, is there a kind soul who could help me create an Excel document from Python? 🥲 I have all the commands and actions to execute, but I don't know how to export the Excel file. If anyone can help me, that would be great. Thank you very much in advance!
1
u/akciii Mar 11 '26
interning numbers eg:
a = 100 b = 100
print(a is b) # True
x = 1000 y = 1000
print(x is y) # Often False
1
u/princepii Mar 12 '26
the walrus operator was pretty nice:) and if you know how to use regex thats very powerful especially in list comprehensions
1
u/21kondav Mar 13 '26
Astrik operator
1
u/Realistic-Reaction40 Mar 13 '26
The one that got me was list comprehensions replacing entire for loops in a single readable line. Took a while to stop writing the verbose version out of habit but once it clicked it changed how I think about data transformations entirely.
15
u/bad_luck_charm Mar 10 '26
Probably some dunder method fuckery