+ 12
What's one cool feature in Python 3 that you've learned about recently and feel that others would benefit from knowing too?
31 Antworten
+ 14
Thoq! I actually recently made a code explaining the difference between += and append()
https://code.sololearn.com/cwIQwwnM8NjA/?ref=app
+ 11
Thomas Williams Yes, I profoundly dig list comprehensions! Did you know there are dictionary comprehensions too?
https://code.sololearn.com/cK7uik9C53a7
+ 10
I also just discovered that "try-except" lines can't catch a SyntaxError. Why? Because they catch EXCEPTIONS and SyntaxError is not an exception (like, e.g. EOFError or NameError), it's a, well, a syntax error.
A code with a syntax error won't run. This means the "try-except" lines never run either.
+ 9
Well, I recently learned that var += 1 is not necessarily the same as var = var + 1 if you use it with mutable values. So using += with lists behaves like append whereas List = List + whatever is a brand new assignment.
And also there will be a major change in Python 3.8 giving us the highly controversial assignment expressions using a socalled walrus operator: :=
This gives you the possibility to assign a name to a value inside an expression like this:
while endCondition:= determineEndCondition():
Here endCondition is assigned the result of determineEndCondition() on each iteration and the while-loop keeps running until endCondition isn't False or None etc.
+ 9
It finally sank in that in comprehensions with conditions,
'if' goes AFTER 'for' but 'if-else' goes BEFORE 'for'
https://code.sololearn.com/cUYk1EkgmVyf
+ 8
How to create your own python module
https://code.sololearn.com/cNW7BiRF0ELJ/?ref=app
+ 7
Thoq! After 10 minutes of research on the walrus operator, I'm still at a loss as to how it works. 😁 Probably need to skill up a bit.
+ 7
Sry, should have explained more. Knowing how to add lists is important for recursive programming. So when doing a graph search you can use append or += for mutating a list in global or an upper scope to "store" results (like a path that lead to a specific node). But to find such paths in the first place to you need a new assignment in each function scope giving each scope its own current version of the path. Hope that makes sense.
+ 7
I totally understand. I am not trying to avoid it but I usually prefer the iterative approach.
+ 6
Nice. I learned it the hard way while writing a dfs (the recursive version).
+ 5
Well list comprehensions have been around for a while now, but only recently have I started using them. Now I use them all the time in my applications. Check it out
https://code.sololearn.com/ccaEkQ35qJGV/?ref=app
https://code.sololearn.com/chu0Xaheiq4W/?ref=app
https://code.sololearn.com/cr2bE8cvDCSN/?ref=app
https://code.sololearn.com/c91RSRAdroXg/?ref=app
https://code.sololearn.com/cB5nLaMs0HrK/?ref=app
https://code.sololearn.com/cZ0ljNOI3SOq/?ref=app
These were all done in a time period of about 2 days after I discovered it😅
+ 5
+ 4
Tuples (Immutable lists) 🙄
+ 4
Another one I just discovered and will be using for more complex projects with many functions/variables:
type hinting
Especially useful if working on a team as it makes code more readable
A simple example here:
https://code.sololearn.com/ch58CLulWJcQ/#py
+ 3
Yep. I used set comprehensions too, one of these codes needed one but I cant remember which😅
Edit: it was the pattern solver. To get the second difference I needed unique values. If there was more than one value in there, then it wasnt a pattern or the program cant solve it
Another one was the Vowel and Consonants one. I wanted unique values for each letter
+ 3
Thomas Williams Nice, I didn't know about set comprehensions.
+ 3
David Ashton Great point! I always end up googling it every time I need an if-else condition in a list comprehension. Hopefully it will stick this time!
+ 3
# unpacking [*] vs list()
[*filter(lambda x: x.isalpha, "ab1")]
+ 2
S-Stefanova, they are really great if you want unique values for each item. Check the pattern solver and the vowels one and see if you can spot them🔎
+ 2
Thomas Williams Depth First Search (had to google it)