+ 12

What's one cool feature in Python 3 that you've learned about recently and feel that others would benefit from knowing too?

16th May 2019, 12:06 PM
S-Stefanova
S-Stefanova - avatar
31 Respuestas
+ 14
Thoq! I actually recently made a code explaining the difference between += and append() https://code.sololearn.com/cwIQwwnM8NjA/?ref=app
16th May 2019, 2:02 PM
Trigger
Trigger - avatar
+ 11
Thomas Williams Yes, I profoundly dig list comprehensions! Did you know there are dictionary comprehensions too? https://code.sololearn.com/cK7uik9C53a7
16th May 2019, 12:33 PM
S-Stefanova
S-Stefanova - avatar
+ 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.
18th May 2019, 5:12 AM
David Ashton
David Ashton - avatar
+ 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.
16th May 2019, 1:44 PM
Thoq!
Thoq! - avatar
+ 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
18th May 2019, 4:00 AM
David Ashton
David Ashton - avatar
18th May 2019, 7:46 AM
Nerderkips
Nerderkips - avatar
+ 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.
16th May 2019, 2:08 PM
S-Stefanova
S-Stefanova - avatar
+ 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.
16th May 2019, 3:39 PM
Thoq!
Thoq! - avatar
+ 7
I totally understand. I am not trying to avoid it but I usually prefer the iterative approach.
16th May 2019, 4:55 PM
Thoq!
Thoq! - avatar
+ 6
Nice. I learned it the hard way while writing a dfs (the recursive version).
16th May 2019, 2:09 PM
Thoq!
Thoq! - avatar
+ 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😅
16th May 2019, 12:20 PM
Trigger
Trigger - avatar
17th May 2019, 1:34 AM
Steven M
Steven M - avatar
+ 4
Tuples (Immutable lists) 🙄
17th May 2019, 7:14 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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
17th May 2019, 8:16 AM
S-Stefanova
S-Stefanova - avatar
+ 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
16th May 2019, 12:45 PM
Trigger
Trigger - avatar
+ 3
Thomas Williams Nice, I didn't know about set comprehensions.
16th May 2019, 12:49 PM
S-Stefanova
S-Stefanova - avatar
+ 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!
18th May 2019, 5:15 AM
S-Stefanova
S-Stefanova - avatar
+ 3
# unpacking [*] vs list() [*filter(lambda x: x.isalpha, "ab1")]
23rd May 2019, 12:42 AM
alex_shchegretsov
alex_shchegretsov - avatar
+ 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🔎
16th May 2019, 12:50 PM
Trigger
Trigger - avatar
+ 2
Thomas Williams Depth First Search (had to google it)
16th May 2019, 2:20 PM
S-Stefanova
S-Stefanova - avatar